Mega Code Archive

 
Categories / C# Tutorial / GUI Windows Forms
 

Set DialogResult in your own dialog class

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Resources;     public class DialogCustomDialogResult : System.Windows.Forms.Form {   private System.Windows.Forms.Button btnCreate;   private System.Windows.Forms.Label lblReturn;   public DialogCustomDialogResult()   {     InitializeComponent();   }   private void InitializeComponent()   {     this.btnCreate = new System.Windows.Forms.Button();     this.lblReturn = new System.Windows.Forms.Label();     this.SuspendLayout();     //      // btnCreate     //      this.btnCreate.Location = new System.Drawing.Point(80, 120);     this.btnCreate.Name = "btnCreate";     this.btnCreate.Size = new System.Drawing.Size(104, 23);     this.btnCreate.TabIndex = 1;     this.btnCreate.Text = "Create Dialog Box";     this.btnCreate.Click += new System.EventHandler(this.btnCreate_Click);     //      // lblReturn     //      this.lblReturn.Location = new System.Drawing.Point(64, 72);     this.lblReturn.Name = "lblReturn";     this.lblReturn.Size = new System.Drawing.Size(144, 23);     this.lblReturn.TabIndex = 2;     //      // DialogCustomDialogResult     //      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);     this.ClientSize = new System.Drawing.Size(292, 273);     this.Controls.AddRange(new System.Windows.Forms.Control[] {                                              this.lblReturn,                                              this.btnCreate});     this.ResumeLayout(false);   }   [STAThread]   static void Main()    {     Application.Run(new DialogCustomDialogResult());   }   private void btnCreate_Click(object sender, System.EventArgs e)   {     Form dlg = new MyDialog();     dlg.Text = "Dialog Test";     dlg.FormBorderStyle = FormBorderStyle.FixedDialog;     dlg.FormBorderStyle = FormBorderStyle.Sizable;     dlg.BackColor = Color.Azure;     dlg.ControlBox = true;     dlg.MaximizeBox = false;     dlg.MinimizeBox = false;     dlg.ShowInTaskbar = false;     dlg.Icon = new Icon("1.ICO");     dlg.Size = new Size(300,300);     dlg.StartPosition = FormStartPosition.CenterScreen;     dlg.ShowDialog();          lblReturn.Text = dlg.DialogResult.ToString();     switch (dlg.ShowDialog())     {       case DialogResult.Abort:         lblReturn.Text = "Abort, Abort";         break;       case DialogResult.Cancel:         lblReturn.Text = "You have cancelled.";         break;       case DialogResult.OK:         lblReturn.Text = "I'm OK, You're OK";         break;       default:         lblReturn.Text = "Whatever...";         break;     } } public class MyDialog : System.Windows.Forms.Form {   private System.Windows.Forms.Button btnOK;   private System.Windows.Forms.Button btnCancel;   private System.Windows.Forms.TextBox textBox1;   private System.Windows.Forms.LinkLabel linkLabel1;   public MyDialog()   {     InitializeComponent();   }   private void InitializeComponent()   {     this.btnOK = new System.Windows.Forms.Button();     this.btnCancel = new System.Windows.Forms.Button();     this.textBox1 = new System.Windows.Forms.TextBox();     this.linkLabel1 = new System.Windows.Forms.LinkLabel();     this.SuspendLayout();     //      // btnOK     //      this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;     this.btnOK.Location = new System.Drawing.Point(56, 152);     this.btnOK.Name = "btnOK";     this.btnOK.TabIndex = 0;     this.btnOK.Text = "Do It!";     //      // btnCancel     //      this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;     this.btnCancel.Location = new System.Drawing.Point(160, 152);     this.btnCancel.Name = "btnCancel";     this.btnCancel.TabIndex = 1;     this.btnCancel.Text = "Cancel";     //      // textBox1     //      this.textBox1.Location = new System.Drawing.Point(112, 64);     this.textBox1.Name = "textBox1";     this.textBox1.TabIndex = 2;     this.textBox1.Text = "textBox1";     //      // linkLabel1     //      this.linkLabel1.Location = new System.Drawing.Point(72, 112);     this.linkLabel1.Name = "linkLabel1";     this.linkLabel1.TabIndex = 3;     this.linkLabel1.TabStop = true;     this.linkLabel1.Text = "linkLabel1";     //      // MyDialog     //      this.AcceptButton = this.btnOK;     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);     this.ClientSize = new System.Drawing.Size(292, 273);     this.Controls.AddRange(new System.Windows.Forms.Control[] {                                              this.linkLabel1,                                              this.textBox1,                                              this.btnCancel,                                              this.btnOK});     this.Name = "MyDialog";     this.Text = "dlg";     this.ResumeLayout(false);   }   private void btnOK_Click(object sender, System.EventArgs e)   {     DialogResult = DialogResult.OK;   }   private void btnCancel_Click(object sender, System.EventArgs e)   {     DialogResult = DialogResult.Cancel;   } }