-
Jun 7th, 2020, 07:23 PM
#1
Thread Starter
Lively Member
[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!
-
Jun 7th, 2020, 10:07 PM
#2
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 7th, 2020, 10:15 PM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 8th, 2020, 12:07 AM
#4
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.
-
Jun 8th, 2020, 03:51 AM
#5
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
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
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Jun 8th, 2020, 04:21 AM
#6
Thread Starter
Lively Member
Re: [help]add number for ranking in listbox [emergency!]
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
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!
-
Jun 8th, 2020, 04:25 AM
#7
Re: [help]add number for ranking in listbox [emergency!]
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
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Jun 8th, 2020, 05:44 AM
#8
Re: [help]add number for ranking in listbox [emergency!]
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?
-
Jun 8th, 2020, 05:48 AM
#9
Thread Starter
Lively Member
Re: [help]add number for ranking in listbox [emergency!]
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
-
Jun 8th, 2020, 06:20 AM
#10
Re: [help]add number for ranking in listbox [emergency!]
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.
-
Jun 8th, 2020, 06:38 AM
#11
Thread Starter
Lively Member
Re: [help]add number for ranking in listbox [emergency!]
ok thank you for the answer! i am going to try your solution!
-
Jun 8th, 2020, 11:15 AM
#12
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 8th, 2020, 11:37 AM
#13
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())
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|