vb.net - 如何在运行时更改列表框中的选定项文本?

标签 vb.net winforms textbox listbox

我试过这样的代码:

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

表格如下所示:

enter image description here

当用户在文本框中键入时,我需要更改该列表文本。可以在运行时做到这一点吗?

最佳答案

您正在使用表单的离开事件 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/

相关文章:

c# - 在C#中播放多个wav文件

c# - 如何使面板内的鼠标拖动从窗口移动?

javascript - Onfocus 文本框用户名放入文本框名字值和文本框姓氏值分割

wpf - 如何将文本框的效果添加到样式

c# - 如何在 C# 的 TextBox 中引发 textBox_InsertPlaceChanged 事件?

mysql - 将查询的总和传输到标签

c# - 在 .NET 世界中开始学习 C# 还是 C++ 更好?

.net - 如何在.net框架4.0中压缩文件夹

.net - 了解KeepAlive模式下的HttpWebRequest

c# - 在flowlayoutpanel中动态添加控件