VB.NET中如何动态修改列表框选中项的显示文本?
- 内容介绍
- 相关推荐
本文共计232个文字,预计阅读时间需要1分钟。
我尝试使用如下代码,但这种方法不起作用:vbPrivate 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.TextEnd Sub
我尝试使用这样的代码: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
本文共计232个文字,预计阅读时间需要1分钟。
我尝试使用如下代码,但这种方法不起作用:vbPrivate 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.TextEnd Sub
我尝试使用这样的代码: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

