locked
How do I tell which Button is Clicked? RRS feed

  • Question

  • My application consists of an MDI form and Child forms.  I have a method to enable and disable the Buttons on each Child form depending on the function being performed (i.e. Open, Read, Process files ).  Since the Child forms contain the same number of buttons, I wanted to create the method in a Class module and call it from whicheve form has focus. 

     

    My situation is that I can't figure out how to tell which form has called the method.  I've tried to use a string variable and even Object sender, nothing has worked.  Does anyone have an idea how I can get the method to interact with the Child form that has called it?

     

    The code below is my last attempt and as you can see, I am trying to see what form is sending the command, which isn't working.  I thought I could use   sender.button... but that obviously doesn't work.

    public void ButtonControlSet ( Object sender, bool btnOpen, bool btnProcess, bool btnClose )

    {

        MessageBox.Show ( "Sender: " + sender.ToString(),

                                         "What Sender", MessageBoxButtons.OK MessageBoxIcon.Information );

    }

     

    The call to this method in the form would look like this:

     

    ButtonControlSet ( true, false, true );

     

    Which would enable the first and third button and disable the second.

    Tuesday, March 27, 2007 10:21 PM

Answers

  • Hi,

     

    if I return to your first post:

     

     Rhubarb wrote:

    public void ButtonControlSet ( Object sender, bool btnOpen, bool btnProcess, bool btnClose )

    {

        MessageBox.Show ( "Sender: " + sender.ToString(),

                                         "What Sender", MessageBoxButtons.OK MessageBoxIcon.Information );

    }

     

    The call to this method in the form would look like this:

     

    ButtonControlSet ( true, false, true );

     

    Which would enable the first and third button and disable the second.

     

    You'd probably be calling this method like:

     

    Code Snippet

    ButtonControlSet (this, true, false, true );

     

    ... where this is the MDI child form instance you want to set buttons to?

     

    Solution #1: You're saying all your child forms have the same buttons. You could create a base form class, exposing your buttons and make your child forms inherit it:

     

    Code Snippet

    public class MyBaseForm : Form

    {

        // your buttons are already on this form...

        public bool ButtonOpenFileEnabled

        {

            get { return buttonOpenFile.Enabled; }

            set { buttonOpenFile.Enabled = value; }

        }

        // repeat this for other buttons

    }

     

    Code Snippet

    // You could do all this through the designer

    public class MyChildForm1 : MyBaseForm

    {

    }

    public class MyChildForm2 : MyBaseForm

    {

    }

    ...

     

    And your function:

     

    Code Snippet

    public void ButtonControlSetMyBaseForm sender, bool btnOpen, bool btnProcess, bool btnClose )

    {

        sender.OpenButtonEnabled = btnOpen;

        //... etc.

    }

     

     

    Solution #2:

     

    Dynamically find your buttons on a generic form. Here's the full code to your function:

      

    Code Snippet

    public void ButtonControlSet(Form sender, bool btnOpen, bool btnProcess, bool btnClose)

    {

        string nameOfButtonControl = "buttonOpenFile";

        Button button = sender.Controls[nameOfButtonControl] as Button;

        if (button == null)

        {

            // button with this name not found

            return;

        }

        button.Enabled = btnOpen;

        // repeat this for the rest of the buttons

    }

     

    Andrej

    Wednesday, March 28, 2007 2:17 PM

All replies

  • i think thr must b smthing which recognizes the sender. like i think..button id can help u..

    if u use child forms all hvng same bottons.it wil b difficult to recognize one.

    Wednesday, March 28, 2007 2:24 AM
  • I think you can pass parameter to each sub form instance when sub form call its structure method.

    and add some identifyer in the sub form, then you can foreach subforms in this.MdiChildren to recognize the one you want.

     

    Wednesday, March 28, 2007 6:28 AM
  • What I found is that Object sender returns the text on the button that I clicked.  I am beginning to think that this is not "do-able". 

    What I do now is this:

    The call to the method looks like this:

     

          ResetButtons ( false, false, true );

     

    The method called in this case is simple, but it centralizes things:

     

    private void ResetButtons ( bool btnOpen, bool btnProcess, bool btnClose )

    {

        this.buttonOpenFile.Enabled = btnOpen;

        this.buttonConvert.Enabled = btnProcess;

        this.buttonClose.Enabled = btnClose;

    }

     

    My situation is that I'm not sure how to get the name of the form to the method in the class.  As you can see above, I'm using "this.", however; when I move the method to the class, I can no longer use that and nothing I've found is recognized either.

     

    So, it looks as though this may very well be a "pipe dream" of mine. 

    Wednesday, March 28, 2007 1:11 PM
  • Hi,

     

    if I return to your first post:

     

     Rhubarb wrote:

    public void ButtonControlSet ( Object sender, bool btnOpen, bool btnProcess, bool btnClose )

    {

        MessageBox.Show ( "Sender: " + sender.ToString(),

                                         "What Sender", MessageBoxButtons.OK MessageBoxIcon.Information );

    }

     

    The call to this method in the form would look like this:

     

    ButtonControlSet ( true, false, true );

     

    Which would enable the first and third button and disable the second.

     

    You'd probably be calling this method like:

     

    Code Snippet

    ButtonControlSet (this, true, false, true );

     

    ... where this is the MDI child form instance you want to set buttons to?

     

    Solution #1: You're saying all your child forms have the same buttons. You could create a base form class, exposing your buttons and make your child forms inherit it:

     

    Code Snippet

    public class MyBaseForm : Form

    {

        // your buttons are already on this form...

        public bool ButtonOpenFileEnabled

        {

            get { return buttonOpenFile.Enabled; }

            set { buttonOpenFile.Enabled = value; }

        }

        // repeat this for other buttons

    }

     

    Code Snippet

    // You could do all this through the designer

    public class MyChildForm1 : MyBaseForm

    {

    }

    public class MyChildForm2 : MyBaseForm

    {

    }

    ...

     

    And your function:

     

    Code Snippet

    public void ButtonControlSetMyBaseForm sender, bool btnOpen, bool btnProcess, bool btnClose )

    {

        sender.OpenButtonEnabled = btnOpen;

        //... etc.

    }

     

     

    Solution #2:

     

    Dynamically find your buttons on a generic form. Here's the full code to your function:

      

    Code Snippet

    public void ButtonControlSet(Form sender, bool btnOpen, bool btnProcess, bool btnClose)

    {

        string nameOfButtonControl = "buttonOpenFile";

        Button button = sender.Controls[nameOfButtonControl] as Button;

        if (button == null)

        {

            // button with this name not found

            return;

        }

        button.Enabled = btnOpen;

        // repeat this for the rest of the buttons

    }

     

    Andrej

    Wednesday, March 28, 2007 2:17 PM
  • Hi,

    have a global (probably static in one form) variable of type form and set it to 'this'(refers to the form ,which is invoing the event).This will help u in finding out which form has invoked the event.

     

    Thanx,

    Ch.T.Gopi Kumar.

    Thursday, March 29, 2007 7:51 AM
  • Have you tried to capture the sender like this:  Button b = (Button)sender;

     

    Then you should have access to all of that button's properties by using b.propertyname.

    Thursday, March 29, 2007 7:49 PM