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 ,
basheer_basataAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.