12 Replies - 2631 Views - Last Post: 09 September 2013 - 05:24 AM Rate Topic: -----

#1 Yianni   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-September 13

using a 2d array to sort list box items by number

Posted 06 September 2013 - 11:58 PM

I can add text box text to the list box and splitting each of the text box contents accross a row with the listbox every time the "add" button is pressed. What I want to do is sort the first entry in each the row which as a number (1-3).

Private Sub btnAddto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddto.Click
        Dim rdbval As String 'apartment size to be added to list box
            'checks which radio button is checked and assigns value to list box accordingly 
            If rdb1.Checked = True Then
                rdbval = "1"
            ElseIf rdb2.Checked = True Then
                rdbval = "2"
            ElseIf rdb3.Checked = True Then
                rdbval = "3"
        End If
        If Not lstbox.Items.Count > 20 Then 'check to see if listbox has reached max capacity of 20
            Dim arr As Array = Integer 
            arr(0) = "txtFullname.Text"

            lstbox.Items.Add(rdbval + " , " + txtFullname.Text + " , " + txtContact.Text + " , " + lblTime.Text + " , " + lblDate.Text + " , ") 'adds text boxes in a row within list box 
        Else
            MsgBox("List capacity reached") 'display this message if listbox has reached capacity 
        End If
            'once items are added to lsitbox, clears all textfields, sets radio buttons to false and resets the apartment size to be added 
            txtFullname.Clear()
            txtContact.Clear()
            rdb1.Checked = False
            rdb2.Checked = False
            rdb3.Checked = False
            rdbval = ""
    End Sub


Private Sub btnSize_Click(sender As System.Object, e As System.EventArgs) Handles btnSize.Click
        Dim swapped As Boolean = True
        'loop right through the array 
        Do Until swapped = False
            swapped = False
            For counter = 0 To myarray.Length - 2
                'you can choose any item on the strucure to sort by 
                If MyArray(counter).firstname > MyArray(counter + 1).firstname Then
                    'but this swaps the whole part of of the structure relevant to that student 
                    tempname = MyArray(counter + 1)
                    MyArray(counter) = tempname
                    swapped = True
                End If
            Next
        Loop
    End Sub

This post has been edited by andrewsw: 07 September 2013 - 03:46 AM
Reason for edit:: Use CODE tags


Is This A Good Question/Topic? 0
  • +

Replies To: using a 2d array to sort list box items by number

#2 manos364   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 45
  • Joined: 09-October 12

Re: using a 2d array to sort list box items by number

Posted 07 September 2013 - 02:45 AM

If i am right, you have for example a listbox that contains three items.
It' like this:

2. FullName , Contact , Time , Date
1. FullName , Contact , Time , Date
3. FullName , Contact , Time , Date

But the numbers are screwed so you want it to be like this?

1. FullName , Contact , Time , Date
2. FullName , Contact , Time , Date
3. FullName , Contact , Time , Date
Was This Post Helpful? 0
  • +
  • -

#3 Yianni   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-September 13

Re: using a 2d array to sort list box items by number

Posted 07 September 2013 - 04:00 AM

YES!, exactly, I need to sort the numbers in order from 1-3 while also moving the rest of the rows. Thanks for the prompt reply. I have tried multiple times and I just cant get it to work. The number (1-3) are from 3 radio buttons. depending on which one is click. that number will be added to the list along with other info from text boxes. so I dimmed rdbval globally as it will change every time.
Was This Post Helpful? 0
  • +
  • -

#4 Yianni   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-September 13

Re: using a 2d array to sort list box items by number

Posted 07 September 2013 - 04:05 AM

I tried to bubble sort it as well but cannot get it to work.
Was This Post Helpful? 0
  • +
  • -

#5 manos364   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 45
  • Joined: 09-October 12

Re: using a 2d array to sort list box items by number

Posted 07 September 2013 - 08:04 AM

I am not sure if i got what you mean BUT,
The code is this.
Dim Str1, Str2, Str3 As String
Str1 = ListBox1.Items.Item(0)
Str2 = ListBox1.Items.Item(1)
Str3 = ListBox1.Items.Item(2)

ListBox1.Items.Clear()

If Str1.StartsWith("1") Then
ListBox1.Items.Add(Str1)
End If

If Str2.StartsWith("1") Then
ListBox1.Items.Add(Str2)
End If

If Str3.StartsWith("1") Then
ListBox1.Items.Add(Str3)
End If

If Str1.StartsWith("2") Then
ListBox1.Items.Add(Str1)
End If

If Str2.StartsWith("2") Then
ListBox1.Items.Add(Str2)
End If

If Str3.StartsWith("2") Then
ListBox1.Items.Add(Str3)
End If

If Str1.StartsWith("3") Then
ListBox1.Items.Add(Str1)
End If

If Str2.StartsWith("3") Then
ListBox1.Items.Add(Str2)
End If

If Str3.StartsWith("3") Then
ListBox1.Items.Add(Str3)
End If



This code is a bit huge but is made without any editor. Sorry about that.

This will clear all items from the listbox and sort them from their starting number.
But also the Properties of the listbox i think contains a sort option. Try playing with that.
Was This Post Helpful? 0
  • +
  • -

#6 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: using a 2d array to sort list box items by number

