changing the order of a listbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi;

I need to change the order of a listbox array from a form app
where I select order up or down.

What's a good way to do that?

Regards;

Segue
 
Hi try this, place two buttons and a listbox on a form, button1.text = UP,
button2.text = DOWN, the listbox should have selectionmode set to one. Now
you can try this code.

Hth Greetz Peter

Private Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
ListBox1.Items.Add("First")
ListBox1.Items.Add("Second")
ListBox1.Items.Add("Third")
ListBox1.Items.Add("Fourth")
ListBox1.Items.Add("Fifth")
ListBox1.Items.Add("Sixth")
ListBox1.Items.Add("Seventh")

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Not ListBox1.SelectedItem Is Nothing Then
moveup()
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
If Not ListBox1.SelectedItem Is Nothing Then
movedown()
End If
End Sub

Private Sub moveup()
Dim tmpItem1, tmpItem2 As Object
Dim intIndex As Integer
If ListBox1.SelectedIndex > 0 Then
intIndex = ListBox1.SelectedIndex
tmpItem1 = ListBox1.Items.Item(intIndex)
tmpItem2 = ListBox1.Items.Item(intIndex - 1)
ListBox1.Items.RemoveAt(intIndex)
ListBox1.Items.RemoveAt(intIndex - 1)
ListBox1.Items.Insert(intIndex - 1, tmpItem2)
ListBox1.Items.Insert(intIndex - 1, tmpItem1)
ListBox1.SelectedItem = ListBox1.Items.Item(intIndex - 1)
End If
End Sub

Private Sub movedown()
Dim tmpItem1, tmpItem2 As Object
Dim intIndex As Integer
If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
intIndex = ListBox1.SelectedIndex
tmpItem1 = ListBox1.Items.Item(intIndex)
tmpItem2 = ListBox1.Items.Item(intIndex + 1)
ListBox1.Items.RemoveAt(intIndex)
ListBox1.Items.RemoveAt(intIndex)
ListBox1.Items.Insert(intIndex, tmpItem2)
ListBox1.Items.Insert(intIndex + 1, tmpItem1)
ListBox1.SelectedItem = ListBox1.Items.Item(intIndex + 1)
End If
End Sub
 
Perfect. Thanks so much.

Another question.

I'm using a checkbox list (appname) that has fields associated
with each checkbox list appname. You click a button and I instantiate this
newform where you can choose the order of the fields. Then
I want to reinstantiate the newform with the next checkboxlist's
group of fields so that they can be ordered and then keep going for each
appname so I have a listbox that has a two dimensional array:
(1(appname1), field1)
(1(appname1), field1)
(1(appname1), field1)
(1(appname1), field1)
then appname2:
(1(appname2), field2)
(1(appname2), field2)
(1(appname2), field2)
(1(appname2), field2)

for the next appname and its group and so on.

Can anyone see a good design for this.
of fields and keep them in a listbox arraylist of appname1.

Regards;

Segue
 
I had to save the re-ordered listbox back to the orignal form in
a file. If I did it as a property it didn't seem like I could save it back
to the original forms listbox.

Regards;

Segue
 
Back
Top