Form for data input : Form Frame Window « GUI Windows Form « C# / C Sharp






Form for data input

Form for data input
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Employee
{
  /// <summary>
  /// Summary description for Form1.
  /// </summary>
  public class EmployeeForm1 : System.Windows.Forms.Form
  {
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public EmployeeForm1()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.listBox1 = new System.Windows.Forms.ListBox();
      this.SuspendLayout();
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(48, 240);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(80, 24);
      this.button1.TabIndex = 2;
      this.button1.Text = "Add Item";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(160, 240);
      this.button2.Name = "button2";
      this.button2.Size = new System.Drawing.Size(96, 24);
      this.button2.TabIndex = 3;
      this.button2.Text = "Cancel";
      this.button2.Click += new System.EventHandler(this.button2_Click);
      // 
      // listBox1
      // 
      this.listBox1.Location = new System.Drawing.Point(38, 32);
      this.listBox1.Name = "listBox1";
      this.listBox1.Size = new System.Drawing.Size(216, 186);
      this.listBox1.TabIndex = 0;
      this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_OnDoubleClick);
      // 
      // Form1
      // 
      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.listBox1,
                                      this.button2,
                                      this.button1});
      this.Name = "Form1";
      this.Text = "Form1";
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new EmployeeForm1());
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
      EMPLOYEE emp = new EMPLOYEE();
      EmpForm eform = new EmpForm (emp);
      if (eform.ShowDialog (this) == DialogResult.Cancel)
        return;
      eform.GetEmployeeData (out emp);
      listBox1.Items.Add (emp);
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
      Application.Exit ();
    }

    private void listBox1_OnDoubleClick(object sender, System.EventArgs e)
    {
      int index = listBox1.SelectedIndex;
      if (index < 0)
        return;
      EMPLOYEE emp = (EMPLOYEE) listBox1.Items[index];
      EmpForm eform = new EmpForm (emp);
      if (eform.ShowDialog (this) == DialogResult.Cancel)
        return;
      eform.GetEmployeeData (out emp);
      listBox1.Items.RemoveAt (index);
      listBox1.Items.Insert (index,emp);
    }
  }
  public struct EMPLOYEE
  {
    public string  FirstName;
    public string  LastName;
    public string  Address;
    public string  City;
    public string  State;
    public string  Zip;
    public override string ToString ()
    {
      return (LastName + ", " + FirstName);
    }
  }
  /// <summary>
  /// Summary description for EmpForm.
  /// </summary>
  public class EmpForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.TextBox textBox4;
    private System.Windows.Forms.TextBox textBox5;
    private System.Windows.Forms.TextBox textBox6;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.Label label6;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public EmpForm(EMPLOYEE emp)
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
      textBox1.Text = emp.FirstName;
      textBox2.Text = emp.LastName;
      textBox3.Text = emp.Address;
      textBox4.Text = emp.City;
      textBox5.Text = emp.State;
      textBox6.Text = emp.Zip;
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if(components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.textBox4 = new System.Windows.Forms.TextBox();
      this.label1 = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.label3 = new System.Windows.Forms.Label();
      this.textBox5 = new System.Windows.Forms.TextBox();
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.label5 = new System.Windows.Forms.Label();
      this.label6 = new System.Windows.Forms.Label();
      this.textBox2 = new System.Windows.Forms.TextBox();
      this.textBox3 = new System.Windows.Forms.TextBox();
      this.label4 = new System.Windows.Forms.Label();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.textBox6 = new System.Windows.Forms.TextBox();
      this.SuspendLayout();
      // 
      // textBox4
      // 
      this.textBox4.Location = new System.Drawing.Point(16, 182);
      this.textBox4.Name = "textBox4";
      this.textBox4.Size = new System.Drawing.Size(120, 20);
      this.textBox4.TabIndex = 3;
      this.textBox4.Text = "";
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(16, 16);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(136, 16);
      this.label1.TabIndex = 8;
      this.label1.Text = "First Name";
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(16, 64);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(128, 16);
      this.label2.TabIndex = 9;
      this.label2.Text = "Last Name";
      // 
      // label3
      // 
      this.label3.Location = new System.Drawing.Point(16, 109);
      this.label3.Name = "label3";
      this.label3.Size = new System.Drawing.Size(120, 19);
      this.label3.TabIndex = 10;
      this.label3.Text = "Address";
      // 
      // textBox5
      // 
      this.textBox5.Location = new System.Drawing.Point(168, 184);
      this.textBox5.Name = "textBox5";
      this.textBox5.Size = new System.Drawing.Size(32, 20);
      this.textBox5.TabIndex = 4;
      this.textBox5.Text = "";
      // 
      // button1
      // 
      this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;
      this.button1.Location = new System.Drawing.Point(48, 224);
      this.button1.Name = "button1";
      this.button1.TabIndex = 6;
      this.button1.Text = "OK";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // button2
      // 
      this.button2.CausesValidation = false;
      this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
      this.button2.Location = new System.Drawing.Point(160, 224);
      this.button2.Name = "button2";
      this.button2.TabIndex = 7;
      this.button2.Text = "Cancel";
      // 
      // label5
      // 
      this.label5.Location = new System.Drawing.Point(168, 163);
      this.label5.Name = "label5";
      this.label5.Size = new System.Drawing.Size(40, 16);
      this.label5.TabIndex = 12;
      this.label5.Text = "State";
      // 
      // label6
      // 
      this.label6.Location = new System.Drawing.Point(224, 163);
      this.label6.Name = "label6";
      this.label6.Size = new System.Drawing.Size(48, 16);
      this.label6.TabIndex = 13;
      this.label6.Text = "Zip";
      // 
      // textBox2
      // 
      this.textBox2.Location = new System.Drawing.Point(16, 82);
      this.textBox2.Name = "textBox2";
      this.textBox2.Size = new System.Drawing.Size(208, 20);
      this.textBox2.TabIndex = 1;
      this.textBox2.Text = "";
      // 
      // textBox3
      // 
      this.textBox3.Location = new System.Drawing.Point(16, 132);
      this.textBox3.Name = "textBox3";
      this.textBox3.Size = new System.Drawing.Size(208, 20);
      this.textBox3.TabIndex = 2;
      this.textBox3.Text = "";
      // 
      // label4
      // 
      this.label4.Location = new System.Drawing.Point(16, 163);
      this.label4.Name = "label4";
      this.label4.Size = new System.Drawing.Size(120, 16);
      this.label4.TabIndex = 11;
      this.label4.Text = "City";
      // 
      // textBox1
      // 
      this.textBox1.Location = new System.Drawing.Point(16, 32);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(160, 20);
      this.textBox1.TabIndex = 0;
      this.textBox1.Text = "";
      // 
      // textBox6
      // 
      this.textBox6.Location = new System.Drawing.Point(224, 184);
      this.textBox6.Name = "textBox6";
      this.textBox6.Size = new System.Drawing.Size(48, 20);
      this.textBox6.TabIndex = 5;
      this.textBox6.Text = "";
      // 
      // EmpForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.CancelButton = this.button2;
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.button2,
                                      this.button1,
                                      this.label6,
                                      this.label5,
                                      this.label4,
                                      this.label3,
                                      this.label2,
                                      this.label1,
                                      this.textBox6,
                                      this.textBox5,
                                      this.textBox4,
                                      this.textBox3,
                                      this.textBox2,
                                      this.textBox1});
      this.MinimizeBox = false;
      this.Name = "EmpForm";
      this.ShowInTaskbar = false;
      this.Text = "EmpForm";
      this.ResumeLayout(false);

    }
    #endregion

    public void GetEmployeeData (out EMPLOYEE emp)
    {
      emp.FirstName = textBox1.Text;
      emp.LastName = textBox2.Text;
      emp.Address = textBox3.Text;
      emp.City = textBox4.Text;
      emp.State = textBox5.Text;
      emp.Zip = textBox6.Text;
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
      this.Close ();
    }
  }
  
}


           
       








