vb.net - 多选列表框最后点击的项目到文本框
问题描述
我想在具有多选的 ListBox 上显示最后单击的项目。当我使用以下内容时,它仅显示所选列表中的第一项:
Textbox1.text = listbox1.text
解决方案
我认为您唯一的选择是存储所选索引的列表,并在选择更改时添加/删除它。
像这样的东西应该可以完成这项工作(假设 WinForms):
Private selectedIndices As New List(Of Integer)
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
' Add the newly selected items.
selectedIndices.AddRange(
ListBox1.SelectedIndices.Cast(Of Integer).
Where(Function(i) Not selectedIndices.Contains(i)))
' Remove the unselected items.
selectedIndices.RemoveAll(Function(i) Not ListBox1.SelectedIndices.Contains(i))
' Update the TextBox text. You can move this code to a different method
' if you want to trigger it using a button or something.
If selectedIndices.Count = 0 Then
TextBox1.Text = String.Empty
Else
Dim lastIndex As Integer = selectedIndices.Last()
TextBox1.Text = ListBox1.GetItemText(ListBox1.Items(lastIndex))
End If
End Sub
看看它的实际效果:
推荐阅读
- pandas - 无法在 seaborn 中创建对角直方图
- git - 如何从功能分支的拉取请求中删除以前的合并和提交?
- c - 在 C 中定期调用函数
- php - 在 Magento 2 后端的类别页面中的 H1 下添加 H2
- javascript - SetInterval 在 p5.js (JavaScript) 中不起作用
- javascript - GraphQL API 中 Sapper/Svelte 中的身份验证
- excel - 为什么此 VBA 代码会出现类型不匹配错误?
- java - Mockito 验证与添加到线程本地侦听器的已创建模拟的交互
- ruby-on-rails - 以非 root 用户身份在 Gitlab CI 中运行 Elasticsearch
- fetch - 为什么我在获取消息时收到错误消息?