user7613510
user7613510

Reputation:

Adding sorted and unsorted integer to list box in C#

Within my system I have a text box and button that allows the user to input an integer to the list box. However, I want to include two radio buttons - sorted and unsorted, so that the user has to select whether they want the integer to be sorted or unsorted when added.

This is the code I have so far for the add button;

private void buttonAdd_Click(object sender, EventArgs e)
{
    listBoxAddedIntegers.Items.Add(textBoxInsert.Text);
    textBoxInsert.Text = string.Empty;
    //MessageBox.Show(listBoxAddedIntegers.SelectedIndex.ToString());
    MessageBox.Show(listBoxAddedIntegers.Items.Count.ToString());
}

this is the code for the radio button 'sorted;

private void radioButtonSorted_CheckedChanged(object sender, EventArgs e)
{
   radioButtonSorted.Checked = true;
}

and this is the code for the 'unsorted' radio button -

private void radioButtonUnsorted_CheckedChanged(object sender, EventArgs e)
{
   radioButtonSorted.Checked = false; 
}

Does anyone have any suggestions on how to add an integer to the list, so that when the user selects the radio button 'sorted' and then clicks add integer, the integer is then added to the sorted list? Thank you.

Upvotes: 0

Views: 373

Answers (2)

Cee McSharpface
Cee McSharpface

Reputation: 8726

Use your radio buttons to toggle the Sorted property of the listbox. According to documentation, it also ensures that

[as] items are added to a sorted ListBox, the items are moved to the appropriate location in the sorted list

So, you could write

private void radioButtonSorted_CheckedChanged(object sender, EventArgs e)
{
    if((sender as RadioButton).Checked) listBoxAddedIntegers.Sorted = true;
}

private void radioButtonUnsorted_CheckedChanged(object sender, EventArgs e)
{
    if((sender as RadioButton).Checked) listBoxAddedIntegers.Sorted = false; 
}

You used the CheckedChanged event. It will fire not only when a radio button is selected, it will fire also for the other one that was unselected. Therefore it is necessary to query the actual check state in the handler.

There is a shortcoming though: Sorted is limited to alphabetical order. If you get 1, 10, 2, 3 but expect 1, 2, 3, 10 then you can left-pad your integers with zeroes to get 0001, 0002, 0003, 0010; or apply a solution like this which pre-sorts the data and then refreshes the entire listbox.

Upvotes: 2

Vicky S
Vicky S

Reputation: 832

this will sort all the items from your list box if sorted is checked, still i don't think its necessary to have 2 redio buttons. you can replace that with single checkbox

                List<int> numbers = new List<int>();
                private void buttonAdd_Click(object sender, EventArgs e)
                {
                    if (radioButtonSorted.Checked)
                    {
                        numbers.Clear();

                        if (!string.IsNullOrEmpty(textBoxInsert.Text))
                            numbers.Add(int.Parse(textBoxInsert.Text));

                        foreach (var item in listBoxAddedIntegers.Items)
                        {
                            if (item != null)
                                numbers.Add(int.Parse(item.ToString()));
                        }

                        listBoxAddedIntegers.Items.Clear();
                        numbers.Sort();
                        foreach (var number in numbers)
                            listBoxAddedIntegers.Items.Add(number);
                    }
                    else
                        listBoxAddedIntegers.Items.Add(textBoxInsert.Text);

                    textBoxInsert.Text = string.Empty;
                    //MessageBox.Show(listBoxAddedIntegers.SelectedIndex.ToString());
                    MessageBox.Show(listBoxAddedIntegers.Items.Count.ToString());
                    }

Upvotes: 0

Related Questions