Clear all items in a ListBox : 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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » GUI Windows Form » ListBoxScreenshots 
Clear all items in a ListBox
Clear all items in a ListBox

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.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.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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.