Export (0) Print
Expand All

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

SUMMARY

     Determine the sender of an event (VB)

     Determine the sender of an event (C#)

REFERENCES

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)

  1. Start Microsoft Visual Studio .NET.
  2. Create a new Visual Basic Smart Device Application project named DetermineSender.
  3. In the Smart Application Wizard, select the Pocket PC platform and Windows Application project type.
  4. Add a Button control to the form. The button is named Button1 by default.
  5. Add a second Button control to the form. This button is named Button2 by default.
  6. Right-click the form, and then click View Code.
  7. 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
    
    
  8. 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
    
    
  9. 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#)

  1. Start Microsoft Visual Studio .NET.
  2. Create a new Visual C# Smart Device Application project named DetermineSender.
  3. In the Smart Application Wizard, select the Pocket PC platform and Windows Application project type.
  4. Add a Button control to the form. The button is named button1 by default.
  5. Add a second Button control to the form. This button is named button2 by default.
  6. Double click button1 to create a Click event handler
  7. 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 );
    }
    
    
  8. Hit Shift-F7 to return to the Windows Form designer.
  9. Select button2 and then in the Properties window hit the Events button to display the events for button2.
  10. Assign the button1_Click handler to the button2 Click event.

REFERENCES

The Visual Studio .NET 2003 online help.

Show:
© 2014 Microsoft