When I clicked on an item in the listbox, the text is displayed in textbox1, for example, it clicked on item 12 (listbox.item.add ("12")); and the textbox text appeared in the textbox1.text = "asdasdasd" if I clicked on another item, then another text appeared in the textbox1 I don’t know how to do it ..

  • winforms or wpf? - Gardes
  • @ S.Kost winforms - komra23

1 answer 1

The ListBox component has a SelectedIndexChanged event, in the handler of which you can implement the logic you need. For example:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { //Объявляем переменную lb и приводим компонент, вызвавший событие к типу ListBox //(у нас же событие сгененрировал ListBox1, верно? А он имеет тип ListBox. var lb = sender as ListBox; //В наж текст-бокс пишем номер выбранного пункта, здесь lb.SelectedIndex + 1 так как //нумерация с нуля. textBox1.Text = "Выбран пункт №" + (lb.SelectedIndex + 1).ToString(); } 

or so:

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { //Получим номер выбранного пункта: int i = (sender as ListBox).SelectedIndex + 1; //В зависимости от номера делаем какие-то РАЗНЫЕ действия: switch (i) { case 1: textBox1.Text = "Выбран первый пункт списка"; break; case 2: textBox1.Text = "Тра-ля-ля"; break; case 3: textBox1.Text = "Тру-ля-ля"; break; case 4: case 5: textBox1.Text = "Выбран 4 или 5 пункт, лениво уточнять..."; break; default: textBox1.Text = "Я таких цифр не знаю!"; break; } } 

UPDATE:

For binding, you can declare and fill in an array of values ​​associated with each item. In the example, I declare a string array (although it can be of any type) of 5 points, and, depending on the index of the selected item in the ListBox , I derive the array element corresponding to this index. If you have 100 items in the ListBox , then the array must be at least 100 points. But just in case, I check the index:

  public string[] textData = new string[5] { "qwe", "rty", "uio", "p[]", "asd" }; private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { //Получим индекс выбранного пункта: int i = (sender as ListBox).SelectedIndex; //Проверяем, не вышли ли мы за границы масива? if (i > textData.GetLength(0)-1) { textBox1.Text = "С этим пунктом ничего не связано!"; return; } textBox1.Text = textData[i]; } 
  • Not that I need, I can select any item from the list and the text string that is "attached" to that item should be displayed in the textbox - komra23
  • And how exactly is the string attached to you? Indeed, in the example I specified, depending on the selected item, you can define a different logic that will take into account just the binding mechanism that you have. - BlackWitcher
  • the first item is tied (as an example) the word "qwe", the second point is tied (as an example) the word "rty", the third point is tied (as an example) the word "uio", the fourth point is tied (as an example) the word "pasd "something like this - komra23
  • I understand what you are talking about, and asked how exactly in the code you have implemented such a binding. Through an array, for example, perhaps, or else how? - BlackWitcher
  • Updated and added the answer, making a binding through the array. - BlackWitcher