Set DialogResult in your own dialog class : Dialog « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » Dialog 
23.52.6.Set DialogResult in your own dialog class
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(80120);
    this.btnCreate.Name = "btnCreate";
    this.btnCreate.Size = new System.Drawing.Size(10423);
    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(6472);
    this.lblReturn.Name = "lblReturn";
    this.lblReturn.Size = new System.Drawing.Size(14423);
    this.lblReturn.TabIndex = 2;
    // 
    // DialogCustomDialogResult
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(513);
    this.ClientSize = new System.Drawing.Size(292273);
    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(56152);
    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(160152);
    this.btnCancel.Name = "btnCancel";
    this.btnCancel.TabIndex = 1;
    this.btnCancel.Text = "Cancel";
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(11264);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 2;
    this.textBox1.Text = "textBox1";
    // 
    // linkLabel1
    // 
    this.linkLabel1.Location = new System.Drawing.Point(72112);
    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(513);
    this.ClientSize = new System.Drawing.Size(292273);
    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;
  }
}
23.52.Dialog
23.52.1.Call ShowDialog on Form object
23.52.2.About DialogBox
23.52.3.Define your own dialog boxDefine your own dialog box
23.52.4.Dialog Apply Event IllustrationDialog Apply Event Illustration
23.52.5.Define Apply Button action method in dialog classDefine Apply Button action method in dialog class
23.52.6.Set DialogResult in your own dialog classSet DialogResult in your own dialog class
23.52.7.Create a custom dialog with radio button groupCreate a custom dialog with radio button group
23.52.8.Dialog with two buttonsDialog with two buttons
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.