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

时间:2015-12-17 23:25:27

标签: c# visual-studio

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中的值(数字), 例如&#34; list&#34;包含数字5和&#34; list2&#34;也包含5号,我需要一个真的,如果不是假的,顺序并不重要,谢谢大家的帮助。

2 个答案:

答案 0 :(得分:0)

您是否尝试验证输入

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

答案 1 :(得分:0)

编写自己喜欢的方法

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

在你的情况下用textBox1.Text值替换字符串

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