CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2002
    Location
    ohio
    Posts
    37

    Delegate Question

    In form based callbacks, where does the eventargs param come from?

    I am implementing a dialog with eight browsing buttons that all share the same openfileDialog. I would like to have one callback function that serves all the browsing buttons and can identify who was clicked.

    ie, a method like:

    private void GenericBrowse_Click(object sender, System.EventArgs e)
    { ... // some code that only differs in who was clicked }


    where all the browsers would subscribe like:

    this.buttonBrowse1.Click += new System.EventHandler(this.GenericBrowse_Click);


    In order to do this, i think i need to derive from System.EventArgs and give the derivative a "who was clicked" attribute. How do i do this and where is it created?

  2. #2
    Join Date
    Apr 2004
    Location
    Austria
    Posts
    43
    You want many buttons to share the same function, and you want to identify what button was clicked isn't it so?

    You simply create the buttons with the windows forms designer (you can do that by code as well) and use the code you posted:

    this.buttonBrowse1.Click += new System.EventHandler(this.GenericBrowse_Click);

    in your GenericBrowser_Click you do that:

    private void GenericBrowser_Click(object sender, EventArgs e)
    {
    if (sender.GetType() == Button)
    {
    Button current = (Button)sender;
    MessageBox.Show(current.Name + " " + current.Tag);
    }
    }

    that should do.. or did I misunderstand you question?

    greetings UNI

  3. #3
    Join Date
    Sep 2002
    Location
    ohio
    Posts
    37
    That works great. Thanks!

  4. #4
    Join Date
    Apr 2004
    Location
    Austria
    Posts
    43
    I had exactly the same problem and asked here for help.. just a few days before you showed up ..

    greetings UNI

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