Results 1 to 12 of 12

Thread: Sort a listbox

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Sort a listbox

    Hello guys!
    I have a problem to sort the results in a listbox.
    Here is an example of what I mean


    Code:
    dim l as integer 
    dim m as integer 
    dim x as integer 'input value for loop
    dim y as integer 'input value for loop
    dim z as double
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    For Me.L = 1 To x ' user gives the x
    For Me.M = 1 To y ' user gives the y
    z = l*M
    ListBox1.Items.Add("(" & L & "," & M & ")= " & z)
    Next M
    Next L
    
    Exit Sub
    I am trying to find a way to sort the results in columns in this way:
    When the first 'L' loop ends then go to next column of the listbox.
    Let's say the user gives x=5 and y=8. I would like to have a listbox
    like this one in the attachment.
    I have tried the listbox proporties ''Horizontal Scrollbar''
    and ''MultiColumn'' but it didn't help... The sorting depends
    on ListBox Height-Width. Any ideas?
    Attached Images Attached Images  

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

    Re: Sort a listbox

    ListBoxes don't support data with multiple columns. Setting MultiColumn to True simply means that the items scroll horizontally rather than vertically. It's still just a single list of simple items. If you want to display tabular data, i.e. multiple columns per item, then use a ListView or, preferably, a DataGridView.

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: Sort a listbox

    I didn't know that.. So, how is it going in a ListViewor DataGridView the above code?
    Last edited by stratos; May 3rd, 2014 at 03:03 PM.

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

    Re: Sort a listbox

    Quote Originally Posted by stratos View Post
    I didn't know that.. So, how is it going in a ListViewor DataGridView the above code?
    What do you not understand about what you found out for yourself when you researched it? I'm happy to help with the things that you can't do despite trying, not do things so that you don't have to try. There is a lot of information out there about using a DataGridView. Give it a go. If you have trouble, start a new thread for that, show us what you did and tell us what happened and we can help with that specifically.

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: Sort a listbox

    Well I tried a ListView but still no luck. Data are displayed in a single Column, although I managed to add new columns...

    Code:
    dim l as integer 
    dim m as integer 
    dim x as integer 'input value for loop
    dim y as integer 'input value for loop
    dim z as double
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
     ListView1.View = View.Details
    
    For Me.L = 1 To x ' user gives the x
    ListView1.Columns.Add("(L,M)")
    For Me.M = 1 To y ' user gives the y
    z = L*M
    ListView1.Items.Add("(" & L & "," & M & ")= " & z)
    Next M
    Next L
    
    Exit Sub
    I have also changed some the ListView properties
    MultiSelect:False
    FullRowSelect: True
    Last edited by stratos; May 4th, 2014 at 07:06 AM.

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

    Re: Sort a listbox

    If you want data to appear in columns other than the first then you have to add subitems to the item. Did you read the MSDN documentation for the ListView class? It includes a code example so there's really no excuse for not being able to populate one. I still recommend the DataGridView as a better option though.

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: Sort a listbox

    I think that I am missing something since I can't make it...
    I have read some tutorials. The problem is the the adding method is done by ''row''. I want to be done by ''column''.
    I have tried a datagridview as you suggested and I managed to add data in several rows, but I didn't want that.
    As you see the code below, I would like a method to add data by column. I don't know if I am comprehensible.

    Code:
    dim x as integer 'input value for loop
    dim y as integer 'input value for loop
    dim z as double
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    
    DataGridView1.ColumnCount = 6  'ADD 6 COLUMNS
    
    For Me.x = 1 To 6
    
                For Me.y = 1 To 5
                    z = x * y
                    DataGridView1.Rows.Add(New String() {z.ToString})
                Next y 
    ' ---> I NEED CODE WHERE THE 'y' LOOP END, GO TO NEXT 'x' COLUMN AND ADD DATA
    
    Next x
    End Sub

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

    Re: Sort a listbox

    Perhaps you should explain exactly what the data represents and on what basis exactly that you want it arranged.

  9. #9

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: Sort a listbox

    I am trying to do a ''What-If'' analysis such as in excel files.
    This is a case in economic field, it's a type of loan.
    So, I am trying to find what happens to payment if rate or the repayment date of the loan change.

    So, user gives three input variables:
    Loan (L), which is a constant number
    Rate (R), which is an array, since it takes various values R(r_min,r_max). User gives r_min, r_max
    Periods-Repayment date (N), which is also an array, N(n_min,n_max). User gives n_min, n_max.
    Red and Blue values are the input variables (Loan, Rate, Periods). Green values is the ouput variables (Payment). (See attachment.)

    So far, I have programmed it well. The issue is that I can't represent the output values like the
    attachment below. I need a code to do this: When the first column ends, go to next column and add data.
    I have added a DataGridView control in the project for that case.

    Maybe I use a wrong vb command (see above posts). I have seen also the command
    "DataGridView1.Rows(x).Cells(y).Value = "number", which is add in a Cell('Row','Column')=Cell(x,y) the desired value.
    I have take a look at this, but It doesn't work with the For-Next Loop, because I have a problem to express the Rows,Columns of the DataGrid. When I use a specific Cell, i.e "DataGridView1.Rows(1).Cells(2).Value = 10, then everytning is fine

    Name:  analysis.jpg
