Ways to trigger windows form Control's event

Posted by VIJI Sunday, July 5, 2009

In this article, we can see the various ways of triggering the Control's Event.

Some times we may want to perform the same functionality as a button control does, from another control, say another button or Checkbox etc.

In such scenarios, it will be useful to trigger the event handler from other controls.

There are Three ways to trigger Conrols Event


1. Perform the event by the Assigned Control itself.

2. Call the Event Handler by passing Null as parameters For both Sender and Args Parameter.

3. Calling the Event Handler by passing Control Name as parameters For Sender and Null For Args Parameter.


To illustrate this, let us consider the following Form and Controls.

a) A Form, and name it as Myform.

b) a button and name it as Button1

c) a button and name it as Button2

d) a ComboBox and name it as ComboBox1


and we will see how the Button1's Click Event Handler is going to be handled by all other controls and Button1.



Public Sub New()

'Assigning text property of the controls

me.Button1.Text = "I am Button 1"

me.Button2.Text = "I am Button 2"

me.ComboBox1.Items.Add ("1")
me.ComboBox1.Items.Add ("2")
me.ComboBox1.Items.Add ("3")


End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click

Dim myComboBox As ComboBox
Dim myButton As Button

'code to perform action when Button is clicked

If Sender Is Nothing Then

MessageBox.Show (Button1.Text)

End If

If TypeOf sender Is Button Then

myButton = CType(sender, Button)

'Just to display the text of the control as an illustration

MessageBox.Show (myButton.Text)

ElseIf TypeOf sender Is ComboBox Then

myComboBox = CType(Ctrl, ComboBox)

MessageBox.Show (myComboBox.SelectedItem.ToString)

End If


End Sub





Perform the event by the Assigned Control itself.(Button1)

In this case,

a. we can just click on the Button1. The Event handler will be invoked.

The Text "I am Button1" will be displayed

b. Call Button1's PerformClick() Method

Button1.PerformClick()

It will also invoke Button1's Event handler


passing Null as parameters For both Sender and Args Parameter.


1. Triggering the Button1's Click Event using Button2's Click

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click

'Calling Button1's Click Event by passing Sender as Button2 And
'Args As Nothing

Button1_Click (sender, Nothing)

End Sub


2. Triggering the Button1's Click Event using ComboBox1's selectedIndexChanged

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

Button1_Click (sender, Nothing)

End Sub



Calling the Event Handler by passing Control Name as parameters For Sender and Null For Args Parameter.


1. Triggering the Button1's Click Event using Button2's Click

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click

'Calling Button1's Click Event by passing Sender And
'Args As Nothing

Button1_Click (Nothing, Nothing)

End Sub


2. Triggering the Button1's Click Event using ComboBox1's selectedIndexChanged

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

'Calling Button1's Click Event by passing Sender And
'Args As Nothing

Button1_Click (Nothing, Nothing)


End Sub

0 Responses to Ways to trigger windows form Control's event

Post a Comment