Results 1 to 4 of 4

Thread: [2008] adding/removing from listbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    407

    [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.

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: [2008] adding/removing from listbox

    Here is a way to go.

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load( _
    4.         ByVal sender As System.Object, _
    5.         ByVal e As System.EventArgs _
    6.     ) Handles MyBase.Load
    7.  
    8.         ListBox1.DrawMode = DrawMode.OwnerDrawFixed
    9.  
    10.         ListBox1.Items.Add("One")
    11.         ListBox1.Items.Add("Two")
    12.         ListBox1.Items.Add("Three")
    13.         ListBox1.Items.Add("Four")
    14.         ListBox1.Items.Add("Five")
    15.         ListBox1.Items.Add("Six")
    16.         ListBox1.Items.Add("Seven")
    17.         ListBox1.Items.Add("Eight")
    18.         ListBox1.Items.Add("Nine")
    19.         ListBox1.Items.Add("Ten")
    20.     End Sub
    21.  
    22.     Private Sub ListBox1_DrawItem( _
    23.         ByVal sender As Object, _
    24.         ByVal e As System.Windows.Forms.DrawItemEventArgs _
    25.     ) Handles ListBox1.DrawItem
    26.  
    27.         Dim myBrush As Brush
    28.         Dim myBackColor As Color = Color.White
    29.         Dim myForeColor As Color = Color.Black
    30.         Dim myFont As Font
    31.         Dim itemText As String
    32.  
    33.         If e.Index > -1 Then
    34.  
    35.             e.DrawBackground()
    36.  
    37.             If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
    38.                 myBackColor = Color.DarkBlue
    39.                 myForeColor = Color.White
    40.             End If
    41.  
    42.             myBrush = New SolidBrush(myBackColor)
    43.             e.Graphics.FillRectangle(myBrush, e.Bounds)
    44.  
    45.             myBrush = New SolidBrush(myForeColor)
    46.  
    47.             myFont = New Font(ListBox1.Font.Name, ListBox1.Font.Size, ListBox1.Font.Style)
    48.             itemText = (e.Index + 1).ToString & ". " & ListBox1.Items(e.Index).ToString
    49.             e.Graphics.DrawString(itemText, myFont, myBrush, e.Bounds)
    50.         End If
    51.     End Sub
    52.  
    53.     Private Sub Button1_Click( _
    54.         ByVal sender As System.Object, _
    55.         ByVal e As System.EventArgs _
    56.     ) Handles Button1.Click
    57.  
    58.         ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
    59.     End Sub
    60.  
    61. 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.

  3. #3
    Lively Member
    Join Date
    Jan 2007
    Posts
    96

    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    407

    Re: [2008] adding/removing from listbox

    thanks for the help guys.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width