Csharp/CSharp Tutorial/GUI Windows Forms/Dialog

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

About DialogBox

<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms;

class AboutDialogBox: Form {

    public static void Main()
    {
         AboutDialogBox dlg = new AboutDialogBox();
         dlg.ShowDialog();
    }    
    public AboutDialogBox()
    {
         Text = "About AboutBox";
  
         StartPosition   = FormStartPosition.CenterParent;
         FormBorderStyle = FormBorderStyle.FixedDialog;
         ControlBox      = false;
         MaximizeBox     = false;
         MinimizeBox     = false;
         ShowInTaskbar   = false;
  
         Label label1     = new Label();
         label1.Parent    = this;
         label1.Text      = " AboutBox Version 1.0 ";
         label1.Font      = new Font(FontFamily.GenericSerif, 24, 
                                     FontStyle.Italic);
         label1.AutoSize  = true;
         label1.TextAlign = ContentAlignment.MiddleCenter;
  
         Icon icon = new Icon(GetType(), "AboutBox.ico");
  
         PictureBox picbox = new PictureBox();
         picbox.Parent     = this;
         picbox.Image      = icon.ToBitmap();
         picbox.SizeMode   = PictureBoxSizeMode.AutoSize;
         picbox.Location   = new Point(label1.Font.Height / 2, 
                                       label1.Font.Height / 2);
  
         label1.Location  = new Point(picbox.Right,label1.Font.Height / 2);
  
         int iClientWidth = label1.Right;
  
         Label label2     = new Label();
         label2.Parent    = this;
         label2.Text      = "\x00A9 ";
         label2.Font      = new Font(FontFamily.GenericSerif, 16);
         label2.Location  = new Point(0, label1.Bottom + 
                                         label2.Font.Height);
         label2.Size      = new Size(iClientWidth, label2.Font.Height);
         label2.TextAlign = ContentAlignment.MiddleCenter;
  
         Button button   = new Button();
         button.Parent   = this;
         button.Text     = "OK";
         button.Size     = new Size(4 * button.Font.Height, 
                                    2 * button.Font.Height);
         button.Location = new Point((iClientWidth - button.Size.Width) / 2,
                                  label2.Bottom + 2 * button.Font.Height);
  
         button.DialogResult = DialogResult.OK;
  
         CancelButton = button;
         AcceptButton = button;
  
         ClientSize = new Size(iClientWidth, 
                               button.Bottom + 2 * button.Font.Height);
    }

}</source>

Call ShowDialog on Form object

<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms;

class SimplerDialog: Form {

    public static void Main()
    {
         Application.Run(new SimplerDialog());
    }
    public SimplerDialog()
    {
         Text = "Simpler Dialog";
  
         Menu = new MainMenu();
         Menu.MenuItems.Add("&Dialog!", new EventHandler(MenuOnClick));
    }
    void MenuOnClick(object obj, EventArgs ea)
    {
         SimplerDialogBox dlg = new SimplerDialogBox();
         DialogResult     dr  = dlg.ShowDialog();
  
         Console.WriteLine(dr);
    }

} class SimplerDialogBox: Form {

    public SimplerDialogBox()
    {
         Text = "Simpler Dialog Box";
  
         FormBorderStyle = FormBorderStyle.FixedDialog;
         ControlBox      = false;
         MaximizeBox     = false;
         MinimizeBox     = false;
         ShowInTaskbar   = false;
  
         Button btn = new Button();
         btn.Parent   = this;
         btn.Text     = "OK";
         btn.Location = new Point(50, 50);
         btn.Size     = new Size (10 * Font.Height, 2 * Font.Height);
         btn.DialogResult = DialogResult.OK;
  
         btn = new Button();
         btn.Parent   = this;
         btn.Text     = "Cancel";
         btn.Location = new Point(50, 100);
         btn.Size     = new Size (10 * Font.Height, 2 * Font.Height);
         btn.DialogResult = DialogResult.Cancel;
    }

}</source>

Create a custom dialog with radio button group

