2013-06-25 61 views
1

我想知道是否有任何的方式来刷新列表框的续数刷新列表框在VB.Net

我与

ListBox1.Items.Add 

我已经设置为按钮的代码添加数据与代码删除选定的数据:

 For i As Integer = ListBox1.SelectedIndices.Count - 1 To 0 Step -1 
     ListBox1.Items.RemoveAt(ListBox1.SelectedIndices.Item(i)) 
    Next 

说我的列表框就是这样

  1. 扎克
  2. 巴里
  3. 约翰
  4. 尼克
  5. 布罗迪

如果我删除巴里说我怎么可以让数字改变,因此约翰将是2等

回答

1

BindingList在这里可以有所帮助,因为它可以侦听列表中的更改。

例如,创建一个Person类并覆盖ToString函数以显示排名和名称。

Public Class Person 
    Property Name As String 
    Property Rank As Integer 

    Public Overrides Function ToString() As String 
    Return Rank & ". " & Name 
    End Function 
End Class 

在您的形式,申报表,并添加事件处理程序:

Private people As New BindingList(Of Person) 

Public Sub New() 
    InitializeComponent() 

    AddHandler people.ListChanged, AddressOf people_ListChanged 
    people.Add(New Person() With {.Name = "Zach"}) 
    people.Add(New Person() With {.Name = "Barry"}) 
    people.Add(New Person() With {.Name = "John"}) 
    people.Add(New Person() With {.Name = "Nick"}) 
    people.Add(New Person() With {.Name = "Brodie"}) 
    ListBox1.DataSource = people 
End Sub 

Private Sub people_ListChanged(sender As Object, e As ListChangedEventArgs) 
    For i As Integer = 0 To people.Count - 1 
    people(i).Rank = i + 1 
    Next 
End Sub 

的ListChanged事件,就像他们在列表开槽更新每个成员的排名,它会自动更新因为DataSource来自人员列表。

简单删除按钮来测试列表,排名在成员列表,它会自动更新列表框自动更新:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    If ListBox1.SelectedIndex > -1 Then 
    people.Remove(ListBox1.SelectedItem) 
    End If 
End Sub 
0

你可以使用与列表框结合的字符串列表。

Dim NamesList1 As New List(Of String) 

Private Sub Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
    NamesList1.AddRange({"Zach", "Barry", "John", "Nick", "Brodie"}) 
    FillListBoxItems() 
End Sub 

Private Sub FillListBoxItems() 
    ListBox1.Items.Clear() 

    For i As Integer = 0 To NamesList1.Count - 1 
     ListBox1.Items.Add(i + 1 & ". " & NamesList1.Item(i)) 
    Next 
End Sub 

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
    For i As Integer = ListBox1.SelectedIndices.Count - 1 To 0 Step -1 
     NamesList1.RemoveAt(ListBox1.SelectedIndices.Item(i)) 
    Next 

    FillListBoxItems() 
End Sub 
+0

名称被用户inputed,所以我不知道他们会做在这个BOH方法中执行 – Binglee2323

+1

用户使用这个方法的输入并不重要,只是'NamesList1'将会填充来自用户的数据,而不是直接。这只是一个**示例** – SysDragon

+0

用户可以使用以下代码填充数据Private Sub Button2_Click(sender As System.Object,e As System.EventArgs)Handles Button2.Click If TextBox1.Text.Length> 0 Then NamesList1.Add(TextBox1.Text) ListBox1.Items.Add(NamesList1.Count&“。”&TextBox1.Text) End If End Sub –

1

如果你有GDI +绘图的一些想法,一个更有趣的方法是设置你的ListBox的DrawModeOwnerDrawFixed模式,并听取ListBox的DrawItem事件,像这样:

C#

private void listBox1_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    e.DrawBackground(); 
    e.Graphics.DrawString((e.Index + 1).ToString() + ". " + listBox1.Items[e.Index].ToString(), listBox1.Font, Brushes.Black, e.Bounds); 
    } 

VB.NET

Private Sub listBox1_DrawItem(sender As Object, e As DrawItemEventArgs) 
    e.DrawBackground() 
    e.Graphics.DrawString((e.Index + 1).ToString() & ". " & listBox1.Items(e.Index).ToString(), listBox1.Font, Brushes.Black, e.Bounds) 
End Sub 

向列表框添加或删除任何项目将自动重新编号所有项目。