Define your own dialog box : 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.3.Define your own dialog box
Define your own dialog box
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class FormInvokeDialog : System.Windows.Forms.Form
{
  private System.ComponentModel.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 Disposebool disposing )
  {
    ifdisposing )
    {
      if (components != null
      {
        components.Dispose();
      }
    }
    base.Disposedisposing );
  }

  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(513);
    this.ClientSize = new System.Drawing.Size(240105);
    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.ComponentModel.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
  {
    getreturn strMessage;}
    set
    
      strMessage = value;
      txtMessage.Text = strMessage;
    }
  }


  protected override void Disposebool disposing )
  {
    ifdisposing )
    {
      if (components != null
      {
        components.Dispose();
      }
    }
    base.Disposedisposing );
  }

    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(128);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(24048);
    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(16104);
    this.btnOK.Name = "btnOK";
    this.btnOK.Size = new System.Drawing.Size(9624);
    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(152104);
    this.btnCancel.Name = "btnCancel";
    this.btnCancel.Size = new System.Drawing.Size(9624);
    this.btnCancel.TabIndex = 3;
    this.btnCancel.Text = "Cancel";
    // 
    // txtMessage
    // 
    this.txtMessage.Location = new System.Drawing.Point(1672);
    this.txtMessage.Name = "txtMessage";
    this.txtMessage.Size = new System.Drawing.Size(23220);
    this.txtMessage.TabIndex = 0;
    this.txtMessage.Text = "";
    // 
    // DialogForm
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(513);
    this.ClientSize = new System.Drawing.Size(266151);
    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;
  }
}
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.