Posted 07 September 2013 - 08:22 AM

in java.util.Arrays there is Sort method, check all overload and see what suits you.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,317
  • Joined: 12-December 12

Re: using a 2d array to sort list box items by number

Posted 07 September 2013 - 08:38 AM

View PostMichael26, on 07 September 2013 - 03:22 PM, said:

in java.util.Arrays there is Sort method, check all overload and see what suits you.

This is a VB.NET question.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,317
  • Joined: 12-December 12

Re: using a 2d array to sort list box items by number

Posted 07 September 2013 - 08:51 AM

If there are less than 10 items then

lstbox.Sorted = True

would sort them, and keep them sorted.

If there are more than 9 (your code suggests up-to 20) then you might pad the numbers with a leading 0, then the above would still work.

If you want to sort by, for example, the 1st column then the 3rd, then this is a little more complicated (but still possible). A simple approach would be to re-order the information added to each item.

This post has been edited by andrewsw: 07 September 2013 - 08:58 AM

Was This Post Helpful? 0
  • +
  • -

#9 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: using a 2d array to sort list box items by number

Posted 07 September 2013 - 10:27 AM

View Postandrewsw, on 07 September 2013 - 05:38 PM, said:

View PostMichael26, on 07 September 2013 - 03:22 PM, said:

in java.util.Arrays there is Sort method, check all overload and see what suits you.

This is a VB.NET question.

For some reason i though this is java.
I clearly missed something :)
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,317
  • Joined: 12-December 12

Re: using a 2d array to sort list box items by number

Posted 07 September 2013 - 10:41 AM

@Michael I've been caught out a few times myself ;) (have to scroll to the top to see what I'm doing).
Was This Post Helpful? 0
  • +
  • -

#11 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,640
  • Joined: 07-September 13

Re: using a 2d array to sort list box items by number

Posted 07 September 2013 - 11:01 AM

with things like this i find inheritance a real god send, you can also then use linq to sort them.

i don't know if this is going to be helpful but it kept me entertained for 5 minutes.

' -- Create a new listitem class and inherit listviewitem

Public Class ListitemExtended
    Inherits ListViewItem

    ' Add a few extra properties that you want it to store 

    Private Size As Integer
    Public Property ApartmentSize As Integer
        Get
            Return Size
        End Get
        Set(ByVal value As Integer)
            Size = value
        End Set
    End Property

    Private fullName As String
    Public Property FullCustomerName() As String
        Get
            Return FullName
        End Get
        Set(ByVal value As String)
            fullName = value
        End Set
    End Property

    Private Contact As String
    Public Property ContactNumber() As String
        Get
            Return Contact
        End Get
        Set(ByVal value As String)
            Contact = value
        End Set
    End Property

    Private time As DateTime
    Public Property TimeAdded() As String
        Get
            Return time
        End Get
        Set(ByVal value As String)
            time = value
        End Set
    End Property

    Private DateAdd As Date
    Public Property DateAdded As Date
        Get
            Return DateAdd
        End Get
        Set(ByVal value As Date)
            DateAdd = value
        End Set
    End Property


    ' Over write the tostring 
    ' -- when you over write the tostring you have to remember this is the text that will show in the listitem. 
    Public Overrides Function ToString() As String
        ' So returning it how you want will be
        '  rdbval + " , " + txtFullname.Text + " , " + txtContact.Text + " , " + lblTime.Text + " , " + lblDate.Text + " , "

        Return MyBase.ToString(String.Format("{0}, {1}, {2}, {3}, {4}", ApartmentSize, FullCustomerName, ContactNumber, TimeAdded, DateAdded))

    End Function

End Class



' -- When you go to add a new item try and use this code instead

        ' create the listitem 
        Dim listItem As New ListitemExtended

        If rdb1.Checked = True Then
            listItem.ApartmentSize = 1
        ElseIf rdb2.Checked = True Then
            listItem.ApartmentSize = 2
        ElseIf rdb3.Checked = True Then
            listItem.ApartmentSize = 3
        End If

        listItem.FullCustomerName = txtFullname.text
        listItem.ContactNumber = txtContact.text

        ' add it to the listbox 
        lstbox.items.add(listItem)

        ' resort the list 
        Dim list = From listtem As ListitemExtended In lstbox.Items
                   Order By listtem.ApartmentSize Ascending
                   Select listtem

        ' clear the listbox 
        lstbox.Items.Clear()

        ' reinsert each item in the list
        For Each item As ListitemExtended In list
            lstbox.Items.Add(item)
        Next



It isn't completely efficient but if your only holding 20 items it would be fine.

You will have to add in your max listview bit again but that wont be hard.

This is also untested for your example i had a very similar thing that i altered to show you this.
Was This Post Helpful? 1
  • +
  • -

#12 Yianni   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-September 13

Re: using a 2d array to sort list box items by number

Posted 07 September 2013 - 04:44 PM

Thanks everyone for the replies, at least now I have something to work with from guys who know what they are doing! Glad I signed up to this forum.
Was This Post Helpful? 0
  • +
  • -

#13 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,640
  • Joined: 07-September 13

Re: using a 2d array to sort list box items by number

Posted 09 September 2013 - 05:24 AM

no problem, remember to upvote people who have helped you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1