Редактирование содержимого ListBox - PullRequest
2 голосов
/ 02 августа 2010

У меня есть ListBox с различными строками данных (в основном, предметы, которые кто-то покупает), и когда пользователь нажимает на строку, он заполняет текстовое поле сбоку количеством того, что он покупает.Например, одна строка может быть «Карандаши, $ 5, 3», а при щелчке по строке в текстовом поле появляется «3».

Если пользователь изменяет это значение, например, на 7, я хочу, чтобывернемся в ряд, и теперь у нас есть «Карандаши, 5, 7 долларов»

Как мне это сделать?

1 Ответ

0 голосов
/ 02 августа 2010

Вам необходимо запрограммировать событие Listbox SelectedIndexChanged и событие Textbox TextChanged.Вот ваш фрагмент:

Private Sub ListBox1_SelectedIndexChanged( _
    ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles ListBox1.SelectedIndexChanged

    // Exit if no item is selected
    If ListBox1.SelectedItem Is Nothing Then Exit Sub

    // Get the item's text
    Dim item As String = ListBox1.SelectedItem.ToString

    // Keep only the last part
    item = item.Substring(item.LastIndexOf(", ") + 2)

    // Update the textbox
    TextBox1.Text = item

End Sub

Private Sub TextBox1_TextChanged( _
    ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles TextBox1.TextChanged

    // Exit if no item is selected
    If ListBox1.SelectedItem Is Nothing Then Exit Sub

    // Get the item's text, removing the last part
    Dim item As String = ListBox1.SelectedItem.ToString
    item = item.Substring(0, item.LastIndexOf(", ") + 2)

    // Add the numerical value of the textbox's text
    item &= Val(TextBox1.Text)

    // Get the index of the selected item
    Dim index As Integer = ListBox1.SelectedIndex

    // Remove the old item, add the new one and select it
    ListBox1.Items.RemoveAt(index)
    ListBox1.Items.Insert(index, item)
    ListBox1.SelectedIndex = index

End Sub
...