0

Explaning this WindowsFormAplication: I have two buttons one is for inserting numbers in listbox1 with textbox and that works fine ,second button is for sorting the elements from listbox1(numbers) as int or array to listbox2.It needs to be int or array because if i sort it as string then if i have for example 3,2,10 as my elements in listbox its going to sort it as 10,2,3 because its string, Now i have this code in my sort button where i made list from listbox elements and sorted it as string and dont know how to convert the list to array or int:

private void button2_Click(object sender, EventArgs e)
    {
        List<String> lista = new List<string>();

        foreach (String x in listBox1.Items)
        {
            lista.Add(x);
        }
        lista.Sort();
        foreach (string a in lista)
        {
            listBox2.Items.Add(a);
        }
    }
1
  • just solved it there is the code below Commented Oct 26, 2017 at 22:33

3 Answers 3

1

You can pass a Comparison delegate to your lista.Sort() method that will cast the items to ints and sort them by their numeric value. Like this:

lista.Sort((a, b) =>
{
    return Convert.ToInt32(a).CompareTo(Convert.ToInt32(b));
});

Note this does no checking that the conversion is valid or anything like that.

Sign up to request clarification or add additional context in comments.

Comments

0

I found the solution myself so this is the code:

private void button2_Click(object sender, EventArgs e)
    {
        List<int> lista = new List<int>();

        foreach (string x in listBox1.Items)
        {
            lista.Add(Convert.ToInt32(x));
        }
        lista.Sort();
        foreach (int a in lista)
        {
            listBox2.Items.Add(a);
        }
    }

I hope someone finds this helpful.

Comments

0

A ListBox accepts elements of any type, not just strings. So, you can add ints directly. There is no need to convert to string. You can then retrieve ints again.

However, I would store the values in a list and not in the items collection of the listbox.

private List<int> numbers = new List<int>();

private void btnAdd_Click(object sender, EventArgs e)
{
    if(Int32.TryParse(TextBox1.Text, out int n)) {
        numbers.Add(n);
        listBox1.DataSource = null;
        listBox1.DataSource = numbers;
    } else {
        MsgBox.Show("You must enter an integer!");
    }
}

private void btnSort_Click(object sender, EventArgs e)
{
    numbers.Sort();
    listBox1.DataSource = null;
    listBox1.DataSource = numbers;
}

Setting the DataSource of the listbox does not insert the items into the listbox, but tells the listbox to display the elements from your collection instead of its own internal collection.

If you assign the same list twice, the listbox won't notice that the contents of the list changed. Therefore, assign null first.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.