【问题标题】:Adding sorted and unsorted integer to list box in C#在 C# 中将已排序和未排序的整数添加到列表框中
【发布时间】:2018-05-22 14:06:04
【问题描述】:

在我的系统中,我有一个文本框和按钮,允许用户在列表框中输入一个整数。但是,我想包括两个单选按钮 - 已排序和未排序,以便用户必须选择在添加时是否要对整数进行排序或未排序。

这是我目前的添加按钮代码;

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());
}

这是单选按钮“排序”的代码;

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

这是“未排序”单选按钮的代码 -

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

是否有人对如何将整数添加到列表有任何建议,以便当用户选择单选按钮“排序”然后单击添加整数时,整数会添加到排序列表中?谢谢你。

【问题讨论】:

  • 您的问题不清楚,您是否有 2 个已排序、未排序的列表框,或者您是否希望使用相同的列表框实现功能,就像 sorted 为 true 一样,否则要排序的列表框项目应添加用户按原样输入。
  • 是的,@VickyS 是对的,我不明白您是否需要在正确的位置插入新值,或者您需要在插入值且 RadioButtonSorted.Checked 为 @ 时对整个列表进行排序987654325@
  • 如果这是 Windows 窗体,则在 CheckedChanged 事件处理程序中不需要相互取消选择单选按钮。属于同一组的单选按钮会自动强制执行唯一选择。
  • 如果列表框的值是 2、4、1,并且您想添加另一个整数,3 作为排序整数,您会将它放在哪里?请你把你的问题说得更清楚。
  • 我认为他应该使用CheckBox而不是两个RadioButtons

标签: c# sorting listbox


【解决方案1】:

使用单选按钮切换列表框的Sorted 属性。根据documentation,它还确保了

[as] 项目被添加到排序的 ListBox,项目被移动到排序列表中的适当位置

所以,你可以写

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; 
}

您使用了CheckedChanged 事件。它不仅会在选择一个单选按钮时触发,它也会在另一个未选中的单选按钮上触发。因此需要在handler中query the actual check state

但有一个缺点:Sorted 仅限于字母顺序。如果您得到 1、10、2、3 但期望 1、2、3、10,那么您可以用零填充整数以获得 0001、0002、0003、0010;或应用 a solution like this 对数据进行预排序,然后刷新整个列表框。

【讨论】:

    【解决方案2】:

    如果选中 sorted,这将对列表框中的所有项目进行排序,但我仍然认为没有必要有 2 个重做按钮。你可以用单个复选框替换它

                    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());
                        }
    

    【讨论】:

    • 非常感谢。这真的很有帮助。有一件事,行号。 Add(int.Parse(textBoxInsert.Text));出现错误消息 - mscorlib.dll 中出现“System.FormatException”类型的未处理异常附加信息:输入字符串的格式不正确。您对这是为什么有任何建议?
    • 请检查您的文本框是否包含数值。你不应该输入字母。我怀疑这应该是因为您的文本框可能是空的或者您输入了字母
    猜你喜欢
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    相关资源
    最近更新 更多