<source lang="csharp">using System; using System.ruponentModel; using System.Drawing; using System.Windows.Forms; using System.IO; using System.Text; class MyDialog : System.Windows.Forms.Form {

 private Button okButton;
 private Button cancelButton;
 private CheckBox checkbox;
 private GroupBox radiogroup;
 private RadioButton radio1,radio2,radio3;
 public int Radio;
 public bool Check {
   get { return checkbox.Checked; }
   set { checkbox.Checked = value;}
   }
 void OnRadio(Object sender,EventArgs e)
 {
   int n=0;
   foreach(Object o in radiogroup.Controls)
   {
     if(o is RadioButton)
     {
       RadioButton r=(RadioButton)o;
       if(r.Checked)
         Radio=n;
       n++;
     }
   }
 }
 public MyDialog()
 {
   Size = new Size(400,300);
   FormBorderStyle = FormBorderStyle.FixedDialog;
   Text = "Dialog test";
   okButton = new Button();    
   okButton.DialogResult = DialogResult.OK;
   okButton.Location = new Point(20,230);
   okButton.Size = new Size(80,25);
   okButton.Text = "OK";
   Controls.Add(okButton);
   cancelButton = new Button();
   cancelButton.Location = new Point(300,230);
   cancelButton.Size = new Size(80,25);
   cancelButton.Text = "Cancel";
   cancelButton.DialogResult = DialogResult.Cancel;
   Controls.Add(cancelButton);
   checkbox = new CheckBox();
   checkbox.Location = new Point(20,30);
   checkbox.Size = new Size(300,25);
   checkbox.Text = "CheckBox";
   Controls.Add(checkbox);
   radiogroup = new GroupBox();
   radiogroup.Text = "Radio Buttons";
   radiogroup.Location = new Point(10,60);
   radiogroup.Size = new Size(380,110);
   Controls.Add(radiogroup);
   radio1 = new RadioButton();
   radio1.Location = new Point(10,15); 
   radio1.Size = new Size(360,25);
   radio1.Click += new EventHandler(OnRadio);
   radio1.Text = "Radio Button #1";
   radiogroup.Controls.Add(radio1);
   radio2 = new RadioButton();
   radio2.Location = new Point(10,40); 
   radio2.Size = new Size(360,25);
   radio2.Click += new EventHandler(OnRadio);
   radio2.Text = "Radio Button #2";
   radiogroup.Controls.Add(radio2);
   radio3 = new RadioButton();
   radio3.Location = new Point(10,70); 
   radio3.Size = new Size(360,25);
   radio3.Click += new EventHandler(OnRadio);
   radio3.Text = "Radio Button #3";
   radiogroup.Controls.Add(radio3);
 }

} public class CustomDialogTest{

 public static void Main()
 {
   MyDialog dlg = new MyDialog();
   DialogResult r=dlg.ShowDialog();
   
     Console.WriteLine(dlg.Radio);
     Console.WriteLine(dlg.Check);
   
 }

}</source>

Define Apply Button action method in dialog class

