HOW TO: Determine the Sender of an Event Without Using the Control.Name Property
.NET Compact Framework 1.0
Applies to:
Microsoft® .NET Compact Framework 1.0
Microsoft® Visual Studio® .NET 2003
IN THIS TASK
Determine the sender of an event (VB)
Determine the sender of an event (C#)
SUMMARY
Learn how to determine the sender of an event given that the .NET Compact Framework controls do not support the name property.
Determine the sender of an event (VB)
- Start Microsoft Visual Studio .NET.
- Create a new Visual Basic Smart Device Application project named DetermineSender.
- In the Smart Application Wizard, select the Pocket PC platform and Windows Application project type.
- Add a Button control to the form. The button is named Button1 by default.
- Add a second Button control to the form. This button is named Button2 by default.
- Right-click the form, and then click View Code.
- Add the following Click event handler for Button1:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click End Sub
- Add the Button2.Click event to the event handlers declaration:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click End Sub
- Add handler code to the Sub Button1_Click:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click Dim s As String If sender Is Button1 Then s = "button1" ElseIf sender Is Button2 Then s = "button2" End If MessageBox.Show("You pressed: " + s) End Sub
Determine the sender of an event (C#)
- Start Microsoft Visual Studio .NET.
- Create a new Visual C# Smart Device Application project named DetermineSender.
- In the Smart Application Wizard, select the Pocket PC platform and Windows Application project type.
- Add a Button control to the form. The button is named button1 by default.
- Add a second Button control to the form. This button is named button2 by default.
- Double click button1 to create a Click event handler
- Add the following code to the event handler:
private void button1_Click(object sender, System.EventArgs e) { string s = "unknown button"; if( sender == button1 ) { s = "button1"; } else if( sender == button2 ) { s = "button2"; } MessageBox.Show( "You pressed: " + s ); } - Hit Shift-F7 to return to the Windows Form designer.
- Select button2 and then in the Properties window hit the Events button to display the events for button2.
- Assign the button1_Click handler to the button2 Click event.
REFERENCES
The Visual Studio .NET 2003 online help.