我尝试使用这样的代码: 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.SelectedIn
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