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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » ListBoxScreenshots 
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 Disposebool disposing )
    {
      ifdisposing )
      {
        if (components != null
        {
          components.Dispose();
        }
      }
      base.Disposedisposing );
    }

    #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(4216);
      this.listBox1.Name = "listBox1";
      this.listBox1.Size = new System.Drawing.Size(208199);
      this.listBox1.TabIndex = 0;
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(17240);
      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(109240);
      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(201240);
      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(513);
      this.CancelButton = this.button3;
      this.ClientSize = new System.Drawing.Size(292273);
      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, -10)
      {
        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 Disposebool disposing )
    {
      ifdisposing )
      {
        if(components != null)
        {
          components.Dispose();
        }
      }
      base.Disposedisposing );
    }

    #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(288);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(22420);
      this.textBox1.TabIndex = 1;
      this.textBox1.Text = "";
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(4048);
      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(18448);
      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(513);
      this.ClientSize = new System.Drawing.Size(28077);
      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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.