9 Replies - 4259 Views - Last Post: 02 May 2011 - 02:52 PM Rate Topic: -----

#1 lumineon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-May 11

How can I sort the bindinglist?

Posted 02 May 2011 - 10:09 AM

Pretty new to all this, doing a project for my course.
I created a contact list application that loads a .txt file into a combobox.
I need to then sort what's in the combobox, but I can't.

Any help would be amazing.

Imports System.ComponentModel
Imports System.IO

Public Class Form1

    Private DG_contactsBlist As BindingList(Of String)

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim DG_contacts As New List(Of String)

        DG_contacts.AddRange(IO.File.ReadAllLines("C:\Users\Daniel\Desktop\my_contacts.txt"))
        Me.DG_contactsBlist = New BindingList(Of String)(DG_contacts)
        Me.contactlistbox.DataSource = Me.DG_contactsBlist

    End Sub

    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        IO.File.WriteAllLines("C:\Users\Daniel\Desktop\my_contacts.txt", Me.DG_contactsBlist.ToArray())
        MessageBox.Show("Contact List has been saved", "Contact List")
    End Sub

    Private Sub btnaddnew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddnew.Click
        Dim DG_addname As String
        DG_addname = txtaddcontact.Text
        If String.IsNullOrEmpty(txtaddcontact.Text) Then
            MessageBox.Show("Please Enter a Valid Name", "Name Input")
        Else
            Me.DG_contactsBlist.Add(DG_addname)
        End If
    End Sub

    Private Sub btnsort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsort.Click
        '???
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: How can I sort the bindinglist?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8377
  • View blog
  • Posts: 31,138
  • Joined: 12-June 08

Re: How can I sort the bindinglist?

Posted 02 May 2011 - 10:40 AM

How about loading your rows into a collection of some sort. Array List? Generic List<of? A dataset? Do your sort on that then add your sorted list to the combo box?
Was This Post Helpful? 0
  • +
  • -

#3 lumineon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-May 11

Re: How can I sort the bindinglist?

Posted 02 May 2011 - 10:48 AM

View Postmodi123_1, on 02 May 2011 - 10:40 AM, said:

How about loading your rows into a collection of some sort. Array List? Generic List<of? A dataset? Do your sort on that then add your sorted list to the combo box?


The list would then be sorted from the start wouldn't it?
I need to be able to add new names to the contact list and then have those sorted with the ones that were already there.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8377
  • View blog
  • Posts: 31,138
  • Joined: 12-June 08

Re: How can I sort the bindinglist?

Posted 02 May 2011 - 11:15 AM

Better look into 'data source' properties of your list box! Have a static datasouce you would add to (once sorted).
Was This Post Helpful? 0
  • +
  • -

#5 lumineon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-May 11

Re: How can I sort the bindinglist?

Posted 02 May 2011 - 01:44 PM

View Postmodi123_1, on 02 May 2011 - 11:15 AM, said:

Better look into 'data source' properties of your list box! Have a static datasouce you would add to (once sorted).


Any advice on how I would do that?
Thanks
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8377
  • View blog
  • Posts: 31,138
  • Joined: 12-June 08

Re: How can I sort the bindinglist?

Posted 02 May 2011 - 01:48 PM

In the easiest way possible - through reading the help documentation!
http://msdn.microsof...v=VS.90%29.aspx
Was This Post Helpful? 0
  • +
  • -

#7 lumineon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-May 11

Re: How can I sort the bindinglist?

Posted 02 May 2011 - 02:04 PM

Thanks man, but I still don't get it.
I'll keep on searching.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8377
  • View blog
  • Posts: 31,138
  • Joined: 12-June 08

Re: How can I sort the bindinglist?

Posted 02 May 2011 - 02:20 PM

What don't you get?

Datasource concepts?

http://msdn.microsof...v=VS.90%29.aspx
Was This Post Helpful? 0
  • +
  • -

#9 lumineon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-May 11

Re: How can I sort the bindinglist?

Posted 02 May 2011 - 02:48 PM

I don't understand where I'm supposed to apply the sort function.

I add my .txt file to the DG_contacts list variable, then turn the list variable into a binding list. The binding list is what becomes the datasource for the combobox?

Is that correct?

When it comes to sorting, what is it that I sort? The binding list? and then, when is the sort carried out?
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8377
  • View blog
  • Posts: 31,138
  • Joined: 12-June 08

Re: How can I sort the bindinglist?

Posted 02 May 2011 - 02:52 PM

You sort what ever you decide to use for a datasource.

Let's say ou use an arraylist.

1. Take each item from the file and put it into this array list
2. Sort the array list
3. Set the datasource of the control to the array list.



right now you are:
1. Load item from text file.
2. Insert into control's item array.
3. Repeat


See the difference? See where the sort would occur?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1