Add new item to ListBox (text from TextBox) : 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 
Add new item to ListBox (text from TextBox)
Add new item to ListBox (text from TextBox)


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

public class Form1 : Form
{
      private System.Windows.Forms.ListBox displayListBox;
      private System.Windows.Forms.TextBox inputTextBox;
      private System.Windows.Forms.Button addButton;
      private System.Windows.Forms.Button removeButton;
      private System.Windows.Forms.Button clearButton;
      
      public Form1() {
        InitializeComponent();
    }

      private void addButton_Clickobject sender, EventArgs e )
      {
        displayListBox.Items.AddinputTextBox.Text );
        inputTextBox.Clear();
      
      private void removeButton_Clickobject sender, EventArgs e )
      {
        if displayListBox.SelectedIndex != -)
          displayListBox.Items.RemoveAtdisplayListBox.SelectedIndex );
      }

      private void clearButton_Clickobject sender, EventArgs e )
      {
         displayListBox.Items.Clear();
      }

      private void InitializeComponent()
      {
         this.displayListBox = new System.Windows.Forms.ListBox();
         this.inputTextBox = new System.Windows.Forms.TextBox();
         this.addButton = new System.Windows.Forms.Button();
         this.removeButton = new System.Windows.Forms.Button();
         this.clearButton = new System.Windows.Forms.Button();
         this.SuspendLayout();
         // 
         // displayListBox
         // 
         this.displayListBox.FormattingEnabled = true;
         this.displayListBox.Location = new System.Drawing.Point(1312);
         this.displayListBox.Name = "displayListBox";
         this.displayListBox.Size = new System.Drawing.Size(119238);
         this.displayListBox.TabIndex = 0;
         // 
         // inputTextBox
         // 
         this.inputTextBox.Location = new System.Drawing.Point(14912);
         this.inputTextBox.Name = "inputTextBox";
         this.inputTextBox.Size = new System.Drawing.Size(10020);
         this.inputTextBox.TabIndex = 1;
         // 
         // addButton
         // 
         this.addButton.Location = new System.Drawing.Point(14956);
         this.addButton.Name = "addButton";
         this.addButton.Size = new System.Drawing.Size(10036);
         this.addButton.TabIndex = 2;
         this.addButton.Text = "Add";
         this.addButton.Click += new System.EventHandler(this.addButton_Click);
         // 
         // removeButton
         // 
         this.removeButton.Location = new System.Drawing.Point(149109);
         this.removeButton.Name = "removeButton";
         this.removeButton.Size = new System.Drawing.Size(10036);
         this.removeButton.TabIndex = 3;
         this.removeButton.Text = "Remove";
         this.removeButton.Click += new System.EventHandler(this.removeButton_Click);
         // 
         // clearButton
         // 
         this.clearButton.Location = new System.Drawing.Point(149165);
         this.clearButton.Name = "clearButton";
         this.clearButton.Size = new System.Drawing.Size(10036);
         this.clearButton.TabIndex = 4;
         this.clearButton.Text = "Clear";
         this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
         // ListBoxTestForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(263268);
         this.Controls.Add(this.clearButton);
         this.Controls.Add(this.removeButton);
         this.Controls.Add(this.addButton);
         this.Controls.Add(this.inputTextBox);
         this.Controls.Add(this.displayListBox);
         this.Name = "ListBoxTestForm";
         this.Text = "ListBoxTest";
         this.ResumeLayout(false);
         this.PerformLayout();

      }

      [STAThread]
      static void Main()
      {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
    }

}


           
       
Related examples in the same category
1.Remove item if one is selected from ListBoxRemove item if one is selected from ListBox
2.Clear all items in a ListBoxClear all items in a ListBox
3.ListBox selected Item changed eventListBox selected Item changed event
4.Add Object to ListBoxAdd Object to ListBox
5.List Box click eventList Box click event
6.Set TopIndex to auto scroll ListBoxSet TopIndex to auto scroll ListBox
7.Form with list, buttonForm with list, button
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.