473,382 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

ListIndex.

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
Nov 21 '05 #1
5 7524
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
"Jonesgj" wrote:
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

Nov 21 '05 #2
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

"Jonesgj" <g@btinternet.com>
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

Nov 21 '05 #3
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

________________________________________________

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uR**************@TK2MSFTNGP12.phx.gbl...
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

"Jonesgj" <g@btinternet.com>
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


Nov 21 '05 #4
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

"Jonesgj" <g@btinternet.com>
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

________________________________________________

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uR**************@TK2MSFTNGP12.phx.gbl...
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

"Jonesgj" <g@btinternet.com>
> 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
>
>



Nov 21 '05 #5
Thanks Cor,

Yes, exactly what I needed - You're a star!
JonesGJ
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

"Jonesgj" <g@btinternet.com>
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

________________________________________________

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uR**************@TK2MSFTNGP12.phx.gbl...
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

"Jonesgj" <g@btinternet.com>

> 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
>
>



Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Josh Nikle | last post by:
I just realized that I posted this earlier with no title. Sorry. Here's the original post: I have a form with a combo box on it that, when a selection is made, fills in several other controls...
1
by: WEM | last post by:
I filling a combo box with all 50 states and when I select a customer from a list box I want to find the identity(listindex) in the combo box which is the identity stored in the customer record. If...
1
by: andrew.panin | last post by:
Hi there! I've got a trouble with these things. What's going on? 1ST STEP: we have ListBox item. Let's call it ListBox1. We're adding four values to it using AddItem method:...
2
by: rsteph | last post by:
I've got a form set up that has a series of textboxes to enter employee info, with a listbox at the bottom listing all the employees with their title (to make it easier to find employees to edit...
3
by: loisk | last post by:
Hi, Is there anyway you restrict your combo list on a certain condition? I'd like to enable only first row (index 0) when 'evertr' is 0. (My combo179 has 9 rows). Here's my code which is not...
3
by: juengelj | last post by:
I need a little advice here... Option Compare Database Global ind As integer Private Sub someobject_on click() ind = 5 End Sub
3
lotus18
by: lotus18 | last post by:
Hello World What is the equivalent of ListIndex property in vb .net? I just want to set the focus on the first items on the list. Rey Sean
10
by: Ken OHanlon | last post by:
I have read everything I can find on the internet, BUT it doesn't help! I'm using Windows XP and Access 2003. The following code does increment the listindex as wanted but then after coming back...
4
by: juing | last post by:
Hi All, I am very new in the VBA coding and I've noticed this (to me strange) behavior: Assume this code for "Value Items" type of single select two column ListBox: 1) If List98.ListIndex...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.