'Enter' key in AutoSuggestBox calls to QuerySubmitted event, like clicking the icon. It is a Windows 10 project.
I need to discriminate the Enter key because I use it to go to the next field. I tried KeyDown event, but it is not called.
How can I do it?
Don't ask me why, but you need to use KeyUp event and everything works fine.
Ex:
private void ContactsBox_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
//some stuff here
}
}
The following code is to test the LWin keyup function in C# when the form is active. It's working fine and now when the form is active I need only the function alone has to take place and whenever I click on Lwin button start menu should not open. How can I achieve this?
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.LWin)
{
MessageBox.Show("Function working!");
}
}
What have you tried?
Try setting e.Handled to True.
Try the same thing with the KeyPress event. And the KeyDown event.
If these options don't work (and I suspect they won't), you've exhausted the options available to you in managed Windows Forms. I can't immediately think of a solution using P/Invoke, but I think that's the avenue you'll have to explore.
I'm currently stumped. I can't seem to get the KeyEvent to work. Simple code like this just won't respond to the key I'm pressing. I've tried KeyDown and KeyPress. No errors while compiling... what is causing this?? It will just let me enter the E key without prompting the MEssage box.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.E)
{
MessageBox.Show("E");
}
}
I think you should be using the PreviewKeyDown Event, for example, instead of the standard key events, as sometimes these events are blocked an not bubbled up through the control.
You should change the Form KeyPreview property to true where textbox1 is located.
I have textboxes in my Windows Forms application, and I want that when the user presses the ENTER key then the cursor goes to the next textbox.
How do I do this?
Is this a good habit or shall I avoid it? Actually the users are very much prone and have adapted and have become habitual of pressing ENTER key for navigation between textboxes and buttons. So, for them I need to do this.
Please help me with the complete code using two text-boxes as an example.
I would say the nicest way is to create a user control that inherits from TextBox and then override the OnKeyPress method to capture enter and send a tab. Focus will then be given to the next TabIndex on the form, just as though a tab had actually been entered.
The code below does exactly that:
public partial class CustomTextbox : TextBox
{
public CustomTextbox()
{
InitializeComponent();
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
e.Handled = true;
SendKeys.Send("{TAB}");
}
}
}
You could also put similar code in the KeyPress event handlers for your controls but this saves a lot of duplicate code and unnececessary event handler.
As for whether this is good practice - I would say in general, no, changing the default behaviour of forms is never a good idea, but of course, if this is what your users want and expect, then it is their decision.
This is a bad idea. The standard UI is for TAB to move between input fields. You make your app less useable when you elect not to follow well known standards. These standards are what makes UI intuitive.
Yes, it depends on you
For a sample, you can place 4 textbox on the form and use the following code
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
textBox2.Focus();
}
}
private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
textBox3.Focus();
}
}
private void textBox3_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
textBox4.Focus();
}
}
It might help you.
Happy coding...
For example you could trap OnKeyUp event, check if it is RETURN and process source control to use Focus() to next control...
Yes you can fire the KeyDown or KeyUp event on a TextBox. To check whether it was the enter key, you can do the following:
//e is the KeyEventArgs from the event.
e.KeyCode == Keys.Enter
Then, if he has pressed the enter key, you can do:
System.Windows.Forms.Control.SelectNextControl();
To set the order of your controls, in Visual Studio look for this little icon:
http://i.stack.imgur.com/nZWLO.png
Click it, and you'll go into tab ordering mode, as I like to call it. Just click the controls in the order you wish them to be and after you're done, click the little icon again. Presto!
Now whether that is a good idea of not, completely depends on how used to it your end users are. If they have always used it like this, and you give them something that doesn't fit into their mind model, they are going to say your software is broken.
Always always always try to emulate what processes the user already has in place in their head.
Read this if you have the time, it's a really light and very good read:
http://www.joelonsoftware.com/uibook/fog0000000249.html
I think the best way would be to:
1) assign each textbox's TabIndex attribute incrementally (first is x, next is x+1 etc).
2) capture on the OnKeyUp event on the whole form, check the argument to see if the key was RETURN
3) focus the next textbox using its TabIndex. Or simulate the TAB key.
This code should work:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
GetNextControl((TextBox)sender, true);
}
}
This way you end up writing only one function, and you can have as many textboxes as you want.
In order to transfer text from one textbox to another, I have created a submit button. However it would be preferable to use the functionality of the 'enter' key.
I am not sure but i think the ascii code is 13.Anyway how do I go about this task at hand?
Look at the Form.AcceptButton property.
You can subscribe to the KeyUp event of the text box.
using System.Windows.Forms;
private void txtInput_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
DoSomething();
}