[help]add number for ranking in listbox [emergency!]
after days of research, I am still going to you to help me. I would like to add ranking numbers to a listbox for each item like this:
1 antoine
2 bob
3 peter
4 franck
5 lisa
i don't know to manipulate the column with listbox. It is very important that there is a separation between the number of ranking and the items already classified . or have a code like this:
column 1(*ranking) column 2 (*from a another listbox already order)
1 | antoine
2 | bob
3 | peter
4 | frank
5 | lisa
the numbers(column 1) must match with numbers of items in lisbox (column2) (if possible)
in resume, i need the number ranking please.
thank you in advance for your answer!
Re: [help]add number for ranking in listbox [emergency!]
For starters, ListBoxes don't have columns by default. Also, you're not making it clear how you decide the ranking.
I can help you, but you need to explain clearly.
Re: [help]add number for ranking in listbox [emergency!]
To enable columns in a ListBox...
Code:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")> _
Private Shared Function SendMessage( _
ByVal hWnd As IntPtr, _
ByVal wMsg As Integer, _
ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As Integer
End Function
Private Sub SetTabStops(ByVal listBox As ListBox, ByVal tabStops() As Integer)
Const LB_SETTABSTOPS As Integer = &H192
Dim pinnedValues As GCHandle
pinnedValues = GCHandle.Alloc(tabStops, GCHandleType.Pinned)
Dim ptr As IntPtr = pinnedValues.AddrOfPinnedObject()
SendMessage(listBox.Handle, LB_SETTABSTOPS, New IntPtr(tabStops.Length), ptr)
pinnedValues.Free()
listBox.Refresh()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetTabStops(ListBox1, New Integer() {30})
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Add("1" & vbTab & "antoine")
ListBox1.Items.Add("2" & vbTab & "bob")
ListBox1.Items.Add("3" & vbTab & "peter")
ListBox1.Items.Add("4" & vbTab & "franck")
ListBox1.Items.Add("5" & vbTab & "lisa")
End Sub
End Class
Re: [help]add number for ranking in listbox [emergency!]
Why not just use a ListView or DataGridView, which are controls designed to have columns in the first place. Even .paul.'s code is still just a simulation of columns. With actual columns then you won't need Win API calls.
1 Attachment(s)
Re: [help]add number for ranking in listbox [emergency!]
where does the Data come from ?
if it's a Database you can execute within the query a running number.
here a sample to add such a number to the Suppliers table
they are sorted by the ContactName
here an image of the results
Attachment 177539
and the query within the Access Database
Code:
SELECT Suppliers.ContactName, (Select Count (*) From [Suppliers] As X
Where [X].[ContactName] < [Suppliers].[ContactName])+1 AS Co
FROM Suppliers
ORDER BY Suppliers.ContactName;
hth
Re: [help]add number for ranking in listbox [emergency!]
Quote:
Originally Posted by
ChrisE
where does the Data come from ?
if it's a Database you can execute within the query a running number.
here a sample to add such a number to the Suppliers table
they are sorted by the ContactName
here an image of the results
Attachment 177539
and the query within the Access Database
Code:
SELECT Suppliers.ContactName, (Select Count (*) From [Suppliers] As X
Where [X].[ContactName] < [Suppliers].[ContactName])+1 AS Co
FROM Suppliers
ORDER BY Suppliers.ContactName;
hth
yes it is like this exept i would like the ranking number at left side!
my database come from a listbox.
thank you very much for your help!
Re: [help]add number for ranking in listbox [emergency!]
Quote:
Originally Posted by
danzey
yes it is like this exept i would like the ranking number at left side!
my database come from a listbox.
thank you very much for your help!
see Post#4
and load the columns in the order you want
Re: [help]add number for ranking in listbox [emergency!]
Quote:
Originally Posted by
danzey
my database come from a listbox.
That's a nonsensical statement. You are currently display your data in a ListBox but that data must come from somewhere. Does it come from a database? If not, where does it come from and how?
Re: [help]add number for ranking in listbox [emergency!]
Quote:
Originally Posted by
jmcilhinney
That's a nonsensical statement. You are currently display your data in a ListBox but that data must come from somewhere. Does it come from a database? If not, where does it come from and how?
i mean it is from a array and then i put it in a listbox for sorting.sorry
Re: [help]add number for ranking in listbox [emergency!]
Quote:
Originally Posted by
danzey
i mean it is from a array and then i put it in a listbox for sorting.sorry
If it's an array then there's no SQL code but you can just load the data into any control you want. You could load it into a ListView or DataGridView manually or you could load it into another appropriate data structure and bind it to a DataGridView, e.g.
vb.net Code:
myDataGridView.DataSource = Enumerable.Range(1, myArray.Length).
Select(Function(n) New With {.Rank = n, .Name = myArray(n - 1)}).
ToArray()
That creates an array of objects that each have Rank and Name properties and binds it to a DataGridView, which will then display those properties in two separate columns.
Re: [help]add number for ranking in listbox [emergency!]
ok thank you for the answer! i am going to try your solution!
Re: [help]add number for ranking in listbox [emergency!]
There’s no need to add an array to a listbox for sorting. The array has a sort method itself...
Re: [help]add number for ranking in listbox [emergency!]
Using the ListBox method I posted in post #3...
Code:
myArray.Sort
ListBox1.Items.AddRange(Enumerable.Range(1, myArray.Length).Select(Function(n) n.ToString & vbTab & myArray(n-1)).ToArray())