C# 无法将对象类型System.Decimal转换为排序列表框中的System.String类型

C# 无法将对象类型System.Decimal转换为排序列表框中的System.String类型,c#,winforms,windows-forms-designer,C#,Winforms,Windows Forms Designer,因此,我尝试运行一个windows窗体,您可以从numericUpDown框(从windows窗体)中添加一个数字,然后它将其放入列表框(我的代码可以很好地执行此操作)。列表框中的外观示例: 七, 5. 10 一, 单击“排序”时,应按以下顺序生成: 一, 5. 7. 十, 当我运行代码并单击排序按钮(按钮1)对列表框中的整数进行排序时,我得到: System.InvalideException:“无法将'System.Decimal'类型的对象强制转换为'System.String'类型。”

因此,我尝试运行一个windows窗体,您可以从numericUpDown框(从windows窗体)中添加一个数字,然后它将其放入列表框(我的代码可以很好地执行此操作)。列表框中的外观示例:

七, 5. 10 一,

单击“排序”时,应按以下顺序生成:

一, 5. 7. 十,

当我运行代码并单击排序按钮(按钮1)对列表框中的整数进行排序时,我得到:

System.InvalideException:“无法将'System.Decimal'类型的对象强制转换为'System.String'类型。”

我不知道这意味着什么

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ListBoxSorter
{
    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //int num = (int)listBox1.Items[0];
            //listBox1.Items.Add(num);


        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            //int number = (int)numericUpDown1.Value;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //listBox1.Items.Sort(numericUpDown1.Value);

            List<int> ListB = new List<int>();

            foreach (string x in listBox1.Items)
            {
                ListB.Add(Convert.ToInt32(x));
            }
            ListB.Sort();
        }



        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(numericUpDown1.Value);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间ListBoxSorter
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效列表框1\u SelectedIndexChanged(对象发送方,事件参数e)
{
//int num=(int)listBox1.Items[0];
//listBox1.Items.Add(num);
}
私有void numericUpDown1_值已更改(对象发送方,事件参数e)
{
//int number=(int)numericUpDown1.Value;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
//listBox1.Items.Sort(numericUpDown1.Value);
List ListB=新列表();
foreach(listBox1.Items中的字符串x)
{
添加(转换为32(x));
}
ListB.Sort();
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
listBox1.Items.Add(numericUpDown1.Value);
}
}
}
试试这个:(未编译或调试)

private void按钮1\u单击(对象发送者,事件参数e)
{
List ListB=新列表();
foreach(listBox1.Items中的字符串x)
{
添加(转换为32(x));
}
ListB.Sort();
listBox1.Items.Clear()//重要!
foreach(列表B中的int x)
{
listBox1.Items.Add(x.ToString());
}
}

foreach(listBox1.Items中的字符串x)
=>听起来像是此语句引发了转换异常。
listBox1.Items
(似乎是
List
)的类型是什么?我错过的是欢呼声:)
private void button1_Click(object sender, EventArgs e)
{
    List<int> ListB = new List<int>();

    foreach (string x in listBox1.Items)
    {
        ListB.Add(Convert.ToInt32(x));
    }

    ListB.Sort();

    listBox1.Items.Clear() //important!

    foreach (int x in ListB)
    {
        listBox1.Items.Add(x.ToString());
    }
}