-
November 29th, 2012, 07:21 PM
#1
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
-
November 30th, 2012, 06:05 AM
#2
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; }
-
November 30th, 2012, 02:13 PM
#3
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
-
December 1st, 2012, 04:45 PM
#4
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.
-
December 3rd, 2012, 02:44 PM
#5
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
-
December 3rd, 2012, 02:59 PM
#6
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
-
December 5th, 2012, 05:28 AM
#7
Re: How to check which button was pressed
Originally Posted by curt_c
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.
-
December 5th, 2012, 08:08 AM
#8
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
-
December 5th, 2012, 07:32 PM
#9
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.
-
December 6th, 2012, 06:26 AM
#10
Re: How to check which button was pressed
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|