Windows Develop Bookmark and Share   
 index > Windows Forms General > Sending an Event
 

Sending an Event

Hi folks,

I'm just curious to know how i can send an Event to my own Form? In particular, I'd like to send the Focus|Leave Event for a textBox control, even though that Event may not have actually occurred.

Zero_  Saturday, July 29, 2006 6:38 PM
Just call the event handler function. For example:
TextBox1_Leave(Me, new System.EventArgs)

Taking a wild guess why you'd want to do that: you're trying to run some validation code to check if a text box contains valid data. If that's the case, do it like this:

Public Function ValidateTextBox() As Boolean
If TextBox1.Text <> "" AndAlso Not IsNumeric(TextBox1.Text) Then
MsgBox("Please enter a valid number")
Return False
Else
Return True
End If
End Function

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
e.Cancel = ValidateTextBox()
End Sub

Now just can just call ValidateTextBox...
nobugz  Sunday, July 30, 2006 2:58 AM
A better question is what will fire this event? Do you want a timer or the user presses a button to start the process? Or is this something outside the actual program....

If it is within the program such as a button press, or a timer, one doesn't have to actually send the event, just call the OnFocus( ) method directly and place nulls/nothings into the arguments. Unless of course the arguments are used by the method then fill them in appropriately.

Note if it is in a timer and one is doing screen updates, it is better to invoke a method which will then subsequently call the OnFocus, if screen painting is done. Any such GUI operations have to occur on the main thread.
OmegaMan  Sunday, July 30, 2006 12:12 AM
Just call the event handler function. For example:
TextBox1_Leave(Me, new System.EventArgs)

Taking a wild guess why you'd want to do that: you're trying to run some validation code to check if a text box contains valid data. If that's the case, do it like this:

Public Function ValidateTextBox() As Boolean
If TextBox1.Text <> "" AndAlso Not IsNumeric(TextBox1.Text) Then
MsgBox("Please enter a valid number")
Return False
Else
Return True
End If
End Function

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
e.Cancel = ValidateTextBox()
End Sub

Now just can just call ValidateTextBox...
nobugz  Sunday, July 30, 2006 2:58 AM
Thanks guys, I just called the Focus Leave Event function for the text box using the default parameters and it works great.
Zero_  Sunday, July 30, 2006 5:54 PM

You can use google to search for other answers

Custom Search

More Threads

• Key transferred from a KeyUp to another
• How to make a window Form stay in the same location all the time?
• How do you stretch the image associated with a label control?
• displaying text file in listview C# windows form
• Cascade non-MDI Forms
• docking control in Framework 2.0
• IViewObject + SetAdvise Not Working
• Control.Invoke and control disposing: deadlock or an exception
• SplitContainer panel collection
• Using Propertygrid Collection Editor for editing Hashtables