<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class DialogApplyMethodInDialogClass : System.Windows.Forms.Form {

 private System.Windows.Forms.Button btnCreate;
 private System.Windows.Forms.Label lblReturn;
 public DialogApplyMethodInDialogClass()
 {
   InitializeComponent();
 }
 private void InitializeComponent()
 {
   this.btnCreate = new System.Windows.Forms.Button();
   this.lblReturn = new System.Windows.Forms.Label();
   this.SuspendLayout();
   this.btnCreate.Location = new System.Drawing.Point(80, 144);
   this.btnCreate.Name = "btnCreate";
   this.btnCreate.Size = new System.Drawing.Size(136, 23);
   this.btnCreate.TabIndex = 0;
   this.btnCreate.Text = "Create Dialog Box";
   this.btnCreate.Click += new System.EventHandler(this.btnCreate_Click);
   this.lblReturn.Location = new System.Drawing.Point(88, 64);
   this.lblReturn.Name = "lblReturn";
   this.lblReturn.Size = new System.Drawing.Size(152, 23);
   this.lblReturn.TabIndex = 1;
   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 DialogApplyMethodInDialogClass());
 }
 private void btnCreate_Click(object sender, System.EventArgs e)
 {
   DialogDemo dlg = new DialogDemo(this);
   dlg.EnableApplyButton = false;
   dlg.ShowDialog();
   
   if (dlg.DialogResult == DialogResult.OK)
     {lblReturn.Text = dlg.TextOut;}
   else
     {lblReturn.Text = dlg.DialogResult.ToString();}
 }
 
 public void UpdateLabel(string str)
 {
   lblReturn.Text = str;
 }

} public class DialogDemo : Form {

 private Button btnApply = new Button();
 private TextBox txt = new TextBox();
 private DialogApplyMethodInDialogClass f;
 
 public DialogDemo(DialogApplyMethodInDialogClass f)
 {
   this.f = f;
   FormBorderStyle = FormBorderStyle.FixedDialog;
   BackColor = System.Drawing.Color.Aquamarine;
   ControlBox = false;
   MaximizeBox = false;
   MinimizeBox = false;
   ShowInTaskbar = false;
   Size = new Size(400,200);
   StartPosition = FormStartPosition.CenterScreen;
   Button btnOK = new Button();
   btnOK.Text = "OK";
   btnOK.DialogResult = DialogResult.OK;
   btnOK.Location = new Point(50,50);
   btnOK.TabIndex = 0;
   Controls.Add(btnOK);
   
   btnApply.Text = "Apply";
   btnApply.Location = new Point(150,50);
   btnApply.TabIndex = 1;
   btnApply.Enabled = false;
   btnApply.Click += new EventHandler(ApplyOnClick);
   Controls.Add(btnApply);
   
   Button btnCancel = new Button();
   btnCancel.Text = "Cancel";
   btnCancel.DialogResult = DialogResult.Cancel;
   btnCancel.Location = new Point(250,50);
   btnCancel.TabIndex = 2;
   Controls.Add(btnCancel);
   
   txt.Size = new Size(100,15);
   txt.Location = new Point(150,15);
   txt.TextChanged += new EventHandler(TextBoxChanged);
   Controls.Add(txt);
 }
 private void ApplyOnClick(object sender, EventArgs e)
 {
   f.UpdateLabel(txt.Text);
   EnableApplyButton = false;
 }
 
 private void TextBoxChanged(object sender, EventArgs e)
 {
   TextBox txt = (TextBox)sender;
   DialogDemo dlg = (DialogDemo)txt.Parent;
   dlg.EnableApplyButton = true;  
 }
 public bool EnableApplyButton
 {
   get {return btnApply.Enabled; }
   set {btnApply.Enabled = value; }
 }
 
 public string TextOut
 {
   get
     {return txt.Text; }
 }

}</source>

Define your own dialog box

