What is the exact cause that fires the validating event? - c#

I have searched for the question that when is the validating event fired but the answer I found so far is that when the validation starts.
What I want to know is the actual firing time of the validating event.
For example, the click event is fired when a control is clicked by either mouse or keyboard and similarly leave is fired when a control is no more the current focused control.
So what is the explanation of validating event being fired?

The Validating event is fired only when the control that receives the focus has the CausesValidation property set to true. For example, if you have written code in TextBox's Validating event, and you click the OK button (CausesValidation = true) then the Validating event is raised, but if you click the Cancel button (CausesValidation = false) then the Validating event would not be able to fire.

Related

WPF: Is this behavior intended? PreviewLostKeyboardFocus and LostKeyboardFocus

I have a TextBox and I want to save the content, when the user leaves the TextBox. I planned to use PreviewLostKeyboardFocus, but it doesn't work as intended.
<TextBox PreviewLostKeyboardFocus="textBox2_PreviewLostKeyboardFocus"
LostKeyboardFocus="textBox2_LostKeyboardFocus" />
When I click on another control inside of the same application, I first get the PreviewLostKeyboardFocus event and then the LostKeyboardFocus event. But when I activate another application, the PreviewLostKeyboardFocus event simply doesn't happen. I only get LostKeyboardFocus.
This is the expected behaviour.
The PreviewLostKeyboardFocus event is not raised when you switch to another application.
The main purpose of handling the event in the first place is to prevent the keyboard focus from changing: https://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.previewlostkeyboardfocus(v=vs.110).aspx
And if the event was raised when you switch to another application, you would be able to prevent the keyboard focus from changing by handling this event and set the the Handled property of the KeyboardFocusChangedEventArgs to true and this would effectively prevent the user from being able to focus any other element on the screen while running your application.

Silverlight Textbox_LostFocus event not firing on Button_Click

I have one textbox and a Save Button in my silverlight. The validations for the textbox are in its lostFocus event. But if I enter something in the textbox and directly press the save button, the validation is not working. In other words, the lostfocus event is not raising in the button_click event. How can I do this ?
Call Focus() on the button. For instance, if your button's name is "MyButton", you may use MyButton.Focus() inside of it's click event.
I would also suggest that you simply call your textbox validation method inside of your button press event, then proceed only if all validations are successful.

About "Load" event

I know that in C# the Form.Load event occurs only before the form is displayed for the first time.
Is there any similar event handler (in C#) which occurs every time that the form is displayed?
Assuming that you're showing and hiding the form instead of destroying it.. then you can hook into the VisibleChanged event and perform some code when its Visible property is true.
It can be
Shown event - fired when the form is first shown
Load event - fired whenever the user loads the form
Activate event - fired each time the form is activated or receives the focus
VisibleChanged event - fired whenever the visibility changes

Control's Focused property has changed as of Leave handler when using mouse, but not keyboard

I am working with a TextBox, and need to fire some logic when the textbox has lost focus.
My problem is twofold:
The Leave event is firing on every keypress for some reason, meaning the logic is run with every keypress when it should not.
When using the Focused property of the Textbox as a double-check, simply exiting out if the property is still set, it now works when the user uses the mouse to leave, but not when the user tabs out.
The Focused property of the TextBox in question is False as of when its Leave event fires when the mouse is used to change focus, but it is still True when the Leave event fires due to a Tab keypress. Seriously?
I need a workaround, because the logic firing on every keystroke is causing a problem for users right now that needs to be fixed post-haste.
I created a form with a textbox on it and attached event handler to the leave event of that text box. I then typed a bunch of stuff into said textbox. The event was not raised. I hit tab, the event was raised. I then clicked back in the textbox, typed some more, and then clicked another control and the event was raised.
I'm just saying that something else is interfering with the textbox. I would look into that a little more, or post some code demonstrating the problem.

Further Details on Vallidating Event not Working

I have created a custom control that inherit the TextBox, in that control i have override validating event and in validating event i have put validation that checks for the empty field.
Now when i use that control on my winform and when i click on save button it immediate fires save event.. the validation event of custom control fires and it displays the error message but still it does not stop the save event to fire....
the save button CauseValidation Property is set to true..
i have also put (this.ValidateChildren())
i have also put CancelEventArgs ce.Cancel = true; in Custom Textbox control
but neither working to stop the save event to fires..
i only want to fire Save event if Textbox is not empty.
validating event fires, shows message for empty field and immediate fires save event..
now if you got an idea then if you have solution then please provide solution..
Validating event of the textbox is fired only when cusor leaves that textbox. If you directly click on the save button, Validating event of the textbox will not be fired.
I think, on Save button you need to provide some validation to check if textbox is empty or not.
Hope this helps..

Categories

Resources