Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlighting item after we move up in listbox

Status
Not open for further replies.

slowNsteady

Programmer
May 29, 2009
22
0
0
US
Hi friends
I have a listbox in form2 and the items are populated to it from another form named form1. In form2 i have created a button named btnUP. When we click the btnUP then the highlighted selected item in the listbox moves one level up. this is working fine.
But the problem is only the value is moving up but not the highlight.

i have also set the setselect property to true
"List1.SetSelected(i - 1, True)" but no use

don know wat is wrong with the code.But it is not working

So here is my code
Code:
Private Sub btnUP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUP.Click
        i = 0
        If Not List1.Items.Count = 0 Then
            For i = 0 To List1.Items.Count
                If List1.SelectedIndex = i Then  'identify the selected item           
                    'swap with the top item(move up)               
                    If i > 0 And Not i = List1.SelectedIndex - 1 Then
                        Item_Obj = List1.SelectedItem
                        List1.Items.Remove(Item_Obj)
                        List1.Items.Insert(i - 1, Item_Obj)
                        List1.SetSelected(i - 1, True)
                    End If
                End If
            Next
        End If
End Sub

Thanks in advance
Sri
 
Change it to this:
Code:
Private Sub btnUP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUP.Click
        If List1.SelectedItems.Count <= 1 And List1.SelectedIndex > 0 Then
            Dim cSelection As Object = List1.SelectedItem
            Dim NewIndex As Integer = List1.SelectedIndex - 1

            List1.Items.RemoveAt(List1.SelectedIndex)
            List1.Items.Insert(NewIndex, cSelection)
            List1.SelectedIndex = NewIndex
        End If
End Sub

For my program I used a VScrollBar and hid the scroll so it looked like it was just a set of up/down buttons and use the following code.
Code:
If track_lb.SelectedItems.Count <= 1 Then
                        If track_lb.SelectedIndex > -1 And track_vsb.Value > -1 Then
                            Dim cSelection As String
                            cSelection = track_lb.SelectedItem
                            track_lb.Items.RemoveAt(track_lb.SelectedIndex)
                            track_lb.Items.Insert(track_vsb.Value, cSelection)
                            track_lb.SelectedIndex = track_vsb.Value
                            track_vsb.Tag = track_vsb.Value
                        Else
                            track_vsb.Value = track_vsb.Tag
                        End If
                    End If
I used the tag to force the value as a safety to prevent values over/under the actual value of listbox items. There are other approaches I might take now as that code is fairly old, but I still use the program that uses it constantly.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I tried your program it works the same way as my code does

 
Hmmm, works fine for me. Do you mean something like this happens?

Dog
Cat
Rabbit <- Highlight
Horse
Rat

Then when you press the up button you get:

Dog
Rabbit
Cat <- Highlight
Horse
Rat

If so show how the code looks now.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 

i want somelike this ..

Dog
Cat
Rabbit <- Highlight
Horse
Rat

Then when you press the up button you get:

Dog
Rabbit <- Highlight
Cat
Horse
Rat



 
The code I gave you does exactly that. I even just made a blank form with a ListBox and an up button to test it again and it worked fine. You have something else going on. What other events/subs/functions do you have for the ListBox or that effect the list box? What version of VS/VB.Net? Also, show your code as you have it now.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Hi sorwen

it is working for me now.

i was calling another subroutine from the btnup subroutine , There i coded as List1.selectedIndex = 0

that was the problem

Thanks a lot

Sri
 
Cool. I'm glad to hear it is working now.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top