Form with list, button : ListBox « GUI Windows Form « C# / C Sharp






Form with list, button

Form with list, button
/*
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 Modeless
{
  /// <summary>
  /// Summary description for MainForm.
  /// </summary>
  public class MainFormDemo : System.Windows.Forms.Form
  {
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button3;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    AddItemForm frmAdd;

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

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
      frmAdd = new AddItemForm (this);

    }

    /// <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.listBox1 = new System.Windows.Forms.ListBox();
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.button3 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // listBox1
      // 
      this.listBox1.Location = new System.Drawing.Point(42, 16);
      this.listBox1.Name = "listBox1";
      this.listBox1.Size = new System.Drawing.Size(208, 199);
      this.listBox1.TabIndex = 0;
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(17, 240);
      this.button1.Name = "button1";
      this.button1.TabIndex = 1;
      this.button1.Text = "Add Item";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(109, 240);
      this.button2.Name = "button2";
      this.button2.TabIndex = 2;
      this.button2.Text = "Delete Item";
      this.button2.Click += new System.EventHandler(this.button2_Click);
      // 
      // button3
      // 
      this.button3.DialogResult = System.Windows.Forms.DialogResult.Cancel;
      this.button3.Location = new System.Drawing.Point(201, 240);
      this.button3.Name = "button3";
      this.button3.TabIndex = 3;
      this.button3.Text = "Close";
      this.button3.Click += new System.EventHandler(this.button3_Click);
      // 
      // MainForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.CancelButton = this.button3;
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.button3,
                                      this.button2,
                                      this.button1,
                                      this.listBox1});
      this.Name = "MainForm";
      this.Text = "MainForm";
      this.ResumeLayout(false);

    }
    #endregion

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


    private void button1_Click(object sender, System.EventArgs e)
    {
      if (frmAdd.IsDisposed == true)
        frmAdd = new AddItemForm (this);
      frmAdd.Show ();
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
      if (listBox1.SelectedIndex < 0)
        return;
      object obj = listBox1.Items[listBox1.SelectedIndex];
      listBox1.Items.Remove (obj);
    }

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

    public string AddItemToList (string strAdd)
    {
      if (strAdd == "")
        return ("");
      if (listBox1.FindString (strAdd, -1) < 0)
      {
        listBox1.Items.Add (strAdd);
        return ("");
      }
      MessageBox.Show ("\"" + strAdd + "\" is already in the list box", "Duplicate");
      return (strAdd);
    }
  }
  /// <summary>
  /// Summary description for AddItemForm.
  /// </summary>
  public class AddItemForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
    MainFormDemo parent;
    public AddItemForm(MainFormDemo parent)
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
      this.parent = parent;
    }

    /// <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.textBox1 = new System.Windows.Forms.TextBox();
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // textBox1
      // 
      this.textBox1.Location = new System.Drawing.Point(28, 8);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(224, 20);
      this.textBox1.TabIndex = 1;
      this.textBox1.Text = "";
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(40, 48);
      this.button1.Name = "button1";
      this.button1.TabIndex = 2;
      this.button1.Text = "Add";
      this.button1.Click += new System.EventHandler(this.button1_Click_1);
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(184, 48);
      this.button2.Name = "button2";
      this.button2.TabIndex = 3;
      this.button2.Text = "Close";
      this.button2.Click += new System.EventHandler(this.button2_Click);
      // 
      // AddItemForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(280, 77);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.button2,
                                      this.button1,
                                      this.textBox1});
      this.Name = "AddItemForm";
      this.Text = "AddItem";
      this.ResumeLayout(false);

    }
    #endregion

    private void button1_Click_1(object sender, System.EventArgs e)
    {
      textBox1.Text = parent.AddItemToList (textBox1.Text);
//      textBox1.Text = "";
    }

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

}



           
       








Related examples in the same category

1.Add new item to ListBox (text from TextBox) Add new item to ListBox (text from TextBox)
2.Remove item if one is selected from ListBoxRemove item if one is selected from ListBox
3.Clear all items in a ListBoxClear all items in a ListBox
4.ListBox selected Item changed eventListBox selected Item changed event
5.Add Object to ListBoxAdd Object to ListBox
6.List Box click eventList Box click event
7.Set TopIndex to auto scroll ListBoxSet TopIndex to auto scroll ListBox
8.ListBox: font and imageListBox: font and image
9.CheckedListBox Demo 2CheckedListBox Demo 2
10.ListBox Demo 2ListBox Demo 2
11.ListBox and Metafile EnumListBox and Metafile Enum
12.ListBox ObjectsListBox Objects
13.Fill XML data to ListBox