【问题标题】:vb.net How to get get specific lines from Listbox and add them to other Listboxesvb.net 如何从列表框中获取特定行并将它们添加到其他列表框
【发布时间】:2019-04-24 23:57:09
【问题描述】:

我正在将(假设是 300 个)IP 加载到 listbox1 中,我希望将所有这些 IP 分成三个其他列表框。这样第一个列表框获得 100,第二个列表框获得另一个 100,第三个列表框获得最后一个 100。

例如。 Listbox1 包含项目 A、B、C。这些项目被划分为 Listbox2 包含 A,Listbox3 包含 B,Listbox4 包含 C。

【问题讨论】:

  • 为什么不从一开始就将这些项目添加到三个不同的 ListBox 中呢?您是否确定了定义这 3 个组的标准?这是你的实际问题吗?你有没有尝试过?

标签: vb.net listbox


【解决方案1】:

我将 listBox 的 .Count 除以 3,以找出每个列表框中有多少条目。然后我创建了三个循环来分别处理它们自己的 listBox 条目。我通过循环索引在 listBox 中输入值,方法是在 listBox1 中选择一个值,然后将所选值输入到 listBox2 中。

可能有更有效的方法,但这只是一种解决方案。

Dim numCount As Integer
numCount = listBox1.Items.Count
Dim perList As Integer = numCount / 3

For i As Integer = 0 To perList - 1
    listBox2.Items.Add(ListBox1.Items(i).ToString)
Next

For i As Integer = perList To perList * 2 - 1
    listBox3.Items.Add(ListBox1.Items(i).ToString)
Next

For i As Integer = perList * 2 To perList * 3 - 1
    listBox4.Items.Add(ListBox1.Items(i).ToString)
Next

【讨论】:

  • 无需添加listBox1.SetSelected(i, True)这一行。只需将您的 listBox2.Items.Add(listBox1.SelectedValue) 更改为这一行:listBox2.Items.Add(ListBox1.Items(i).ToString)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-24
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
相关资源
最近更新 更多