Related examples in the same category

1.Print out Form size and position related information
2.Use Font from Form to paint string on a form
3.Set ResizeRedraw property
4.Set ClientSize to change the form window size
5.FormStartPosition.CenterScreen
6.Form hideForm hide
7.Change Form window ownershipChange Form window ownership
8.Create Graphics Object from form window handleCreate Graphics Object from form window handle
9.Center form windowCenter form window
10.Assign Form window default valueAssign Form window default value
11.Change the background and text colors of a form using Color DialogChange the background and text colors of a form using Color Dialog
12.Draw image based on the window sizeDraw image based on the window size
13.Auto scroll form windowAuto scroll form window
14.Set Form window title and center the Form window on the desktopSet Form window title and center the Form window on the desktop
15.Add control to a form windowAdd control to a form window
16.Change Form window backgroundChange Form window background
17.Simplest form code: set window titleSimplest form code: set window title
18.Login fromLogin from
19.makes a new window out of a graphics path that has been traced on this forms spacemakes a new window out of a graphics path that has been traced on this forms space
20.Windows Forms Getting StartedWindows Forms Getting Started
21.A form-based Windows SkeletonA form-based Windows Skeleton
22.Simple formSimple form
23.Add controls to a formAdd controls to a form
24.Demonstrates creating a form in a console programDemonstrates creating a form in a console program
25.AutoScrollPosition