Mik

How can I check which button has been clicked in a Dialog window

I tried ToString(), but it returns "Cancel" no matter which button has been clicked (there are two buttons, "Cancel" and "OK").

The code I am working on is not mine so perhaps there's something I don't know, maybe I should set some property of a Button to "OK" or something and it would work



Re: Windows Forms General DialogResult

TilakGopi

Otherwise you can have a public property indicating the button that is pressed to close the dialog,set on button_click handlers in the Dialog class.

Thanks,

Ch.T.Gopi Kumar.






Re: Windows Forms General DialogResult

Ðãv? S. Â???????

You simply need to do:

DialogResult result = myform.ShowDialog();

if (result == Windows.Forms.DialogResult.OK)
{
// some code
}
else of (result == Windows.Forms.DialogResult.Cancel)
{
// some code
}





Re: Windows Forms General DialogResult

nobugz

Check the properties of the dialog form. AcceptButton should be set to the OK button, CancelButton should be set to the Cancel button. The DialogResult property could also be assigned in code so double-check that.





Re: Windows Forms General DialogResult

decyclone

Hi Mik,

Send in some sample code of your scenario, so everyone can see where the problem is. And which object's ToString() method are you using that always returns "Cancel"

Here is a sample code :

class Form4 : Form
{
private Button button2;
private Button button1;

public Form4()
{
InitializeComponent();
}

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "OK";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button2.Location = new System.Drawing.Point(131, 12);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Cancel";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form4
//
this.AcceptButton = this.button1;
this.CancelButton = this.button2;
this.ClientSize = new System.Drawing.Size(218, 46);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form4";
this.ResumeLayout(false);

}

private Button mClickedButton;
public Button ClickedButton
{
get { return mClickedButton; }
set { mClickedButton = value; }
}

private void button1_Click(object sender, EventArgs e)
{
ClickedButton = sender as Button;
Close();
}

private void button2_Click(object sender, EventArgs e)
{
ClickedButton = sender as Button;
Close();
}

static void Main(string[] args)
{
Form4 f = new Form4();

Application.Run(f);

if (f.DialogResult == DialogResult.OK)
{
MessageBox.Show("OK. : " + f.ClickedButton.Name);
}
else if (f.DialogResult == DialogResult.Cancel)
{
MessageBox.Show("Cancel. : " + f.ClickedButton.Name);
}
}
}





Re: Windows Forms General DialogResult

Mik

Thanks., this is the line I've been missing:

this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;

Best regards, Mik