Click here to Skip to main content
15,896,726 members
Home / Discussions / C#
   

C#

 
AnswerRe: Match Two List Box By Sorting ... Pin
Luc Pattyn26-Nov-11 8:14
sitebuilderLuc Pattyn26-Nov-11 8:14 
GeneralRe: Match Two List Box By Sorting ... Pin
nassimnastaran26-Nov-11 8:42
nassimnastaran26-Nov-11 8:42 
AnswerRe: Match Two List Box By Sorting ... Pin
Luc Pattyn26-Nov-11 9:53
sitebuilderLuc Pattyn26-Nov-11 9:53 
GeneralRe: Match Two List Box By Sorting ... Pin
nassimnastaran26-Nov-11 10:22
nassimnastaran26-Nov-11 10:22 
GeneralRe: Match Two List Box By Sorting ... Pin
Luc Pattyn26-Nov-11 10:34
sitebuilderLuc Pattyn26-Nov-11 10:34 
GeneralRe: Match Two List Box By Sorting ... Pin
nassimnastaran26-Nov-11 10:43
nassimnastaran26-Nov-11 10:43 
AnswerRe: Match Two List Box By Sorting ... Pin
BillWoodruff26-Nov-11 19:55
professionalBillWoodruff26-Nov-11 19:55 
AnswerRe: Match Two List Box By Sorting ... Pin
DaveyM6926-Nov-11 22:48
professionalDaveyM6926-Nov-11 22:48 
There have been several things suggested but you don't seem to be understanding it, I'll try to help.

You seem to be getting confused about how much data you have. You only have ONE collection of cities, you just happen to be showing the individual bits of information (name, population) on two seperate controls. The first thing to do is to get your information into one City class and provide methods for sorting based on name or population - something like:
C#
public class City
{
    public const int MinPopulation = 0;

    public event EventHandler PopulationChanged;

    private string name;
    private int population;

    public City(string name, int population)
    {
        if (string.IsNullOrEmpty(name))
            throw new ArgumentNullException("name", "Name cannot be null or empty");
        // Do more validation here for only whitespace etc

        if (population < MinPopulation)
            throw new ArgumentOutOfRangeException(
                "population",
                string.Format("Population cannot be less than {0}", MinPopulation));
        this.name = name;
        this.population = population;
    }

    public string Name
    {
        get { return name; }
    }
    public int Population
    {
        get { return population; }
        set
        {
            if (value < MinPopulation)
                throw new ArgumentOutOfRangeException(
                    "Population",
                    string.Format("Population cannot be less than {0}", MinPopulation));
            population = value;
            OnPopulationChanged(EventArgs.Empty);
        }
    }

    protected virtual void OnPopulationChanged(EventArgs e)
    {
        EventHandler eh = PopulationChanged;
        if (eh != null)
            eh(this, e);
    }
    public static int CompareName(City first, City second)
    {
        if (object.ReferenceEquals(first, second)) // same reference or both null
            return 0;
        if (object.ReferenceEquals(first, null)) // first null?
            return -1;
        if (object.ReferenceEquals(second, null)) // second null?
            return 1;
        return first.name.CompareTo(second.name); // neither null and different reference so compare
    }
    public static int ComparePopulation(City first, City second)
    {
        if (object.ReferenceEquals(first, second)) // same reference or both null
            return 0;
        if (object.ReferenceEquals(first, null)) // first null?
            return -1;
        if (object.ReferenceEquals(second, null)) // second null?
            return 1;
        return first.population.CompareTo(second.population); // neither null and different reference so compare
    }
}

Now you can keep a collection of cities somewhere (perhaps a List<City>). You can then use that list as a common DataSource for both of your ListBoxes, using the DisplayMember property to choose which property to display. The rough code below needs a button adding buttonSort(with suitable width) for changing the sort.
C#
using System;
using System.Collections.Generic;
using System.Windows.Forms;

