1

I was wondering if there is any way to refresh the cont numbers in a listbox

i am adding data with the code

    ListBox1.Items.Add

I have set up as button to remove selected data with the code:

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

say my list box is like this

  1. Zach
  2. Barry
  3. John
  4. Nick
  5. Brodie

if i deleted say barry how can i make the numbers change so john would be 2. etc

4

3 回答 3

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 事件只是更新每个成员在列表中的排名,这将自动更新 ListBox,因为 DataSource 来自人员列表。

一个简单的删除按钮来测试列表,人物列表中的排名会自动更新,会自动更新ListBox:

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
于 2013-06-25T15:51:02.507 回答
1

If you have some idea of GDI+ drawing, a more interesting approach is to set your ListBox's DrawMode to OwnerDrawFixed mode and listen to ListBox's DrawItem event, something like this:

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

Adding or removing any items to the ListBox will automatically renumber all the items.

于 2013-06-25T10:55:31.693 回答
0

您可以使用与 ListBox 结合的字符串列表。

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
于 2013-06-25T10:39:15.007 回答