0

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.

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • 1
    your question is unclear, do you have 2 listboxes as sorted, unsorted or do you want to implement the functionalities with same listbox as if sorted is true then the listbox items to be sorted otherwise should add the user entry as it is. – Vicky S Dec 08 '17 at 10:36
  • yes, @VickyS is right, I'm don't understand if you need to insert new value in correct place, or you need to sort whole list when value is inserting and `RadioButtonSorted.Checked` is `true` – V. Panchenko Dec 08 '17 at 10:40
  • if this is Windows Forms, then the mutual deselecting of radio buttons is unnecessary in the `CheckedChanged` event handlers. Unique selection is automatically enforced for radio buttons that belong to the same group. – Cee McSharpface Dec 08 '17 at 10:41
  • 1
    If the listbox has the values 2, 4, 1 and you want to add another integer, 3 as a sorted integer, where would you put it? Please can you make your questions clearer. – Rob Anthony Dec 08 '17 at 10:42
  • I think he should use `CheckBox` instead of two `RadioButton`s – V. Panchenko Dec 08 '17 at 10:42
  • I’m just using one list box. For example my list box now contains integers 2,3,4,5. When the user wants to input another integer e.g. 6, they have to input 6 into the text box, select the radio button sorted, and then click add. If this explanation is more clear :) –  Dec 08 '17 at 10:56
  • can you please try the answer what i have added and add a comment was that the thing you require – Vicky S Dec 08 '17 at 10:59

2 Answers2

2

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.

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
0

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());
                    }
Vicky S
  • 762
  • 4
  • 16
  • thank you so much. this has really helped. one thing though, with the line numbers.Add(int.Parse(textBoxInsert.Text)); the error message is coming up - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format. Do you have any suggestions as to why this is? –  Dec 08 '17 at 12:29
  • Please check whether your textbox holds numeric value. You should not input alphabets. I suspect it should be because your textbox might be empty or you had input alphabet – Vicky S Dec 09 '17 at 14:29