文本输入 C# 比较值和限制

本文关键字:比较 输入 文本 | 更新日期: 2023-09-27 18:27:11

private void button1_Click(object sender, EventArgs e)
{
        int v1 = Convert.ToInt32(textBox1.Text);
        int v2 = Convert.ToInt32(textBox2.Text);
        int v3 = Convert.ToInt32(textBox3.Text);
        int v4 = Convert.ToInt32(textBox4.Text);
        int v5 = Convert.ToInt32(textBox5.Text);
        int v6 = Convert.ToInt32(textBox6.Text);
        int v7 = Convert.ToInt32(textBox7.Text);
        int v8 = Convert.ToInt32(textBox8.Text);
        int v9 = Convert.ToInt32(textBox9.Text);
        int v10 = Convert.ToInt32(textBox10.Text);
        int n11 = Convert.ToInt32(textBox11.Text);
        int n12 = Convert.ToInt32(textBox12.Text);
        int n13 = Convert.ToInt32(textBox13.Text);
        int n14 = Convert.ToInt32(textBox14.Text);
        int n15 = Convert.ToInt32(textBox15.Text);
        int n16 = Convert.ToInt32(textBox16.Text);
        int n17 = Convert.ToInt32(textBox17.Text);
        int n18 = Convert.ToInt32(textBox18.Text);
        int n19 = Convert.ToInt32(textBox19.Text);
        int n20 = Convert.ToInt32(textBox20.Text);


        List<int> list = new List<int>() { v1, v2, v3, v4, v5, v6, v7, v8, v9 ,v10  };
        List<int> list2 = new List<int>() { n11, n12, n13, n14, n15, n16, n17, n18, n19, n20 };
        list = list.OrderBy(o => o).ToList();
        list2 = list2.OrderBy(o => o).ToList();
        list2.SequenceEqual(list);

}

嗨,evryone,这是这个项目的第二部分,我需要帮助弄清楚如何比较列表与列表 2 中的值(数字(,例如,如果"list"包含数字5,而"list2"也包含数字5,我需要一个true,如果不是false,顺序并不重要,谢谢大家的帮助。

文本输入 C# 比较值和限制

您是否尝试验证输入

private void textBox1_TextChanged(object sender, EventArgs e)
{
     int value = 100 ;
     if ( !int.TryParse ( textBox1.Text, out value ) || value > 99 || value < -99 )
     {
         textBox1.TextChanged -= textBox1_TextChanged;
         textBox1.Text = string.Empty;
         textBox1.TextChanged += textBox1_TextChanged;
     }
}

像这样编写自己的方法

public static bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
{
    Int32 result;
    return Int32.TryParse(val, NumberStyle,
        System.Globalization.CultureInfo.CurrentCulture, out result);
}

这是你怎么称呼它

var _isNumeric = isNumeric("9.", System.Globalization.NumberStyles.Integer);//test like this returns false

在您的情况下,将字符串替换为文本框1.文本值

var _isNumeric = isNumeric(textBox1.Text, System.Globalization.NumberStyles.Integer);