<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class FormInvokeDialog : System.Windows.Forms.Form {

 private System.ruponentModel.Container components = null;
 private System.Windows.Forms.MenuItem mnuModalBox;
 private System.Windows.Forms.MenuItem menuItem1;
 private System.Windows.Forms.MainMenu mainMenu1;
 
 private string dlgMsg = "Pick a menu item";
 public FormInvokeDialog()
 {
   InitializeComponent();
   CenterToScreen();
 }
 protected override void Dispose( bool disposing )
 {
   if( disposing )
   {
     if (components != null) 
     {
       components.Dispose();
     }
   }
   base.Dispose( disposing );
 }
 private void InitializeComponent()
 {
   this.menuItem1 = new System.Windows.Forms.MenuItem();
   this.mnuModalBox = new System.Windows.Forms.MenuItem();
   this.mainMenu1 = new System.Windows.Forms.MainMenu();
   // 
   // menuItem1
   // 
   this.menuItem1.Index = 0;
   this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                         this.mnuModalBox});
   this.menuItem1.Text = "Dialogs";
   // 
   // mnuModalBox
   // 
   this.mnuModalBox.Index = 0;
   this.mnuModalBox.Text = "Show Modal Box";
   this.mnuModalBox.Click += new System.EventHandler(this.mnuModalBox_Click);
   // 
   // mainMenu1
   // 
   this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                         this.menuItem1});
   // 
   // FormInvokeDialog
   // 
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(240, 105);
   this.Menu = this.mainMenu1;
   this.Name = "FormInvokeDialog";
   this.Text = "Parent Form";
   this.Resize += new System.EventHandler(this.FormInvokeDialog_Resize);
   this.Paint += new System.Windows.Forms.PaintEventHandler(this.FormInvokeDialog_Paint);
 }
 [STAThread]
 static void Main() 
 {
   Application.Run(new FormInvokeDialog());
 }
 protected void FormInvokeDialog_Resize (object sender, System.EventArgs e)
 {
   Invalidate();
 }
 protected void FormInvokeDialog_Paint (object sender, System.Windows.Forms.PaintEventArgs e)
 {
   Graphics g = e.Graphics;
   g.DrawString(dlgMsg, new Font("times New Roman", 24),Brushes.Blue, this.ClientRectangle);
 }
 protected void mnuModalBox_Click (object sender, System.EventArgs e)
 {
       DialogForm myForm = new DialogForm();
   myForm.Message = dlgMsg;
   myForm.ShowDialog(this);
   if(myForm.DialogResult == DialogResult.OK)
   {
     dlgMsg = myForm.Message;
     Invalidate();
   }
 }

} public class DialogForm : System.Windows.Forms.Form {

   private System.ruponentModel.Container components = null;
 private System.Windows.Forms.Button btnCancel;
 private System.Windows.Forms.Button btnOK;
 private System.Windows.Forms.Label label1;
 private System.Windows.Forms.TextBox txtMessage;
   public DialogForm()
   {
       InitializeComponent();
   this.StartPosition = FormStartPosition.CenterParent;      
   }
 private string strMessage;
 public string Message
 {
   get{ return strMessage;}
   set
   { 
     strMessage = value;
     txtMessage.Text = strMessage;
   }
 }
 protected override void Dispose( bool disposing )
 {
   if( disposing )
   {
     if (components != null) 
     {
       components.Dispose();
     }
   }
   base.Dispose( disposing );
 }
   private void InitializeComponent()
 {
   this.label1 = new System.Windows.Forms.Label();
   this.btnOK = new System.Windows.Forms.Button();
   this.btnCancel = new System.Windows.Forms.Button();
   this.txtMessage = new System.Windows.Forms.TextBox();
   this.SuspendLayout();
   // 
   // label1
   // 
   this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold);
   this.label1.Location = new System.Drawing.Point(12, 8);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(240, 48);
   this.label1.TabIndex = 1;
   this.label1.Text = "Type in your message, and the main window will use it...";
   // 
   // btnOK
   // 
   this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
   this.btnOK.Location = new System.Drawing.Point(16, 104);
   this.btnOK.Name = "btnOK";
   this.btnOK.Size = new System.Drawing.Size(96, 24);
   this.btnOK.TabIndex = 2;
   this.btnOK.Text = "OK";
   this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
   // 
   // btnCancel
   // 
   this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
   this.btnCancel.Location = new System.Drawing.Point(152, 104);
   this.btnCancel.Name = "btnCancel";
   this.btnCancel.Size = new System.Drawing.Size(96, 24);
   this.btnCancel.TabIndex = 3;
   this.btnCancel.Text = "Cancel";
   // 
   // txtMessage
   // 
   this.txtMessage.Location = new System.Drawing.Point(16, 72);
   this.txtMessage.Name = "txtMessage";
   this.txtMessage.Size = new System.Drawing.Size(232, 20);
   this.txtMessage.TabIndex = 0;
   this.txtMessage.Text = "";
   // 
   // DialogForm
   // 
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(266, 151);
   this.ControlBox = false;
   this.Controls.Add(this.btnCancel);
   this.Controls.Add(this.btnOK);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.txtMessage);
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
   this.MaximizeBox = false;
   this.MinimizeBox = false;
   this.Name = "DialogForm";
   this.Text = "Some Custom Dialog";
   this.ResumeLayout(false);
 }
 protected void btnOK_Click (object sender, System.EventArgs e)
 {
   strMessage = txtMessage.Text;
 }

}</source>

Dialog Apply Event Illustration

