Enter button automatically clicks button without being initialized (C#/Visual Studio) - c#

My program compiles and runs fine as long as you only use the mouse to navigate. I noticed that when I hit "Enter" it automatically registers as clicking one of my buttons in the window. I have started playing around with the "AcceptButton" property and setting it to appropriate buttons or even to "None." Nothing seems to work and it stays with it's default button it seems to has tied to "Enter." I have noticed that the buttons it's going to are the first I have defined in the code.
Long story short, I want to remove the "default" value for the Enter key to what the "AcceptButton" property actually specifies it to be.
Thanks,
Andy

you could capture the onKeyDown event and not handle it if it is enter
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.YOURBUTTON.PerformClick();
}
}

Assuming the WinForm has a TextBox, set the TextBox.TabIndex to 0. Again, making the assumption that this TextBox should be the first UI Element the user interacts with.
Then, change all the buttons to have a TabIndex > 0.
Finally, update the Form.AcceptButton to be the button you want to have for the default Accept/Enter.
If there is not a TextBox or some other element that can have a lower TabIndex, then the button will be the default UI Element with focus when the form is loaded up.

Related

On leaving a control, how can I give that control focus again?

I've got TextBoxes in a C# form. The user enters data, and then when they leave the control (almost always by hitting Tab), I check the data to make sure it's valid. If it is invalid, I want to highlight their text so they can immediately fix it rather than having to click it.
Right now, on Control.Leave, I validate their entry. This works just fine. However, since they hit Tab, right after they dismiss the error message, it goes on to the next object, even though I've got ((TextBox)sender).Focus();
How can I have the above line fire after the form Tabs to the next control.
You may want to look into Control.CausesValidation property
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.causesvalidation(v=vs.110).aspx
You can validate the control prior to the user leaving focus rather than waiting on Focus moving itself.
And here's MSDN documentation for Control.Validating event, does a good job at laying out the sequence of events when gaining / losing focus of a Control.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating(v=vs.110).aspx
Notice how Control.Validating and Control.Validated are launched prior to Control.LostFocus. You can perform your validation step prior to allowing the user to lose focus of your Textbox.
There's also a pretty good previous answer on stackoverflow.com which outlines how to do this: C# Validating input for textbox on winforms
If you handle the Control.Validating event, setting e.Cancel to true will stop the change of focus from occurring.
Note that this method will also stop buttons from working, so you may need to set Control.CausesValidation to false on certain buttons.
You will also need the following snippet on the main form to allow the close button to work:
protected override void OnFormClosing(FormClosingEventArgs e) {
e.Cancel = false;
base.OnFormClosing(e);
}
Try using the LostFocus event on the TextBox to Focus it again

How to change default behaviour that a textbox loses focus on back button pressed

The default behaviour of pressing back button when a textbox is on focus is that the virtual keyboard closes and the textbox loses focus. And the user press back key again, the window goes back to previous window.
However, I want the change this behavior. I want the window to go back to previous window directly when the back key is pressed, ignoring whether the textbox is on focus or not.
I tried the following methods,
Use HardwareButtons.BackPressed event, doesn't work (maybe it only works for Direct3D, I am not sure). The event isn't fired during back button pressed.
Use Textbox_onKeyUp, doesn't work. The event isn't fired during back button is up.
Use override void OnBackKeyPress, doesn't work. It does fire as expected during other cases, but during the situation when the textbox is from on focus to losing focus (the keyboard closes), the event isn't fired.
Use Textbox_OnLoseFocus, works fine but need a lot of condition checks because some times losing focus doesn't mean that I want to go back to previous page.
Please help. Thanks.
Finally I didn't change the behaviour, made a different design. But I still think that in that case, the Nokia's behaviour is most user-friendly.

How do I prevent a tab from rendering when selected?

