• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1548
  • Last Modified:

Move Up and down CheckedListBox items ?




Hi to All .NET Experts,

I' using VB .NET contains CheckedListBox control, this control has alot of items
I have 2 buttons onr to move up the selected item and the other move it to down .

How can I do it plz ,
0
basheer_basata
Asked:
basheer_basata
  • 3
  • 2
1 Solution
 
Bob LearnedCommented:
Try this:

  Public Sub MoveItem(ByVal list As ListBox, ByVal direction As Integer)

    If (direction = -1 And list.SelectedIndex > 0) Or _
     (direction = 1 And list.SelectedIndex < list.Items.Count - 1) Then

      Dim index As Integer = list.SelectedIndex
      Dim value As String = list.Items(index)

      list.Items.RemoveAt(index)
      list.Items.Insert(index + direction, value)
    End If

  End Sub

Bob
0
 
Bob LearnedCommented:
Example:

  Me.CheckedListBox1.Items.AddRange(New String() {"Test 1", "Test 2", "Test 3"})
  Me.CheckedListBox1.SelectedIndex = 0
  MoveItem(Me.CheckedListBox1, 1)
  Me.CheckedListBox1.SelectedIndex = 2
  MoveItem(Me.CheckedListBox1, -1)

Bob
0
 
basheer_basataAuthor Commented:
Dear Bob ,

It is CheckedListBox not ListBox

Basheer
0
 
Bob LearnedCommented:
Basheer,

There is some magic going on there.  CheckedListBox inherits from ListBox, so you can always cast a CheckedListBox down to a ListBox.  In this way, you don't have to have a different method for each type (CheckedListBox and ListBox).  Try it with the CheckedListBox, and it will work just fine.

Bob
0
 
basheer_basataAuthor Commented:
Dear Bob ,

You are right =)
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now