logo search
Учебник_ПОА

ListBox and ComboBox Controls

How to: Add and Clear Items in a ListBox Control

This example adds the contents of a Windows Forms TextBox control to a ListBox control when you click button1, and clears the contents when you click button2.

Example

private void button1_Click(object sender, System.EventArgs e)

{

listBox1.Items.Add("Sally");

listBox1.Items.Add("Craig");

}

private void button2_Click(object sender, System.EventArgs e)

{

listBox1.Items.Clear();

}

Compiling the Code

This example requires:

ListBoxиComboBox

Добавление и удаление элементов в элементе управления "ListBox"

В этом примере содержимое элемента управления TextBox Windows Forms добавляется7 в элемент управления ListBox при нажатии кнопки button1, а удаляется при нажатии кнопки button2.

Пример

private void button1_Click(object sender, System.EventArgs e)

{

listBox1.Items.Add("Sally");

listBox1.Items.Add("Craig");

}

private void button2_Click(object sender, System.EventArgs e)

{

listBox1.Items.Clear();

}

Компиляция кода

Для этого примера необходимы следующие компоненты.

How to: Determine the Selected Item in a ListBox Control

This example determines which item has been selected in a Windows Forms ListBox control.

Example

private void Form1_Load(object sender, System.EventArgs e)

{

listBox1.Items.Add("One");

listBox1.Items.Add("Two");

listBox1.Items.Add("Three");

}private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)

{

if ((string)listBox1.SelectedItem == "Two")

MessageBox.Show((string)listBox1.SelectedItem);

}

Compiling the Code

This example requires:

Note:

This code can also be used with a ComboBox control by substituting a ComboBox control named comboBox1 for the ListBox control and changing the code from listBox1 to comboBox1.

Определение выбранных элементов в элементе управления "ListBox"

В этом примере определяется, какой элемент был выбран в элементе управления ListBox Windows Forms.

Пример

private void Form1_Load(object sender, System.EventArgs e)

{

listBox1.Items.Add("One");

listBox1.Items.Add("Two");

listBox1.Items.Add("Three");

}

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)

{

if ((string)listBox1.SelectedItem == "Two")

MessageBox.Show((string)listBox1.SelectedItem);

}

Компиляция кода

Для этого примера необходимы следующие компоненты.

Примечание.

Код также можно использовать с элементом управления ComboBox, заменив элемент управления ListBox на элемент управления ComboBox с именем comboBox1 и изменив в коде имя с listBox1 на comboBox1.

How to: Populate a ListBox Control with an Array of Strings

This example adds an array of strings to a ListBox control when the Windows Form opens.

Example

private void Form1_Load(object sender, System.EventArgs e)

{

string [] myList = new string[4];

myList[0] = "One";

myList[1] = "Two";

myList[2] = "Three";

myList[3] = "Four";

listBox1.Items.AddRange(myList);

}

Compiling the Code

This example requires:

This code can also be used with a ComboBox control by substituting a ComboBox control named comboBox1 for the ListBox control and changing the code from listBox1 to comboBox1.

Robust Programming

The following conditions may cause an exception:

Заполнение элемента управления "ListBox" массивом строк

В этом примере массив строк добавляется в элемент управления ListBox при открытии формы Windows Form.

Пример

private void Form1_Load(object sender, System.EventArgs e)

{

string [] myList = new string[4];

myList[0] = "One";

myList[1] = "Two";

myList[2] = "Three";

myList[3] = "Four";

listBox1.Items.AddRange(myList);

}

Компиляция кода

Для этого примера необходимы следующие компоненты.

Код также можно использовать с элементом управления ComboBox, заменив элемент управления ListBox на элемент управления ComboBox с именем comboBox1 и изменив в коде имя с listBox1 на comboBox1.

Надежное программирование

Исключение может возникнуть при следующих условиях.

How to: Search for an Item in a ListBox Control

In this example, you add some items to a Windows Forms ListBox control when the form is loaded. Then you search the ListBox for a specific item by clicking a button on the form. If the item is found, it is selected and a success message, which contains the item and its index, is sent by using a message box. Otherwise, an "Item not found" message is sent.

Example

private void Form1_Load(object sender, System.EventArgs e)

{

listBox1.Items.Add("Angelina");

listBox1.Items.Add("Isabella");

listBox1.Items.Add("Sarah");

}

private void button1_Click(object sender, System.EventArgs e)

{

// Set the search string:

string myString = "Isabella";

// Search starting from index -1:

int index = listBox1.FindString(myString, -1);

if (index != -1)

{

// Select the found item:

listBox1.SetSelected(index,true);

// Send a success message:

MessageBox.Show("Found the item \"" + myString + "\" at index: " + index);

}

else

MessageBox.Show("Item not found.");

}