ListIndex.

J

Jonesgj

Hi,

I have loaded a listbox with items from a database. I now want to allow the
user to re-order the list box manually using up down buttons, before
submitting his changes to back to the database.

I did this sometime ago in classic vb, and I think I used the Listindex
property to check ... anyway, can anyone help?

Thanks in anticipation


JonesGJ
 
G

Guest

A DataView would be a good choice for the list's datasource. In case you
have not used them, if you create a Datatable, you can create a DataView as
follows:
dim DT as new datatable
....fill datatable
Dim DV as new Dataview(DT)

Be sure the Datatable has a Sort field. As you probably already know, when
you bind a List control to a datasource, you set the DisplayMember, the
ValueMember if applicable, and the Datasource.

When you click MoveUp or MoveDown button, use the SelectedIndex property of
the ListBox to identify the row of the ListView. Hold the Sort field item
for that row in a variable. Depending on the direction of the move, get the
Sort field item for the adjacent row and exchange the values.

Call ListView.sort = YourSortField
 
C

Cor Ligthert

Jonesgj,

I assume that you use a windowform.

Then the place in the listbox says nothing about a place in a database.

Assuming you use the datasource only a reference to a row in a datatable,
which is itself unordered.

So what is it you want to archieve

Cor
 
J

Jonesgj

Sorry Cor,

I was a bit vague. Since I wrote this I found my old test code I wrote in
VB6. I ran it through the upgrade wizard and it produce the following code
(aimple form, listbox and two commend buttons) This should
explain/demonstrate what I am trying to do. I could implement this as it
stands, it works, but it needs reference to the VB6 Compatibility. However,
I would like to do it completely in .NET. Does this make sense?


_______________________________________________________________________

Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles Command1.Click


Dim lposition As Short
Dim lposition2 As Short
Dim My_string1 As String
Dim My_string2 As String

'gj - to shuffle position of selected item.


'find position
lposition = List3.SelectedIndex
'find if already top of list
If lposition = 0 Then Exit Sub


lposition2 = (List3.SelectedIndex) - 1

My_string1 = VB6.GetItemString(List3, lposition)
My_string2 = VB6.GetItemString(List3, lposition2)

'find if list if empty
If My_string1 = "" Then Exit Sub



VB6.SetItemString(List3, lposition2, My_string1)
VB6.SetItemString(List3, lposition, My_string2)

List3.SelectedIndex = (List3.SelectedIndex) - 1

End Sub

Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles Command2.Click
Dim lposition As Short
Dim lposition2 As Short
Dim My_string1 As String
Dim My_string2 As String

'gj - to shuffle position of selected item.


'find position
lposition = List3.SelectedIndex
'find if already end of list
If lposition = (List3.Items.Count - 1) Then Exit Sub


lposition2 = (List3.SelectedIndex) + 1

My_string1 = VB6.GetItemString(List3, lposition)
My_string2 = VB6.GetItemString(List3, lposition2)

'find if list if empty
If My_string1 = "" Then Exit Sub



VB6.SetItemString(List3, lposition2, My_string1)
VB6.SetItemString(List3, lposition, My_string2)

List3.SelectedIndex = (List3.SelectedIndex) + 1

End Sub

Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs
As System.EventArgs) Handles MyBase.Load
List3.Items.Add("Blue")
List3.Items.Add("Red")
List3.Items.Add("Green")
List3.Items.Add("Pink")
List3.Items.Add("Orange")
List3.Items.Add("Maroon")

End Sub

________________________________________________
 
C

Cor Ligthert

Jonesgj,

Did you mean this?

\\\
Private Sub Command1_Click(ByVal eventSender As System.Object, _
ByVal eventArgs As System.EventArgs) Handles Command1.Click
If List3.SelectedIndex > 0 Then
List3.Items.Insert(List3.SelectedIndex - 1, List3.SelectedItem)
Dim newindex As Integer = List3.SelectedIndex - 2
List3.Items.RemoveAt(List3.SelectedIndex)
List3.SelectedIndex = newindex
End If
End Sub
Private Sub Command2_Click(ByVal eventSender As System.Object, _
ByVal eventArgs As System.EventArgs) Handles Command2.Click
If List3.SelectedIndex < List3.Items.Count - 1 AndAlso _
List3.SelectedIndex <> -1 Then
List3.Items.Insert(List3.SelectedIndex + 2, List3.SelectedItem)
Dim newindex As Integer = List3.SelectedIndex + 1
List3.Items.RemoveAt(List3.SelectedIndex)
List3.SelectedIndex = newindex
End If
End Sub
Private Sub Form1_Load(ByVal eventSender As System.Object, _
ByVal eventArgs As System.EventArgs) Handles MyBase.Load
List3.Items.Add("Blue")
List3.Items.Add("Red")
List3.Items.Add("Green")
List3.Items.Add("Pink")
List3.Items.Add("Orange")
List3.Items.Add("Maroon")
End Sub
///

As addition to this, you would in my opinion try to avoid Short in VBNet,
Integer is the format that is used by the processor and therefore will give
you the most performance. Saving two bytes will probably cost you more in
the setting to 32 bits.

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top