I have a ComboBox control in C# Windows forms project. I have set the DropDownStyle of the control to DropDown and have also configured AutoComplete properties to the control. This is working well.
On this control, I have a requirement when I press Enter after selecting a value, it should fire a button click. For this, I handled the keyDown event and checked if (e.KeyCode==Keys.Enter) which works well.
The problem is the following -
When I start typing the AutoComplete box opens up for the control and if I select one of them from mouse, the KeyDown event is fired and the system thinks that an "Enter" key is pressed.
CODE
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("hello");
}
private void cb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1_Click(sender, e);
}
}
BEHAVIOR
I type B from keyboard and clicked on "B2" and got the alert.
Any ideas on what could be the issue? Happy to share more details if required.
Related
In AutoCompleteBox, when drop-down is opened and key is pressed (for example Enter) control's KeyDown event is not arroused, and this makes to press Enter two times. I'm trying to make small dialog box, that doesn't oblige user to press keyboard buttons lots of times, but which closes with result when user has found his variant and presses Enter only one time. How to catch this event?
AXAML:
<AutoCompleteBox
FilterMode="Contains"
KeyDown="InputBox_KeyDown"/>
C#:
private async void InputBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// Getting form result
Close();
}
}
Is there an option to force Enter to be the main key on certain view? What I want to achieve is whenever I click "Enter" key I want to invoke button which has "IsDefault" property set to true, no matter what is focused.
One possibility would be to intercept the RoutedEvent before it gets to the controls that try to react to it:
Connect to the PreviewKeyDown event as far up the visual tree as you want to catch it. React to it there accordingly if the enter key was pressed, e.g. run the same code as in your button, and then set in the eventargs Handled to true.
private void previewKeyDownHandler(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter || e.Key == Key.Return)
{
e.Handled = true;
//Do your stuff
}
}
The Preview events are part of the RoutedEvent system in WPF. The 2 mechanisms are called Tunneling and Bubbling. Here you can read more on that.
I want to select all the contents of a MaskedTextBox when the clicks (or tabs onto) the control, so they can easily replace the old content. I tried calling SelectAll() in the Enter event, but that didn't work at all.
I switched to using the GotFocus event, which works great when tabbing through controls, but doesn't work when I click on it with the mouse. I would only want to select all the contents when first entering/focusing on the control (subsequent clicks might be used to position the cursor to edit the existing text).
I added a button and tried calling SelectAll() in the button click event, but that didn't do anything either. What's going on? Is this a bug?
How can I get around this?
Steps to reproduce
Create a new Windows Form Application in .NET 4.0 in Visual
Studio 2010.
Add a TextBox, MaskedTextBox, and Button to the default form
Change the Mask property on the MaskedTextBox to "_____".
Add some event handlers:
private void maskedTextBox1_GotFocus(object sender, EventArgs e)
{
Debug.WriteLine("GotFocus");
maskedTextBox1.SelectAll();
}
private void button1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Click");
maskedTextBox1.SelectAll();
}
Run the program, entered some data into the MaskedTextBox, tab through controls back to it. It selects the contents of the MaskedTextBox.
Select the other TextBox. Try clicking on MaskedTextBox. Output shows that GotFocus event was called, but text doesn't get selected.
Try clicking on button in form. Text doesn't get selected.
Tested in Visual Studio 2010 with .NET 4.0 in a Windows Forms Application project
Why this isn't a duplicate of TextBox.SelectAll() does not work with TAB
If you notice, the title says "SelectAll doesn't work with TAB". In my case, it does work with Tab, it doesn't work with the mouse - completely opposite scenario. The answer for that question is to use the GotFocus event. I'm already using the GotFocus event, but it doesn't work. That answer does not answer this question. It is clearly not a duplicate. If I'm wrong, please explain in the comments.
Your SelectAll() is being overwritten by the default functionality of the masked textbox select. I would use the Enter event, it allows for tabbed entry or mouse click entry to the masked text box. You will most likely need to use the BeginInvoke method. Try the code below. It worked for me when I tried...
private void maskedTextBox1_Enter(object sender, EventArgs e)
{
BeginInvoke((Action) delegate { SetMaskedTextBoxSelectAll((MaskedTextBox) sender); });
}
private void SetMaskedTextBoxSelectAll(MaskedTextBox txtbox)
{
txtbox.SelectAll();
}
Executing Focus before Select All worked for me:
private void Masked_Enter(object sender, EventArgs e) {
((MaskedTextBox)sender).Focus();
((MaskedTextBox)sender).SelectAll();
}
I'm writing a Hangman program in C# and when I press a keyboard button I want the button on the form to be clicked. Where should I write this? In form1.load()?
No, you should select the Form on which you want the event to be triggered, then go to the properties pane, select the event tab and go down to KeyPress event, double click it and add some code.
Normaly something like this would do what you want, just google the KeyChar value to determine the keyPress you want to control, you can add more if statements:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
Button1.PerformClick();
}
//Other if statements if you want to keep an eye out for other keyPresses
}
[edit] I just remembered you might be also considering the shortcuts, in wich case the Button1.Text property should be marked &Button1, this way the "B" would be underlined and accept the alt+B shortcut to execute the button click event.
The & symbol is set before the letter you want for the shortcut, make sure you dont use the same letter for various buttons.
You wouldn't write the code in Form.Load() if you want it to happen in response to a keyboard event. That event occurs (and the code inside of it is executed) when your form first loads (appears on screen).
How about handling the KeyPress event and writing the code in that method, instead? Your form has one of those events, too.
Sample code:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
Button1.PerformClick();
}
The PerformClick method will generate a Click event for a Button control. You can handle that Click event in a similar way:
private void Button1_Click(object sender, EventArgs e)
{
// do something in response to the button being clicked
// ...
MessageBox.Show("Button clicked!");
}
If this event-handling stuff is confusing to you, make sure that you pick up a good book on programming in C# and/or the .NET Framework so that you learn it well. It's very important and not something to skip!
You have to enable KeyPreview property on the form, then you have to implements the KeyPress event directly on the form :
Form1.KeyPress +=new KeyPressEventHandler(Form1_KeyPress);
private void Form1_KeyPress(object sender, KeyPressEventArgs e) {
if(e.KeyCode == someKey) {
button1.performclick();
}
}
private void Form_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Left)
{
// Do your hang job
Button_Click(sender, EventArgs.Empty);
}
}
You need to subscribe to the events of interest and process them after. But before you need to read and study how to do that. It's a not a difficult issue in C#.
http://msdn.microsoft.com/en-us/library/ms171534.aspx
One time subscribed to the events create a function that you can call from button click and from keydown.
I have a Windows Forms application in C# .NET. It contains a user-drawn control that also handles the keyboard focus. If a part of the control has the focus, a focus highlight border is painted around it. When the form that contains the control is deactivated, the focus border must disappear from the control, obviously. But the control doesn't even get a notification about it. It only receives the "Leave" event when another control is focused, not another window. How can the control know about that?
When the Form+Control are loaded, the Control can subscribe to the Activate and DeActivated events of the Form.
If it is a UserControl, you have the Control.Load event to do this. For a CustomControl I would have to look it up.
Anyway, be sure to implement Dispose in your Control to unsubscribe to the events.
Just gave it a try:
private void UserControl1_FormActivate(object sender, EventArgs e)
{
label1.Text = "Acitve";
}
private void UserControl1_FormDeActivate(object sender, EventArgs e)
{
label1.Text = "InAcitve";
}
private void UserControl1_Load(object sender, EventArgs e)
{
this.ParentForm.Activated += UserControl1_FormActivate;
this.ParentForm.Deactivate += UserControl1_FormDeActivate;
}