How to handle complete change of the Textbox in Windows Forms? - c#

What event I should handle to react to the completed change of the TextBox (i.e. when the user is
finished editing the content of the TextBox)?

There are several methods that you can use: Leave event, or a manual "typing stopped" event.
The Leave method is the most straight forward way of doing it, although as the event name suggests, it only happens when they TextBox looses focus, not when the user stops typing.
The TypingStopped event is something you would need to create yourself, but the basic idea of it is a short duration timer (say 500ms, but you would need to test it), which you restart on every KeyDown event of the TextBox. The timer would fire its own event and disable itself if it ever hits the end of it's timeout.
Edit: Updated to Leave event as per Hans' recommendation.

The Leave event is generally a good one for processing user input (for validation for example) as they move on to another part of the form. Just make sure that the event fires if they go from the textbox to any other UI element on your form - you may need to force a focus on the new element.

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

C#.NET mnemonics unexpected behaviour

I have few buttons in my windows application which has mnemonics.Now if i press multiple keys the events of the button clicks gets fired as i have used mnemonics.
But the behaviour is not as expected,because second button event handler is getting executed before the first button event handler has finished its execution,and also i have written my code in such a way that in the event handler of first button i am disabling my second button,still the second button event handler is getting executed.The problem here is due to mnemonics.PLease suggest me a better way to handle mnemonics as soon as possible.
Thanks in advance.
If your logic is tied to your buttons, change that. Let your buttons merely manipulations an object that implements your logic. You can then check in this object whether the requested action is allowed (i.e. whether the previous operation has finished.
Alternatively you can just disable buttons when running and re-enable then when.finished.

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.

C# - are there any events fired right after loading a form?

I want to give the user the option to use a tutorial, the first time he uses the program. I tried adding it in the Form.Load event, but the forms shows up after the Messageboxes have popped up.
That's why I would like to know, are there any events fired right after loading a form?
If not, is there a way to perform actions right after loading?
You should try the shown event, which fires after the form is shown for the first time.
Load happens before the form is shown.
You could try using the Shown event but that might be a bit early too based on what you are doing but it does occur after the Load.
If you have any controls on the page you could trigger it off the controls GotFocus event. Just make sure to put in checks to only do it once if using the GotFocus method.
MSDN Form.Shown
MSDN Control.GotFocus
MSDN Reference to order of events
System.Windows.Forms.Control.HandleCreated
System.Windows.Forms.Control.BindingContextChanged
System.Windows.Forms.Form.Load
System.Windows.Forms.Control.VisibleChanged
System.Windows.Forms.Form.Activated
System.Windows.Forms.Form.Shown
The Shown event should do this for you.

I have to click twice to fire an event

I'm developing a windows form application in C# and (SOMETIME) every button in the form need to be clicked twice to fire an event.
Can anybody suggest a solution?
Best Regards,
BaDoOoReY
Its a win form application.
I think you have handled button_doubleClicked event instead of Button_Clicked event.
Please make sure that you handle Button_Clicked event.
If this is not the case then can you please share your code ??
You can create a puplic variable with 0 value.
When the user click the button for the first time you give the varriable value 1.
When the user click the button for the second time you give the varriable value 2.
and you make if statement to check if the varriable is equal 2, this mean that the button clicked twice, if true you can fire your event.
Also you can you use button double_cilck event.
I think roundtrip is not happening some times because of any ajax control or javascript.If you use any of these two please check this may stops events to fire server side some times.

Categories