By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,978 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,978 IT Pros & Developers. It's quick & easy.

changing the order of a listbox

P: n/a

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
Nov 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"segue" <se***@discussions.microsoft.com> schreef in bericht
news:88**********************************@microsof t.com...

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

Nov 23 '05 #2

P: n/a
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

"Peter Proost" wrote:
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
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"segue" <se***@discussions.microsoft.com> schreef in bericht
news:88**********************************@microsof t.com...

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


Nov 23 '05 #3

P: n/a
Please don't respond to this it's too complicated.

"segue" wrote:
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

"Peter Proost" wrote:
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
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"segue" <se***@discussions.microsoft.com> schreef in bericht
news:88**********************************@microsof t.com...

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


Nov 23 '05 #4

P: n/a
So how do I get the reorganized listbox1 items back to my original main form?

"Peter Proost" wrote:
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
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"segue" <se***@discussions.microsoft.com> schreef in bericht
news:88**********************************@microsof t.com...

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


Nov 23 '05 #5

P: n/a
By using a property or a reference

Greetz Peter
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"segue" <se***@discussions.microsoft.com> schreef in bericht
news:5B**********************************@microsof t.com...
So how do I get the reorganized listbox1 items back to my original main form?
"Peter Proost" wrote:
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
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"segue" <se***@discussions.microsoft.com> schreef in bericht
news:88**********************************@microsof t.com...

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


Nov 23 '05 #6

P: n/a
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

"Peter Proost" wrote:
By using a property or a reference

Greetz Peter
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"segue" <se***@discussions.microsoft.com> schreef in bericht
news:5B**********************************@microsof t.com...
So how do I get the reorganized listbox1 items back to my original main

form?

"Peter Proost" wrote:
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
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"segue" <se***@discussions.microsoft.com> schreef in bericht
news:88**********************************@microsof t.com...
>
> 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


Dec 11 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.