Results 1 to 13 of 13

Thread: [help]add number for ranking in listbox [emergency!]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Exclamation [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!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  5. #5
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    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
    Name:  RankContacts.jpg
Views: 172
Size:  39.5 KB

    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.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: [help]add number for ranking in listbox [emergency!]

    Quote Originally Posted by ChrisE View Post
    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
    Name:  RankContacts.jpg
Views: 172
Size:  39.5 KB

    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!

  7. #7
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: [help]add number for ranking in listbox [emergency!]

    Quote Originally Posted by danzey View Post
    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.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [help]add number for ranking in listbox [emergency!]

    Quote Originally Posted by danzey View Post
    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?

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: [help]add number for ranking in listbox [emergency!]

    Quote Originally Posted by jmcilhinney View Post
    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

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [help]add number for ranking in listbox [emergency!]

    Quote Originally Posted by danzey View Post
    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:
    1. myDataGridView.DataSource = Enumerable.Range(1, myArray.Length).
    2.                                        Select(Function(n) New With {.Rank = n, .Name = myArray(n - 1)}).
    3.                                        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.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: [help]add number for ranking in listbox [emergency!]

    ok thank you for the answer! i am going to try your solution!

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

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

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

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

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
  •  



Click Here to Expand Forum to Full Width