我试过这样的代码:
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles MyBase.Leave
' This way is not working
ListBox1.SelectedItem = TextBox1.Text
' This is not working too
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text
End Sub
表格如下所示:
当用户在文本框中键入时,我需要更改该列表文本。可以在运行时做到这一点吗?
最佳答案
您正在使用表单的离开事件 MyBase.Leave
,所以当它着火时,对你来说毫无用处。
尝试改用 TextBox 的 TextChanged 事件。
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) _
Handles TextBox1.TextChanged
确保检查是否在 ListBox 中实际选择了一个项目:
If ListBox1.SelectedIndex > -1 Then
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text
End If
关于vb.net - 如何在运行时更改列表框中的选定项文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27447474/