【问题标题】:0 added to listbox2 display when item selected in Listbox10 在 Listbox1 中选择项目时添加到 listbox2 显示
【发布时间】:2014-12-04 12:15:38
【问题描述】:

1.当我从 listbox1 中选择一个项目时,它会显示有关 listbox2 中项目的信息。但是,当我单击 listbox1 中的一个项目时,它会向我显示信息并在 listbox2 的末尾添加一个 0。我如何摆脱 0 或者我在做什么导致这种情况?我认为它可能正在显示索引号。这是我的代码-

If ListBox1.SelectedIndex = 2 Then ListBox2.Items.Add("60137" & ListBox2.Items.Add("60138"))

2.另外,当我在 list1 中选择不同的项目时,如何清除 List2 以使它们不会同时填充 list2?

【问题讨论】:

  • 你为什么要为相关项目使用 ListBox? ListBox 用于显示用户可以从中选择的多个项目。看起来标签会是更好的选择。

标签: vb.net listbox


【解决方案1】:

*忽略这是一个糟糕的设计这一事实......

变化:

If ListBox1.SelectedIndex = 2 Then ListBox2.Items.Add("60137" & ListBox2.Items.Add("60138"))

收件人:

If ListBox1.SelectedIndex = 2 Then 
    ListBox2.Items.Clear
    ListBox2.Items.Add("60137")
End If

这是另一种方法:

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim petA As New Pet
        petA.Name = "Puss in Boots"
        petA.Species = "Cat"
        ListBox1.Items.Add(petA)

        Dim petB As New Pet
        petB.Name = "Nemo"
        petB.Species = "Fish"
        ListBox1.Items.Add(petB)

        Dim petC As New Pet
        petC.Name = "Rango"
        petC.Species = "Lizard"
        ListBox1.Items.Add(petC)
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedIndex <> -1 Then
            Dim P As Pet = DirectCast(ListBox1.SelectedItem, Pet)
            Label1.Text = P.Species
        End If
    End Sub

End Class

Public Class Pet

    Public Name As String
    Public Species As String

    Public Overrides Function ToString() As String
        Return Name
    End Function

End Class

【讨论】:

  • 我知道这很粗糙。你会建议用什么来代替这个?而且它仍然添加了一个 0 例如“601370”我愿意学习:) 还是谢谢你。
猜你喜欢
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多