Add controls to a form : Form Frame Window « GUI Windows Form « C# / C Sharp






Add controls to a form

Add controls to a form
/*
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 AddControls
{
  /// <summary>
  /// Summary description for AddControls.
  /// </summary>
  public class AddControls : System.Windows.Forms.Form
  {
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button2;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

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

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

    /// <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.button2 = new System.Windows.Forms.Button();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.button1 = new System.Windows.Forms.Button();
      this.listBox1 = new System.Windows.Forms.ListBox();
      this.SuspendLayout();
      // 
      // 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);
      // 
      // textBox1
      // 
      this.textBox1.Location = new System.Drawing.Point(38, 200);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(216, 20);
      this.textBox1.TabIndex = 1;
      this.textBox1.Text = "";
      // 
      // 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);
      // 
      // listBox1
      // 
      this.listBox1.Location = new System.Drawing.Point(38, 32);
      this.listBox1.Name = "listBox1";
      this.listBox1.Size = new System.Drawing.Size(216, 147);
      this.listBox1.TabIndex = 0;
      // 
      // 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.button2,
                                      this.button1,
                                      this.textBox1,
                                      this.listBox1});
      this.Name = "AddControls";
      this.Text = "AddControls";
      this.ResumeLayout(false);

    }
    #endregion

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

    private void button1_Click(object sender, System.EventArgs e)
    {
      if (textBox1.Text == "")
        return;
      string strAdd = textBox1.Text;
      if (listBox1.FindString (strAdd, -1) < 0)
      {
        listBox1.Items.Add (strAdd);
        textBox1.Text = "";
        textBox1.Focus ();
        return;
      }
      MessageBox.Show ("\"" + strAdd + "\" is already in the list box", "Duplicate");
    }

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


           
       








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.Form for data inputForm for data input
24.Demonstrates creating a form in a console programDemonstrates creating a form in a console program
25.AutoScrollPosition