I would like to be able to have a user be able run through the tabs, setting focus to each one, but only when they hit enter, the tabpage will render.
You would think that the paint event would be involved, but I don't know how to "cancel out" of it, if that would even do the job..
First, I should caution you that you're overriding the standard Windows behavior. In any property page dialog or anywhere else that uses tabs in the user interface, using the left and right arrow keys will flip through the tabs and cause them to display their contents in the tab control. You do not have to press Enter to get the selected tab page to display. Make sure that your users understand that your application is different (and that you understand the needs of your users) if you decide to go this route.
That said, you can override this behavior by handling the KeyDown event for the TabControl, detecting when one of the arrow keys has been pressed, and cancelling it. For example:
private void myTabControl_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
//Check to see if an arrow key was pressed
if ((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.Right))
{
//Cancel the keypress by indicating it was handled
e.Handled = true;
}
}
However, once you do this, there will be no way for the user to set focus to a particular tab page's tab, because once the tab gets focus, the tab page is immediately brought into view. This is handled by the parent TabControl and is unrelated to the Paint event (which is responsible for how the control gets painted, not when or why).
Of course, you can always determine if the Enter key was pressed in the same KeyDown event and activate any tab page that you wish (such as by using a counter variable that is incremented/ decremented each time the corresponding arrow key is pressed), but there will be no visible indication to the user which tab will then be brought into view. The focus rectangle will not be drawn.
Also be aware that pressing Ctrl+Tab or Ctrl+Page Up/Page Down will switch between tab pages. If this is also undesirable, you'll need to watch for and cancel these key combinations as well.Any time you start trying to override default behaviors, you're in for a lot more trouble than if you just design your application around it. If there's a particular reason you want to require the Enter key to commit tab page switching, we might be able to help you come up with an easier and better solution.
I'm not sure I understand what you are trying to accomplish, but it sounds like you can do it using the Visible property.
You should be able to set the TabPage's visibility to false when the user switches to it, and then set it to true only when you want to.

How to set focus to a control in a Windows Forms application?

In a Windows Forms application, when do I write the code to set the focus to a control both while the application is launched and subsequently after I call a function?
For instance, if I have a DropDownList, a TextBox and four buttons and I want the Focus to be set to the DropDownList, where do I write my code?
To set the focus to a particular control on application launch, I can set the tab index to that DropDown (with a minimum value, under the assumption TabStop property is set to True).
Now, if the user completes an operation (say, any of the Click Button Events) and then I update the DropDown and after that if I want to set the focus...I can do it as
MyDropDownList.Focus()
QUESTION
NB: The question is more about where, not how?
By far the simplest solution is to set the TabIndex property correctly so that your 'MyDropDownList' control has the lowest index. The next approach is to do it in the constructor. But you have to use Select(), the Focus() method cannot work yet because the control doesn't become visible until later.
Public Sub New()
InitializeComponent()
MyDropDownList.Select()
End Sub
Works in the Load event as well. Focus() starts working in the Shown event.
When the parent window is activated (that is, when it receives the "Activated" event), set the focus to the child control where you want the focus located.
private void Form_AddAppID_Activated(object sender, EventArgs e)
{
textID.Focus();
}
Note that the tab order has nothing to do with where the focus starts. Instead, the tab order is used to decide how the focus gets transferred when the user hits the tab key.

Suppress Automatic Textbox Selection on Windows Forms Form.Show()

I have an application where I am trying to mimic the "soft description" textboxes like those found for the tags and title locations on this site.
The way I've done this is essentially to create my textbox, and depending on what happens when the mouse pointer enters or leaves the control, it updates the content of the textbox to get the effect.
The problem is what when my form is first shown, the mouse cursor immediately jumps into the first textbox, which removes the title telling the user what the textbox is for.
If I turn off AcceptTab on the textbox, then everything works as expected, but the user loses the ability to tab into the textbox.
Is there a way to turn off this automatic selection of the textbox?
Could you this.Focus() on the form itself, or on some label control?
Bit late but a perfect solution is to select the form on load of form.
Adding this line to the constructor will give the expecting result.
this.Select();
But while using multi thread controls like OpenFileDialog if u want to unfocus/deselect text-box this.Select() was not working so I selected a button in the form using.
button1.Select();
The TabIndex property controls what order things will tab in, and on load, focus goes to the first control (ordered by TabIndex) that has AcceptTab as true. You can change the ordering so that the control that you want the user focus to start in is lowest (and have tabs work cycle through controls as you'd expect).
Alternatively, as Jason suggested, you could simply call Focus() on whatever control or the form itself in the FormLoad event.
I used a variant on Jason's technique. First, I created a dummy textbox with tabindex 0. That way, when the form is shown, that textbox will be selected. Next, I made the dummy textbox have zero width, so that it has no visible component.
However, once the form is loaded, I don't want the user to be able to tab over to the "nonexistant" textbox. Therefore, I added these two bits:
//These functions prevent the textboxes from being implicitly selected.
private void dummyBox_Leave(object sender, EventArgs e)
{
dummyBox.TabStop = false;
}
private void Main_Enter(object sender, EventArgs e)
{
dummyBox.TabStop = true;
dummyBox.Select();
}
Where Main is the name of my form.
Hope this helps someone.
Billy3

Categories