CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: sender

Threaded View

  1. #3
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: sender

    sender is object that triggered the event.

    for example you can have only one event handler for more than one controls.

    consider this example:

    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
                button1.Click += new EventHandler(Btn_click);
                button2.Click += new EventHandler(Btn_click);
            }
    
            private void Btn_click(object sender, EventArgs e)
            {
                Button btn = (Button)sender;
    
                MessageBox.Show(btn.Text);
            
            }
    Explanation: before running the above code add two button control to your main form and let the name properties of them be : button1 and button2.

    then in form load event i attached single event handler function (Btn_click) to both of their click events.

    if button1 trigger the event MessageBox will show its text property that is button1; if button2 trigger it MessageBox will show text property of second button.

    Notice that sender is type of Object so i casted it to Button to extract the text property.
    Last edited by toraj58; December 20th, 2008 at 07:07 AM.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured