I have 2 combo boxes and one text box(combo1, combo2, textBox). Here is the code for event key_down:
private void MyForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
buttonSearch_Click(sender, e);
}
When I click button ENTER on keyboard I want that program call search button on form. The problem is when I select some item from combo box and click on ENTER to give me that item, he also call searh button, ofcource, but I dont want to call search until I fill both combo boxes and text box. So, I want to call search button ONLY when my focus is on text box. Any idea how to do that?
as said, you could put the event on the textbox. Also, going with your original problem, you could check if the textbox has focus:
private void MyForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (textBox1.Focused) // or whatever your textbox is called
{
buttonSearch_Click(sender, e);
}
}
}
You have specific events for each control. You are using the Form events but if you only want to have the keydown when the Textbox is focussed I suggest the following:
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
buttonSearch_Click(sender,e);
}
Related
The if statement is being checked until I hit enter then it goes straight to another method. My guess is there is something else on the form that is getting triggered when I hit enter but I can't find it despite my search.
I want to not have to put a button on the form to call this function, the button I had worked but I just want to be able to hit enter from my textbox input.
Here is my code below:
private void textBox1firstName_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
searchAD();
}
}
private void textBox2lastName_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
searchAD();
}
}
searchAD() is not getting called despite hitting enter. Any suggestions? Thank you!
The form has a KeyDown event, but also has a "AcceptButton" property, which hooks the [Enter] keypress and can call an event handler. Check if there are event-handlers attached to either of those on the form.
I want to move ActiveControl from listview to textbox by using keys Up. but I get the problem when pressing the keys up. The first item in the listview can not be selected . if the item is selected in the second row and pressing the keys up , the focus will be on the textbox directly without passing through the first row.
i use this code:
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
if (listView1.SelectedItems[0].Selected)
{
this.ActiveControl = textBox1;
}
}
anyone have an idea of the above problems ?
Your code changes focus to textBox1 when the first selected item is selected. Something that'll be true unless there's no selected items.
This is what you want to do:
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
if (listView1.Items[0].Selected)
{
this.ActiveControl = textBox1;
}
}
}
I've 5 buttons in my windows application. When I click arrow keys the focus changing between buttons, then only
KeyUp
event firing. How to stop this?
Subscribe to the PreviewKeyDown event instead.
Occurs before the KeyDown event when a key is pressed while focus is on this control.
As you move through the buttons, the sender parameter will contain the previously selected button.
I found a solution that should work for you, adapted from here. Apparently, MS made the decision that the arrow keys wouldn't trigger the KeyDown event, so you can't cancel them.
One workaround is to specify that your arrow keys are normal input keys, like any other key. Then the KeyDown event will fire and you can cancel the button press if you want.
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
e.IsInputKey = true;
}
private void button1_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
}
You may want to read the other answers and comments in that post to see what would work best in your situation.
Answer for your question in comment
void button1_LostFocus(object sender, EventArgs e)
{
button1.Focus();
}
To prevent Up from moving focus from a Button you have to utilize at least 3 methods:
bool _focus;
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Up)
_focus = true;
}
private void button1_KeyUp(object sender, KeyEventArgs e)
{
_focus = false;
}
private void button1_Leave(object sender, EventArgs e)
{
if(_focus)
button1.Focus(); // or (sender as Control)
}
Trick is to use flag when user press Up and to return focus in Leave. You have to unflag in KeyUp, otherwise it would be impossible to change focus (by pressing Tab to example).
You could possible unflag in Leave, I didn't test it.
I need to do some logic but only from a specific textbox. Trying to do some search it appears that there is no event for KeyDown or PreviewKeyUp for a textbox but for the entire window. So in XAML I have this
PreviewKeyUp="keyPressLogic"
Then have a method that looks like this;
private void keyPressLogic(object sender, KeyEventArgs e)
{
if ((e.Key == Key.Down) && (check focus command ) )
{
//My logic
return;
}
}
As you can see I cannot figure out the check focus command. So either I am missing the key check on the textbox or got to find the focus command
thanks
To get the textbox you pressed you should:
TextBox textbox = (TextBox)sender;
and then you can:
private void keyPressLogic(object sender, KeyEventArgs e)
{
if ((e.Key == Key.Down) && (textbox.IsFocused))
{
//My logic
return;
}
}
System.Windows.Controls.TextBox has an event caleed. Here you can find it.
It will trigger the KeyDown event only for the textbox you add it on.
Here is how you add it into your XAML
<TextBox x:Name="MyTextbox" KeyDown="MyTextbox_KeyDown" />
And here is how your event handler should look like
private void MyTextbox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if(e.Key == Key.Down)
{
// Add your logic here
}
}
In Windows Form controls like listview and treeview, when someone edit the label of an item an then press the "Escape" key, the edition end but the node remains with whatever i write in it. I want in exchange that when i press the Escape key the label return to what it was. I know that i must take the label before the label edit precisely in the "BeforeLabelEdit" event. In the "KeyPress" event handler i don't know how to stop the label edition. How can i do that?
Update
i found the method that i thought that doesn't exist, but now the problem is other. The Escape key press appears to be unchatchable in the middle of an edition label action.
private void ObjectWithItems_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
if (treeViewDocXml.SelectedNode != null)
{
treeViewDocXml.SelectedNode.EndEdit(true);
}
}
}
ok, I'am not sure what you talking about, but here is example how to cancel text box editing and set text before editing started:
string textBefore;
private void textBox1_Enter(object sender, EventArgs e)
{
textBefore = textBox1.Text;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == (char)Keys.Escape)
textBox1.Text = textBefore;
}
Hope it helps.