[2008] adding/removing from listbox
HI guys,
I have a question about listboxes or trying to come up with an alternative.
My problem is this. I want to have a listbox. With the following strings.
1. apple
2. oranges
3. bananas
4. strawberries
5. carrots.
However if you just have a listbox what happens if you remove the 3rd item then you end up with
1. apple
2. oranges
4. strawberries
5. carrots.
but what we want to do is get rid of one and get this
1. apple
2. oranges
3. strawberries
4. carrots
So my question is how do you get a listbox to act like this but without clearing the listbox and making a new one because the listbox could have thousands of items in it. Or does anyone know of a better way? thanks.
Re: [2008] adding/removing from listbox
Here is a way to go.
vb.net Code:
Public Class Form1
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
ListBox1.Items.Add("One")
ListBox1.Items.Add("Two")
ListBox1.Items.Add("Three")
ListBox1.Items.Add("Four")
ListBox1.Items.Add("Five")
ListBox1.Items.Add("Six")
ListBox1.Items.Add("Seven")
ListBox1.Items.Add("Eight")
ListBox1.Items.Add("Nine")
ListBox1.Items.Add("Ten")
End Sub
Private Sub ListBox1_DrawItem( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.DrawItemEventArgs _
) Handles ListBox1.DrawItem
Dim myBrush As Brush
Dim myBackColor As Color = Color.White
Dim myForeColor As Color = Color.Black
Dim myFont As Font
Dim itemText As String
If e.Index > -1 Then
e.DrawBackground()
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
myBackColor = Color.DarkBlue
myForeColor = Color.White
End If
myBrush = New SolidBrush(myBackColor)
e.Graphics.FillRectangle(myBrush, e.Bounds)
myBrush = New SolidBrush(myForeColor)
myFont = New Font(ListBox1.Font.Name, ListBox1.Font.Size, ListBox1.Font.Style)
itemText = (e.Index + 1).ToString & ". " & ListBox1.Items(e.Index).ToString
e.Graphics.DrawString(itemText, myFont, myBrush, e.Bounds)
End If
End Sub
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Button1.Click
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
End Class
Here I am doing all the stuff in DrawItem event of ListBox control. When you click the Remove button to remove a item from listbox, it fires the DrawItem event. I am using this event to re-draw the text of listbox items.
Re: [2008] adding/removing from listbox
Put this together really quick. May not be the best way, but it worked fine for me without having to mess with redrawing and what not. Basically once the item is removed it loops through the list and updates the text of the item. May end up being not such a good idea for lists with thousands and thousands of items, but shouldn't be a problem on shorter ones. Anyhow, hope this helps.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ListBox1.SelectedIndex > -1 Then
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
For iCount As Integer = 0 To ListBox1.Items.Count - 1
ListBox1.Items(iCount) = iCount + 1 & ". " & ListBox1.Items(iCount).Substring(ListBox1.Items(iCount).IndexOf(". ") + 2)
Next
End If
End Sub
Re: [2008] adding/removing from listbox
thanks for the help guys.