Надежное программирование
Исключение может возникнуть при следующих условиях.
· В массиве содержится одно или несколько значений NULL.
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.");
}
Compiling the Code
This example requires:
· A form with a ListBox control named listBox1 and a Button control named button1. Set the button1Click event handler to button1_Click.
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 при загрузке формы добавляются некоторые элементы. Затем после нажатия кнопки в форме в ListBox выполняется поиск определенного элемента. Найденный элемент выделяется и отправляется сообщение об успешном выполнении поиска, которое содержит сам элемент и его индекс. В противном случае отправляется сообщение "Элемент не найден".
Пример
ß-----
Компиляция кода
Для этого примера необходимы следующие компоненты.
· Форма с элементом управления ListBox с именем listBox1 и с элементом управления Button с именем button1. Задайте обработчику событий Click button1 значение button1_Click.
Примечание. Этот код также можно использовать с элементом управления ComboBox, заменив элемент управления ComboBox с именем comboBox1 для элемента управления на элемент управления ListBox и изменив в коде имя с listBox1 на comboBox1. |
Date and Time Controls
Дата добавления: 2022-05-27; просмотров: 114;