C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Ways to trigger windows form Control's event


Posted Date: 26 Jun 2009    Resource Type: Articles    Category: .NET Framework
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 7



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





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Ways to trigger windows form Control's event  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: .NET Interview Questions Part 6
Previous Resource: Time is of the essence
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use