<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class DialogApplyEvent : System.Windows.Forms.Form {

 private System.Windows.Forms.Button btnCreate;
 private System.Windows.Forms.Label lblReturn;
 public DialogApplyEvent()
 {
   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(72, 176);
   this.btnCreate.Name = "btnCreate";
   this.btnCreate.Size = new System.Drawing.Size(144, 23);
   this.btnCreate.TabIndex = 0;
   this.btnCreate.Text = "Create Dialog Box";
   this.btnCreate.Click += new System.EventHandler(this.btnCreate_Click);
   // 
   // lblReturn
   // 
   this.lblReturn.Location = new System.Drawing.Point(88, 88);
   this.lblReturn.Name = "lblReturn";
   this.lblReturn.TabIndex = 1;
   // 
   // DialogApplyEvent
   // 
   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 DialogApplyEvent());
 }
 private void btnCreate_Click(object sender, System.EventArgs e)
 {
   DialogDemo dlg = new DialogDemo();
   dlg.EnableApplyButton = false;
   dlg.ClickApply += new EventHandler(DialogDemoOnApply);
   
   dlg.ShowDialog();
   
   if (dlg.DialogResult == DialogResult.OK)
     {lblReturn.Text = dlg.TextOut;}
   else
     {lblReturn.Text = dlg.DialogResult.ToString();}
 }
 
 private void DialogDemoOnApply(object sender, System.EventArgs e)
 {
   DialogDemo dlg = (DialogDemo)sender;
   lblReturn.Text = dlg.TextOut;
   dlg.EnableApplyButton = false;  
 }

} class DialogDemo : Form {

 private Button btnApply;
 private TextBox txt;
 public event EventHandler ClickApply;
 
 public DialogDemo()
 {
   FormBorderStyle = FormBorderStyle.FixedDialog;
   BackColor = System.Drawing.Color.Aquamarine;
   ControlBox = false;
   MaximizeBox = false;
   MinimizeBox = false;
   ShowInTaskbar = false;
   Size = new Size(400,200);
   StartPosition = FormStartPosition.CenterScreen;
   Button btnOK = new Button();
   btnOK.Text = "OK";
   btnOK.DialogResult = DialogResult.OK;
   btnOK.Location = new Point(50,50);
   btnOK.TabIndex = 0;
   btnOK.Click += new EventHandler(ApplyButtonOnClick);
   Controls.Add(btnOK);
   
   btnApply = new Button();
   btnApply.Text = "Apply";
   btnApply.Location = new Point(150,50);
   btnApply.TabIndex = 1;
   btnApply.Enabled = false;
   btnApply.Click += new EventHandler(ApplyButtonOnClick);
   Controls.Add(btnApply);
   
   Button btnCancel = new Button();
   btnCancel.Text = "Cancel";
   btnCancel.DialogResult = DialogResult.Cancel;
   btnCancel.Location = new Point(250,50);
   btnCancel.TabIndex = 2;
   Controls.Add(btnCancel);
   txt = new TextBox();
   txt.Size = new Size(100,15);
   txt.Location = new Point(150,15);
   txt.TextChanged += new EventHandler(TextBoxChanged);
   Controls.Add(txt);
   
 }
 
 private void TextBoxChanged(object sender, EventArgs e)
 {
   this.EnableApplyButton = true;  
 }
 public bool EnableApplyButton
 {
   get {return btnApply.Enabled; }
   set {btnApply.Enabled = value; }
 }
 
 public string TextOut
 {
   get {return txt.Text; }
 }
 
 private void ApplyButtonOnClick (object sender, System.EventArgs e)
 {
   if (ClickApply != null)
     ClickApply(this, new EventArgs());
 }

}</source>

Dialog with two buttons

<source lang="csharp">using System; using System.Drawing; using System.ruponentModel; using System.Windows.Forms; public class SimpleDialog : Form {

 public SimpleDialog()
 {
   Button OkButton=new Button();
   OkButton.Text = "Ok";
   OkButton.DialogResult = DialogResult.OK;
   OkButton.Location = new Point(8,20);
   OkButton.Size = new Size(50,24);
   this.Controls.Add(OkButton);
   Button CancelButton=new Button();
   CancelButton.Text = "Cancel";
   CancelButton.DialogResult = DialogResult.Cancel;
   CancelButton.Location = new Point(64,20);
   CancelButton.Size = new Size(50,24);
   this.Controls.Add(CancelButton);
   this.Text="Dialog";
   this.Size = new Size(130,90);
   this.FormBorderStyle = FormBorderStyle.FixedDialog;
   this.StartPosition = FormStartPosition.CenterParent;
   this.ControlBox = false;
 }

}

public class SimpleDialogTest {

 public static void Main(){
   SimpleDialog dlg = new SimpleDialog();
   if(dlg.ShowDialog() == DialogResult.OK)
     MessageBox.Show("You clicked Ok");
   else
     MessageBox.Show("You clicked Cancel");
 }

}</source>

Set DialogResult in your own dialog class

<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; 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;
 }

}</source>