How to check which button was pressed
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: How to check which button was pressed

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    How to check which button was pressed

    Hi all,

    I have a Main Dialog it has 3 buttons; Install, Support and Exit
    When the Install button is pressed a License Agreement Dialog is opened using .ShowDialog();
    This Dialog has two buttons Install and Cancel.
    (The Install button is enabled using two radio buttons (accept and do not accept))

    But I use Close(); on both the Cancel and the Install button.
    So back in the main menu I need to know if the Install button was presssed or if the cancel button was hit.
    I can use a global int or bool flag, but that is not C# like.

    How can this be done.

    regards,

    Gerwin

  2. #2
    Join Date
    Jan 2010
    Posts
    1,102

    Re: How to check which button was pressed

    The License Agreement Dialog is made by you? If so, provide a public get-only property on your dialog class that would return an indicator of type DialogResult, and set it to either DialogResult.OK or DialogResult.Cancel from within the button handlers. Then you can check this property when the dialog is closed. Other dialogs available in the library use this method, and the property is usually called DialogResult itself - something like this:
    DialogResult DialogResult { get; private set; }

  3. #3
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Re: How to check which button was pressed

    Hi Great,

    Thanks for the info I only inserted the following lines:

    Code:
                DialogResult dr = new DialogResult();
                LicenseAgreement LA = new LicenseAgreement();
                dr = LA.ShowDialog();
                if (dr == DialogResult.OK)
                    MessageBox.Show("User clicked OK button");
                else if (dr == DialogResult.Cancel)
                    MessageBox.Show("User clicked Cancel button");
    thanks!

    G.


    To Admin Please remove the duplicate post!!
    Last edited by TBBW; November 30th, 2012 at 02:14 PM. Reason: Duplicate Post removal

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: How to check which button was pressed

    I am not entirely sure I follow. However, I think DialogResult might be helpful: http://www.dotnetperls.com/dialogresult If so, great! If not, push back an we'll try to help you some more.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    May 2002
    Location
    Boston
    Posts
    67

    Re: How to check which button was pressed

    Hi

    try this

    in the form designer set the click event for both buttons to the same function; in this case 'button_Click'

    Code:
    this.button6.Location = new System.Drawing.Point(243, 85);
                this.button6.Name = "button6";
                this.button6.Size = new System.Drawing.Size(85, 29);
                this.button6.TabIndex = 6;
                this.button6.Text = "button6";
                this.button6.UseVisualStyleBackColor = true;
                this.button6.Click += new System.EventHandler(this.button_Click);
                // 
                // button7
                // 
                this.button7.Location = new System.Drawing.Point(342, 84);
                this.button7.Name = "button7";
                this.button7.Size = new System.Drawing.Size(79, 29);
                this.button7.TabIndex = 7;
                this.button7.Text = "button7";
                this.button7.UseVisualStyleBackColor = true;
                this.button7.Click += new System.EventHandler(this.button_Click);
    then do this in the form

    Code:
    private void button_Click(object sender, EventArgs e)
            {
                if (sender == button6)
                    MessageBox.Show (sender.ToString());
                else if ( sender == button7 )
                    MessageBox.Show (sender.ToString());
     
            }
    Hope this helps

    Curt

  6. #6
    Join Date
    May 2002
    Location
    Boston
    Posts
    67

    Re: How to check which button was pressed

    This is probably a better way to test which object is the sender rather than ==

    if (object.ReferenceEquals(sender, button6 ) )
    MessageBox.Show (sender.ToString());
    else if (object.ReferenceEquals(sender, button7 ) )
    MessageBox.Show (sender.ToString());



    Curt

  7. #7
    Join Date
    Jan 2010
    Posts
    1,102

    Re: How to check which button was pressed

    Quote Originally Posted by curt_c View Post
    This is probably a better way to test which object is the sender rather than ==

    if (object.ReferenceEquals(sender, button6 ) )
    MessageBox.Show (sender.ToString());
    else if (object.ReferenceEquals(sender, button7 ) )
    MessageBox.Show (sender.ToString());

    Curt
    Thanks for contributing, but that can't be done as the buttons which are raising the evens are encapsulated inside a different form (a dialog), so they are not accessible (and are out of scope, since the dialog is closed). Anyway, this is an accidental duplicate thread, the question has already been resolved here, using the DialogResult enumeration - as suggested by
    BioPhysEngr
    in the post above (which is a standard way to get this kind of info from a dialog).
    Last edited by TheGreatCthulhu; December 5th, 2012 at 05:31 AM.

  8. #8
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Re: How to check which button was pressed

    Thanks all for the replies,

    First, for some strange reason there is a duplicate post, please do not reply to that one !!

    As I mentioned in the parrallel post I only use these short snip


    Code:
    DialogResult dr = new DialogResult();
    LicenseAgreement LA = new LicenseAgreement();
    dr = LA.ShowDialog();
    if (dr == DialogResult.OK)
         MessageBox.Show("User clicked OK button");
    else 
         MessageBox.Show("User clicked Cancel button");
    Works Super!!
    awaiting your comments.

    regards,

    Gerwin
    Last edited by TBBW; December 5th, 2012 at 08:10 AM. Reason: code correction

  9. #9
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: How to check which button was pressed

    [ Merged in posts from duplicate thread ]
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    198

    Re: How to check which button was pressed

    Thanks !!!!

    Regards,

    G.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center