Click here to Skip to main content
15,886,562 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Updated 12-Apr-11 21:03pm
v2
Comments
Toniyo Jackson 13-Apr-11 3:04am    
Added pre tag for code

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


hope it helps!!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Apr-11 3:13am    
Detected! My 5.
--SA
girish sp 13-Apr-11 3:16am    
thanks SAK..
You need to cast the key to char and need to give == instead of =.

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

}
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 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
Use the equality operator == if you want to compare for equality.

C#
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.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 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
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
 
Share this answer
 
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?
Sergey Alexandrovich Kryukov 13-Apr-11 3:58am    
Please see it in more detail. Tested.
--SA
Sergey Alexandrovich Kryukov 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?
Hi, here i give you a code where you can also get a history of keys.
the mainpart will be highlighted with reagion MAIN

C#
#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
 
Share this answer
 
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 :(
 
Share this answer
 
v2
Comments
Thomas Daniels 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
C#
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..
 
Share this answer
 
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)
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
       {
           if (e.KeyChar == (char)Keys.Enter)
           {
               MessageBox.Show("Enter Pressed");
           }
       }



Best of luck
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900