Click here to Skip to main content
12,628,919 members (34,427 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hi all,
I m tryng to do the following code C# windows application
 private void txtRollNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar=Keys.Enter)
}
but is showing implicite conversion is error
Please help


Thanks & Regards
Indrajit
Posted 12-Apr-11 22:02pm
Updated 12-Apr-11 22:03pm
v2
Comments
Toniyo Jackson 13-Apr-11 3:04am
   
Added pre tag for code
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

just check your if statement..you are actually assigning a value..use == instead.
simple mistake.


hope it helps!!
  Permalink  
Comments
SAKryukov 13-Apr-11 3:13am
   
Detected! My 5.
--SA
girish sp 13-Apr-11 3:16am
   
thanks SAK..
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

You need to cast the key to char and need to give == instead of =.

Try this,
if (e.KeyChar == (char)Keys.Enter)
{
//logic here

}
  Permalink  
v3
Comments
SAKryukov 13-Apr-11 3:18am
   
Not quite. This is not a key press. Please see my Answer.
--SA
Tarun.K.S 13-Apr-11 3:38am
   
Correct answer, it did work.
Proposed as answer.
Toniyo Jackson 13-Apr-11 3:40am
   
Thanks Tarun
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Use the equality operator == if you want to compare for equality.

private void txtRollNumber_KeyPress(object sender, KeyPressEventArgs e)
{
 if (e.KeyChar == (char)Keys.Enter)
  {
  //Do your operation here
  }
}

Now its should work!

Edit : Added (char) to cast it to char type.
  Permalink  
v4
Comments
SAKryukov 13-Apr-11 3:20am
   
Tarun, I already voted 5 but later saw: it won't even compile: you compare different type.
(Let my vote be an upfront pay for something else.)
This is not key press.
Please see my Answer.
--SA
Tarun.K.S 13-Apr-11 3:30am
   
Actually it did work. I have updated my answer to cast it to "Char".
You can vote a neutral 3 though.
Toniyo Jackson 13-Apr-11 3:21am
   
Did you try this? It will not compile itself
Tarun.K.S 13-Apr-11 3:31am
   
Thanks Toniyo, I have added "char" for type casting.
Toniyo Jackson 13-Apr-11 3:37am
   
Welcome
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

To detect Enter you need to use KeyDown instead of KeyPress, and use:

myTextBox.KeyDown += (sender, eventArgs) => {
    //can optionally check eventArgs.Modifiers
    if (eventArgs.KeyCode == Keys.Enter)
        //...
};

(As there was a concern: this code was compiled, checked up.)

—SA
  Permalink  
v5
Comments
Tarun.K.S 13-Apr-11 3:37am
   
I didn't get the KeyCode working, maybe I am missing something, I will try it again.
Tarun.K.S 13-Apr-11 3:46am
   
Sorry SA, I am not able to get it work. Could you elaborate more?
SAKryukov 13-Apr-11 3:58am
   
Please see it in more detail. Tested.
--SA
SAKryukov 13-Apr-11 4:02am
   
And one more detail (in comment) -- eventArgs.Modifiers.
--SA
Tarun.K.S 13-Apr-11 4:08am
   
Perfect this also works fine! 5d!
But I have a doubt, are KeyDown and KeyPress the same?
Tarun.K.S 13-Apr-11 4:11am
   
Well found it myself. KeyDown has more goodies in its eventHandler when compared to KeyPress.
SAKryukov 13-Apr-11 4:13am
   
From the other hand, KeyPress can cancel a press! Will you see my previous (or next Answer) on related topic?
--SA
Tarun.K.S 13-Apr-11 4:26am
   
I shall have a look for sure.
SAKryukov 13-Apr-11 4:17am
   
They are not the same, not at all. KeyPress is not fired on many cases KeyDown/KeyUp do, but if KeyPress is fired, KeyDown always goes before.
KeyDown/KeyUp are low-level, KeyPress is "resulting" and capture characters, not keys, but also can be canceled -- important to filter our characters.
--SA
Tarun.K.S 13-Apr-11 4:27am
   
Hmmm now it makes sense. Thanks for the heads-up. :)
SAKryukov 13-Apr-11 4:42am
   
Sure. You're welcome.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 8

Hi, here i give you a code where you can also get a history of keys.
the mainpart will be highlighted with reagion MAIN

#region just for fun :D
 
private string _lastKeys = "";
private readonly Dictionary<string,> _keyChecker = new Dictionary<string,>
	{
		// key code to press, msgbox header, msgbox text or
		// key code to press, switch to function execute, function to execute
		{"iddqd", new[] {"Cheater! :-)", "Godmode activated!"}},
		{"idkfa", new[] {"Cheater! :-)", "All Weapons unlocked!"}},
		{"aAa", new[] {"Testing", "Test!"}},
		{"aaa", new[] {"function", "close"}}
	};
 
private void KeyChecker(KeyEventArgs e)
{
	_lastKeys += e.Shift
		? ((char) e.KeyValue).ToString(CultureInfo.InvariantCulture)
		: ((char) e.KeyValue).ToString(CultureInfo.InvariantCulture).ToLower();
 
	foreach (var key in _keyChecker.Keys)
		if (_lastKeys.EndsWith(key))
		{
			if (_keyChecker[key][0] != "function")
				MessageBox.Show(_keyChecker[key][1], _keyChecker[key][0]);
			else
				KeyCheckerFunction(_keyChecker[key][1]);
			_lastKeys = "";
		}
 
	while (_lastKeys.Length > 100)
		_lastKeys = _lastKeys.Substring(1);
}
 
private void KeyCheckerFunction(string func)
{
	switch (func)
	{
		case "close":
			Close();
			break;
	}
}
 
#endregion just for fun :D

#region MAIN
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
	switch (e.KeyData)
	{
		case Keys.F1:
			MessageBox.Show("F1");
			break;
		case (Keys.F1 | Keys.Shift):
			MessageBox.Show("F1 + Shift");
			break;
		default:
			if (e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z)
				KeyChecker(e);
			break;
	}
}
#endregion MAIN
  Permalink  
v2
Comments
Nooa 28-Nov-13 11:38am
   
and yes, now i realized, this question was 2 yeahrs old...
still i think my answer is good ;)

but sorry for this push again, realized it to late :(
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

  Permalink  
v2
Comments
ProgramFOX 11-Nov-12 13:02pm
   
I don't understand why you post an answer to a question that's more then a year old.
Nelek 11-Nov-12 14:24pm
   
Not only a year old, but solved and provoqued by a typo
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

private void newkey(object sender, KeyEventArgs e)
       {
           if (e.KeyCode  == Keys.Enter) textBox2.Focus();
       }
 
       private void textBox1_TextChanged(object sender, EventArgs e)
       {
           this.KeyDown += new System.Windows.Forms.KeyEventHandler(newkey);
           this.KeyPreview = true;
       }

HERE I POSTE THE CORRECT SOLUTION FOR ALL TYPE KEYDOWN EVENT PROBLEMS IN C#.....WORK AT ANY COST..
  Permalink  
Comments
CHill60 7-Nov-13 8:25am
   
I'm not sure why you're posting this answer to a topic that was answered more than 2 years ago. The problem was a typing error ... if statements require ==
Reason for my downvote - Your solution won't be appropriate for all keydown event problems at all and please don't shout (CAPITALS are considered shouting)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 10

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
       {
           if (e.KeyChar == (char)Keys.Enter)
           {
               MessageBox.Show("Enter Pressed");
           }
       }


Best of luck
  Permalink  
Comments
CHill60 30-Nov-13 10:04am
   
Question was answered clearly 2 years ago

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 29 Nov 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100