Define Apply Button action method in dialog class : Dialog « GUI Windows Forms « C# / CSharp Tutorial






Define Apply Button action method in dialog class
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
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; }
  }
}








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