Advertisement

Responsive Advertisement

III.2- Using List Box

 2- Add at run time (time code)

When selecting an item, display the select location in the text box message.

SourceCode:

private void Form1_Load(object sender, EventArgs e)

        {

            for (int i = 0; i < 6; i++)

            {

                cboItem.Items.Add("Item" + i.ToString());

            }

        }

 

        private void cboItem_SelectedIndexChanged(object sender, EventArgs e)

        {

            txtMessage.Text = "Selected at position"+ cboItem.SelectedIndex.ToString();

        }

Result

III.2- Using List Box

In Windows Forms, ListBox control is used to show multiple elements in a list, from which a user can select one or more elements and the elements are generally displayed in multiple columns. The ListBox class is used to represent the windows list box and also provide different types of properties, methods, and events. It is defined under System.Windows.Forms namespace. The ListBox class contains three different types of collection classes, i.e.


The following is a list of key properties and methods that need to be used:

For example, they have the form as shown below.




Source Code:

private void btnAdd_Click(object sender, EventArgs e)

{

if (this.txtItem.Text.Trim () != "")

}

else

this.lstItem.Items.Add(txtItem.Text);

MessageBox.Show("Invalid item. ");

txtItem.Clear();

txtItem. Focus();

}

private void btnRemove_Click(object sender, EventArgs e)

{

if (1stItem.Items.Count==0 | 1stItem. SelectedIndex==-1)

MessageBox.Show("There is no item to delete");

else

1stItem.Items.RemoveAt (1stItem. SelectedIndex);

}

private void btnEdit_Click(object sender, EventArgs e)

{

if (1stItem. SelectedIndex == -1)

{

MessageBox.Show("Invalid item to edit");

Return;

if (txtItem.Text.Trim() == ””)

{

 

MessageBox.Show("Invalid item to update.");

}

return;

}

1stItem. Items [1stItem. SelectedIndex] =txtItem.Text;

private void 1stItem_DoubleClick(object sender, EventArgs e)

{

if (this.lstItem. SelectedIndex == -1)

MessageBox.Show("Double click on item to edit");

else

{

            txtItem.Text = (string)IstItem.selectedItem;

}

private void btnSort_Click(object sender, EventArgs e)

{

lstItem. Sorted = true;

}

private void btnSearch_Click(object sender, EventArgs e)

{

if (txtItem.Text.Trim() != "")

{

int x = -1;

x = 1stItem.Items.IndexOf (txtItem.Text);

if (x == -1)

MessageBox.Show("Not Found");

else

1stItem. SelectedIndex = x;

}

}

 



















Post a Comment

0 Comments