Reader Level:
ARTICLE

Using ListBox in C#

Posted by devinder arora Articles | Windows Controls C# December 28, 2004
The source code demonstrates how to use a listbox control in C# - adding, deleting and altering strings.
  • 0
  • 0
  • 249253

Description:

Tools: Windows 2000 & VS.NET SDK Beta 2.0

The source code demonstrates how to use a listbox control in C# - adding, deleting and altering strings. The source code is self-explanatory. To compile the code, install .NET SDK including C# compiler and use the following commands on MS-DOS prompt:

>csc ListBox.cs
>ListBox.exe 


 ListBoxDA001.gif

The source code listed in the following table. 

namespace
test
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class ListBox : System.Windows.Forms.Form
{
private System.ComponentModel.Container container;
private System.Windows.Forms.Button buttonAdd;
private System.Windows.Forms.Button buttonClose;
private System.Windows.Forms.Button buttonModify;
private System.Windows.Forms.Button buttonDelete;
private System.Windows.Forms.Button buttonMoveUp;
private System.Windows.Forms.Button buttonMoveDown;
private System.Windows.Forms.ListBox listbox;
private System.Windows.Forms.TextBox textbox;
private System.Windows.Forms.Label label;
private int nSelectedIndex;
//*********SIZE & LOCATION******************//
// COMPONENT - BUTTON(s) aligned along X-axis.
const int BUTTON_LENGTH = 50;
const int BUTTON_HEIGHT = 20;
const int FIRSTBUTTON_XPOS = 20;
const int FIRSTBUTTON_YPOS =220;
const int XSPACING = 70; // (Note: XSPACING >= BUTTON_LENGTH)
const int YSPACING = 0;
//COMPONENT - MOVE BUTTONS
const int MBUTTON_LENGTH = 20;
const int MBUTTON_HEIGHT = 20;
const int FIRSTMBUTTON_XPOS = 220;
const int FIRSTMBUTTON_YPOS =70;
const int SECONDMBUTTON_XPOS = 220;
const int SECONDMBUTTON_YPOS =100;
// COMPONENT - LISTBOX
const int LISTBOX_LENGTH = 3*BUTTON_LENGTH;
const int LISTBOX_HEIGHT = 6*BUTTON_HEIGHT;
const int LISTBOX_XPOS = 50;
const int LISTBOX_YPOS = 50;
// COMPONENT - LABEL
const int LABEL_LENGTH = 50;
const int LABEL_HEIGHT = 50;
const int LABEL_XPOS = 20; // align it with first button
const int LABEL_YPOS = 173;
// COMPONENT - TEXTBOX
const int TEXTBOX_LENGTH = 120;
const int TEXTBOX_HEIGHT = 50;
const int TEXTBOX_XPOS = 70;
const int TEXTBOX_YPOS = 170;
public ListBox() : base()
{
InitializeComponent();
}
public override void Dispose()
{
base.Dispose();
container.Dispose();
}
private void InitializeComponent()
{
// this
this.container = new System.ComponentModel.Container();
this.Text="List Box";
// buttonAdd
this.buttonAdd = new System.Windows.Forms.Button();
buttonAdd.Location =
new System.Drawing.Point(FIRSTBUTTON_XPOS,FIRSTBUTTON_YPOS);
buttonAdd.Text = "&Add";
buttonAdd.Size =
new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT); buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click); buttonAdd.Enabled = false;
this.Controls.Add(this.buttonAdd);
//buttonModify
this.buttonModify = new System.Windows.Forms.Button();
buttonModify.Location =
new System.Drawing.Point(FIRSTBUTTON_XPOS+XSPACING,FIRSTBUTTON_YPOS+YSPACING);
buttonModify.Text = "&Modify";
buttonModify.Size =
new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT); buttonModify.Click += new System.EventHandler(this.buttonModify_Click); buttonModify.Enabled = false;
this.Controls.Add(this.buttonModify);
//buttonDelete
this.buttonDelete = new System.Windows.Forms.Button();
buttonDelete.Location =
new System.Drawing.Point(FIRSTBUTTON_XPOS+2*XSPACING,FIRSTBUTTON_YPOS+2*YSPACING); buttonDelete.Text = "&Delete";
buttonDelete.Size =
new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT); buttonDelete.Enabled = false;
buttonDelete.Click +=
new System.EventHandler(this.buttonDelete_Click); this.Controls.Add(this.buttonDelete);
// buttonClose
this.buttonClose = new System.Windows.Forms.Button();
buttonClose.Location =
new System.Drawing.Point(FIRSTBUTTON_XPOS+3*XSPACING,FIRSTBUTTON_YPOS+3*YSPACING); buttonClose.Text = "&Close";
buttonClose.Size =
new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT); buttonClose.Click += new System.EventHandler(this.buttonClose_Click); this.Controls.Add(this.buttonClose);
// listbox
this.listbox = new System.Windows.Forms.ListBox();
listbox.Location =
new System.Drawing.Point(LISTBOX_XPOS,LISTBOX_YPOS); listbox.Size = new System.Drawing.Size(LISTBOX_LENGTH,LISTBOX_HEIGHT); listbox.Click += new System.EventHandler(this.listbox_SelectedIndexChanged); listbox.BackColor = (Color)System.Drawing.SystemColors.Desktop;
this.Controls.Add(this.listbox);
// label
this.label = new System.Windows.Forms.Label();
label.Location =
new System.Drawing.Point(LABEL_XPOS,LABEL_YPOS);
label.Size =
new System.Drawing.Size(LABEL_LENGTH,LABEL_HEIGHT);
label.Text = "Enter:";
this.Controls.Add(this.label);
// textbox
this.textbox = new System.Windows.Forms.TextBox();
textbox.Location =
new System.Drawing.Point(TEXTBOX_XPOS,TEXTBOX_YPOS); textbox.Click += new System.EventHandler(this.textbox_Click);
textbox.Size =
new System.Drawing.Size(TEXTBOX_LENGTH,TEXTBOX_HEIGHT); this.Controls.Add(this.textbox);
// buttonMoveUp
this.buttonMoveUp = new System.Windows.Forms.Button(); buttonMoveUp.Location = new System.Drawing.Point(FIRSTMBUTTON_XPOS,FIRSTMBUTTON_YPOS);
buttonMoveUp.Text = "<";
buttonMoveUp.Size =
new
System.Drawing.Size(MBUTTON_LENGTH,MBUTTON_HEIGHT);
buttonMoveUp.Click +=
new System.EventHandler(this.buttonMoveUp_Click);
buttonMoveUp.Enabled =
false;
this.Controls.Add(this.buttonMoveUp);
// buttonMoveDown
this.buttonMoveDown = new System.Windows.Forms.Button();
buttonMoveDown.Location =
new System.Drawing.Point(SECONDMBUTTON_XPOS,SECONDMBUTTON_YPOS);
buttonMoveDown.Text = ">";
buttonMoveDown.Size =
new System.Drawing.Size(MBUTTON_LENGTH,MBUTTON_HEIGHT);
buttonMoveDown.Click +=
new System.EventHandler(this.buttonMoveDown_Click); buttonMoveDown.Enabled = false;
this.Controls.Add(this.buttonMoveDown);
}
protected void textbox_Click(Object sender, System.EventArgs e)
{
this.buttonAdd.Enabled = true;
if (this.listbox.Items.Count>0)
EnableAllListBoxButtons();
}
protected void listbox_SelectedIndexChanged(object sender, System.EventArgs e)
{
nSelectedIndex =
this.listbox.SelectedIndex;
string szSelected = (string)this.listbox.SelectedItem;
this.textbox.Text = szSelected;
}
protected void buttonAdd_Click(Object sender, System.EventArgs e)
{
if (this.textbox.Text !="")
{
this.listbox.Items.Add(this.textbox.Text);
this.textbox.Text = "";
EnableAllListBoxButtons();
}
}
protected void buttonModify_Click(Object sender, System.EventArgs e)
{
this.listbox.Items[nSelectedIndex] = this.textbox.Text;
}
protected void buttonDelete_Click(Object sender, System.EventArgs e)
{
nSelectedIndex =
this.listbox.SelectedIndex;
this.listbox.Items.Remove(nSelectedIndex);
System.Console.WriteLine("Remove fn does not work...");
}
protected void buttonClose_Click(Object sender, System.EventArgs e)
{
this.Close();
}
protected void buttonMoveUp_Click(Object sender, System.EventArgs e)
{
if (this.listbox.SelectedIndex >0)
this.listbox.SelectedIndex--;
}
protected void buttonMoveDown_Click(Object sender, System.EventArgs e)
{
if (this.listbox.SelectedIndex < this.listbox.Items.Count-1)
this
.listbox.SelectedIndex++;
}
private void EnableAllListBoxButtons()
{
this.buttonAdd.Enabled = true;
this.buttonModify.Enabled = true;
this.buttonDelete.Enabled = true;
this.buttonMoveUp.Enabled = true;
this.buttonMoveDown.Enabled = true;
}
[STAThread]

public static void Main(string[] args)
{
Application.Run(
new ListBox());
}
}
// class
} // namespace 

Further Readings

Here are more articles on ListBox control.

COMMENT USING

Trending up