Views: 327
Size:  38.3 KB

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Sort a listbox

    There is a general convention in programs that tabular data has fields as the columns, and each row is a set of values for these fields. In other words, data is generally added by row. You have seen this in the Listview and the DGV, as both of them are really designed primarily for this kind of mapping. With the DGV, you can add data to individual cells, so you can certainly add by column or by row, though the DGV makes it more natural to add by row rather than by column.

    In your case, it may make more sense to you to add the data column by column, but you don't have to do it that way from what I can see. It looks like you know the number of columns that will be in the final grid before you start putting any data in the grid. Therefore, you can add the number of columns you need, and the number of rows you need, then populate them by row, by column, or by darn near random. The issue isn't with the grid or the means to access it, both of which you have. The issue is with the For...Next loop you are using to populate the grid.

    A loop of that nature is probably going to run from 0 to N, or 1 to N. This is a single value, though. The problem is to translate that single value into a row, column in the DGV. That's not difficult, though it might be a bit new to you. The key is whether you are going to go by columns or by rows. Assuming you are going to fill column 1 first, then column 2, then column 3, and asssuming that the picture is correct, the basic design is:

    The first 9 items go into column 1 row 1 - 9
    The next 9 items go into column 2 row 1 - 9
    etc.

    Assuming that the loop begins with 0, the loop would look something like this:

    Code:
    For x = 0 to 53
     Dim row As Integer = x Mod 9
     Dim col As Integer = x \ 9 'Note that this is integer division, not the more common /.
    
     yourDGV.Rows(row).Cells(col).value = whatever
    Next
    You can do that in one line, too, if you don't want the row and col variables, but I felt those might be more clear.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: Sort a listbox

    Hey!
    For some how I manage to add data in all cells using a routine. The problem is that I can't add the shole data to DataGridView

    Here is an example

    Code:
    Public Class Form1
    
    'I only have one button and a DataGridView
    
    Dim x As Double
    Dim y As Double
    Dim z As Double
    Dim row As Integer = 4
    Dim col As Integer = 5
    
    
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    'Generate z number using the FOR LOOP
    
    For Me.x = 1 To 5
    For Me.y = 1 To 4
    z = x * y
    Next y
    Next x
    
    ' GENERATE COLUMNS,ROWS OF DATAGRIDVIEW
    
    DataGridView2.ColumnCount = 5
    DataGridView2.RowCount = 4
    
    
    For Me.col = 0 To 4
    For Me.row = 0 To 3
    DataGridView2.Rows(row).Cells(col).Value = z   '--> HERE IS THE PROBLEM
    Next
    Next
    
    End Sub
    End Class
    In DATA GRID VIEW it is appeared only the last value of z (z=20). I have the represenation I want (see above image) with this kind of code but data are missed. I think that I have to store somewhere the z-values and then add them to DataGridView
    I suppose also that I have some problem with declarations of variables and some other things that I can't figure out....
    Any help?

  12. #12

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: Sort a listbox

    Hey!
    For some how I manage to add data in all cells using a routine. The problem is that I can't add the shole data to DataGridView

    Here is an example

    Code:
    Public Class Form1
    
    'I only have one button and a DataGridView
    
    Dim x As Double
    Dim y As Double
    Dim z As Double
    Dim row As Integer = 4
    Dim col As Integer = 5
    
    
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    'Generate z number using the FOR LOOP
    
    For Me.x = 1 To 5
    For Me.y = 1 To 4
    z = x * y
    Next y
    Next x
    
    ' GENERATE COLUMNS,ROWS OF DATAGRIDVIEW
    
    DataGridView2.ColumnCount = 5
    DataGridView2.RowCount = 4
    
    
    For Me.col = 0 To 4
     For Me.row = 0 To 3
    DataGridView2.Rows(row).Cells(col).Value = z   '--> HERE IS THE PROBLEM
    Next
    Next
    
    End Sub
    End Class
    In DATA GRID VIEW it is appeared only the last value of z (z=20). I have the represenation I want (see above image) with this kind of code but data are missed. I think that I have to store somewhere the z-values and then add them to DataGridView
    I suppose also that I have some problem with declarations of variables and some other things that I can't figure out.... Unless there is a way to move data from one datagridview to another...
    Any help?

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