public partial class FormMain : Form
{
    private const string SortByNameText = "&Sort by Name";
    private const string SortByPopulationText = "&Sort by Population";

    private List<City> cities;

    public FormMain()
    {
        InitializeComponent();
        cities = new List<City>();
        BuildCityList();
        listBox1.DataSource = cities;
        listBox1.DisplayMember = ("Name");
        listBox2.DataSource = cities;
        listBox2.DisplayMember = ("Population");
        buttonSort.Text = "&Sort by Name";
        buttonSort.Click += new EventHandler(buttonSort_Click);
    }

    void buttonSort_Click(object sender, EventArgs e)
    {
        if (buttonSort.Text == SortByNameText)
        {
            cities.Sort(new Comparison<City>(City.CompareName));
            buttonSort.Text = SortByPopulationText;
        }
        else
        {
            cities.Sort(new Comparison<City>(City.ComparePopulation));
            buttonSort.Text = SortByNameText;
        }
        listBox1.DataSource = null;
        listBox2.DataSource = null;
        listBox1.DataSource = cities;
        listBox1.DisplayMember = ("Name");
        listBox2.DataSource = cities;
        listBox2.DisplayMember = ("Population");
    }

    private void BuildCityList()
    {
        cities.Add(new City("City One", 2500));
        cities.Add(new City("City Two", 3000));
        cities.Add(new City("City Three", 1500));
    }
}

Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



GeneralRe: Match Two List Box By Sorting ... Pin
nassimnastaran27-Nov-11 7:59
nassimnastaran27-Nov-11 7:59 
Questionthread resume with out using sleep method Pin
somasekhara77726-Nov-11 6:11
somasekhara77726-Nov-11 6:11 
AnswerRe: thread resume with out using sleep method Pin
Mark Salsbery26-Nov-11 6:28
Mark Salsbery26-Nov-11 6:28 
AnswerRe: thread resume with out using sleep method Pin
Luc Pattyn26-Nov-11 6:30
sitebuilderLuc Pattyn26-Nov-11 6:30 
QuestionscrollBars Pin
FM726-Nov-11 5:25
FM726-Nov-11 5:25 
AnswerRe: scrollBars Pin
Luc Pattyn26-Nov-11 5:33
sitebuilderLuc Pattyn26-Nov-11 5:33 
GeneralRe: scrollBars Pin
FM726-Nov-11 6:07
FM726-Nov-11 6:07 
GeneralRe: scrollBars Pin
BillWoodruff26-Nov-11 18:52
professionalBillWoodruff26-Nov-11 18:52 
GeneralRe: scrollBars Pin
Luc Pattyn27-Nov-11 1:04
sitebuilderLuc Pattyn27-Nov-11 1:04 
AnswerRe: scrollBars Pin
Jitendra Parida - Jeetu30-Nov-11 19:01
Jitendra Parida - Jeetu30-Nov-11 19:01 
QuestionOriginal datarow value. Pin
paper6726-Nov-11 5:11
paper6726-Nov-11 5:11 
QuestionAutomation InternetExplorer, i am getting crazy! Pin
Laaky25-Nov-11 23:03
Laaky25-Nov-11 23:03 
Questionhelp me: problem with remoting Library Pin
kornkimhour25-Nov-11 21:34
kornkimhour25-Nov-11 21:34 
AnswerRe: help me: problem with remoting Library Pin
Richard MacCutchan25-Nov-11 22:27
mveRichard MacCutchan25-Nov-11 22:27 
GeneralRe: help me: problem with remoting Library Pin
kornkimhour26-Nov-11 3:05
kornkimhour26-Nov-11 3:05 
GeneralRe: help me: problem with remoting Library Pin
Richard MacCutchan26-Nov-11 3:57
mveRichard MacCutchan26-Nov-11 3:57 
Questionhelp me: Problem connection string object and class object when passing cross exe C# Pin
kornkimhour25-Nov-11 15:24
kornkimhour25-Nov-11 15:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.