Silverlight Textbox_LostFocus event not firing on Button_Click - c#

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.

Related

C# Forms - Only the first of what should be two consecutive events triggers

I have a TextBox with a "Leave" event and a Button with a "Click" event. There is no code in either event handler.
If I place the cursor in the TextBox then leave it by clicking any other item, the "Leave" event triggers.
If I place the cursor anywhere except in the TextBox and click the Button, the "Click" event triggers.
However, if I click the Button with the cursor inside the TextBox, only the "Leave" event triggers.
How can I get both events to trigger in the third scenario?

What is the exact cause that fires the validating event?

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.

Winforms control LostFocus event not firing when you click the form background

I have a custom ComboBox which opens a separate control when you click on the arrow of the ComboBox. I would like to call the 'LostFocus' event handler to close the custom control when you lose focus of the ComboBox.
This works fine if you click onto another control such as a text box, however doesn't fire if you click on the background of the form.
I want to mimic the functionality of when you click off and close the dropdown of a normal combobox.
Take a bool variable at Form level and make it true during combobox enter(GotFocus) event. InLeave(LostFocus)event ofcombobox, make itfalse`.
Subscribe to Form MouseClick event and check bool variable in this event. if true , call combobox leave event here.

Stop textbox leave handler on click of exit button?

I have a form where I do some validations in a textbox and a button for closing the form. Now whenever I click the button and focus is on text box, the leave handler is called.
How can I stop textbox leave handler from executing ?
update :
The text of button in E&XIT, now whenever I press Atl + X it calls the button click event, but when I click on the button it doesn't. Why is it happening ?
Thanks.
Use the Validating event instead of the Leave event. Set the button's CausesValidation property to False.

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