Archive for July 23, 2008

Simple Application, ListBox and Item

Simple ListBox exercise…..  create two list box , one button and one label as following picture.

Select button and set the properties text  as  “Add Item” , and ID as “addItem”
For the First list box click the upper right icon and “Edit item”


And then , show the ListItem Collection Editor

To add the item –> Click Add button and Input the Text properties  anything you want.

After finish Adding item Click ok.    You will get the list of all items in the first Listbox
To add the code, Double click on  “Add button”  and put the following code.

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)

protected void adItem_Click(object sender, EventArgs e)
if (ListBox1.SelectedIndex != -1) // to check, is there any item in the first listbox is selected.
Label1.Text = “Move -> ” + ListBox1.SelectedItem.Value; // show, the moved item
ListBox2.Items.Add(ListBox1.SelectedItem.Text); // add the selected item to the second listbox.
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex); // Remove the selected item from the first listbox
Label1.Text = “Error , Please item you want to move “;

Categories: DOT NET