c#如何找到最多4个文本框/结果

时间:2018-03-16 03:18:10

标签: c#

我有4个文本框,我想找到4个中最大的数字,有什么方法不是循环,这些文本框是4个团队的总分,最后一个按钮是将显示的框最大数量

我必须在每个按钮中输入每个值(值1到5),结果是四个中最大的数字,我将使这些值上升

private void button1_Click(object sender, EventArgs e)
{
   int value1 = 0;
   int value2 = 0;
   int value3 = 0;
   int value4 = 0;
   int value5 = 0;
   int result = 0;

   if (Int32.TryParse(textBox4.Text, out value1) && Int32.TryParse(textBox2.Text, out value2) && Int32.TryParse(textBox6.Text, out value3) && Int32.TryParse(textBox14.Text, out value4) && Int32.TryParse(textBox13.Text, out value5))

   {
      result = value1 + value2 + value3 + value4 + value5;
      textBox21.Text = result.ToString();
   }

}

private void button2_Click(object sender, EventArgs e)
{
   int value1 = 0;
   int value2 = 0;
   int value3 = 0;
   int value4 = 0;
   int value5 = 0;
   int result = 0;

   if (Int32.TryParse(textBox11.Text, out value1) && Int32.TryParse(textBox10.Text, out value2) && Int32.TryParse(textBox9.Text, out value3) & Int32.TryParse(textBox8.Text, out value4) && Int32.TryParse(textBox15.Text, out value5))

   {
      result = value1 + value2 + value3 + value4 + value5;
      textBox22.Text = result.ToString();
   }
}

private void button3_Click(object sender, EventArgs e)
{
   int value1 = 0;
   int value2 = 0;
   int value3 = 0;
   int value4 = 0;
   int value5 = 0;
   int result = 0;

   if (Int32.TryParse(textBox7.Text, out value1) && Int32.TryParse(textBox5.Text, out value2) && Int32.TryParse(textBox1.Text, out value3) & Int32.TryParse(textBox3.Text, out value4) && Int32.TryParse(textBox12.Text, out value5))

   {
      result = value1 + value2 + value3 + value4 + value5;
      textBox23.Text = result.ToString();
   }
}

private void button4_Click(object sender, EventArgs e)
{
   int value1 = 0;
   int value2 = 0;
   int value3 = 0;
   int value4 = 0;
   int value5 = 0;
   int result = 0;

   if (Int32.TryParse(textBox20.Text, out value1) && Int32.TryParse(textBox19.Text, out value2) && Int32.TryParse(textBox18.Text, out value3) & Int32.TryParse(textBox17.Text, out value4) && Int32.TryParse(textBox16.Text, out value5))

   {
      result = value1 + value2 + value3 + value4 + value5;
      textBox24.Text = result.ToString();
   }
}

private void button5_Click(object sender, EventArgs e)

1 个答案:

答案 0 :(得分:3)

有很多方法可以做到这一点,但这可能会帮助你

使用一些简易伸展方法让生活更轻松

扩展方法

public static class TextBoxExtensions
{
   public static int GetInt(this TextBox source)
   {
      // if TextBox null just return 0
      if (source == null)
      {
         return 0;
      }
      // if it is a valid int, return it, otherwise return 0
      // not we use string, in case someone put a space at the start or end
      return int.TryParse(source.Text.Trim(), out var value) ? value : 0;
   }

   public static bool HasValidInt(this TextBox source)
   {
      // if TextBox null or its not an int return false
      // otherwise return true
      return source != null && int.TryParse(source.Text.Trim(), out var _);
   }
}

帮助函数获取Max

// helper function, this does not use a loop
// get the max of all textboxes
private int GetMax(params TextBox[] args)
{
   return args.Where(x => x.HasValidInt()) // remove any invalid numbers
               .Select(x => x.GetInt()) // project to int
               .Max(); //get the max of all
}

您现有的代码

使用扩展方法

private void button4_Click(object sender, EventArgs e)
{
   // this is just a better way to validate your text boxes with the extension method
   if (textBox1.HasValidInt() && textBox2.HasValidInt() && textBox3.HasValidInt() && textBox4.HasValidInt())
   {
      // get the ints from all text boxes using extension method
      var result = textBox1.GetInt() + textBox2.GetInt() + textBox3.GetInt() + textBox4.GetInt();
      textBox6.Text = result.ToString();
   }
}

获取最高版本1

这不使用循环。但是,确实使用Linq

private void button5_Click(object sender, EventArgs e)
{
   // get the max of all textboxes using the helper method
   textBox6.Text = GetMax(textBox1, textBox2, textBox3, textBox4).ToString();
}

获取最高版本2

这不使用循环。但是,确实使用Math.Max

private void button6_Click(object sender, EventArgs e)
{
   // this is just a better way to validate your text boxes with the extension method
   if (textBox1.HasValidInt() && textBox2.HasValidInt() && textBox3.HasValidInt() && textBox4.HasValidInt())
   {
      // use math to get the max
      var result = 0;
      result = Math.Max(result, textBox1.GetInt());
      result = Math.Max(result, textBox2.GetInt());
      result = Math.Max(result, textBox3.GetInt());
      result = Math.Max(result, textBox4.GetInt());
      textBox6.Text = result.ToString();
   }
}

其他资源

Extension Methods (C# Programming Guide)

  

扩展方法使您可以向现有类型“添加”方法   无需创建新的派生类型,重新编译或其他方式   修改原始类型。扩展方法是一种特殊的方法   静态方法,但它们被称为就像它们是实例方法一样   扩展类型。对于用C#,F#和Visual Basic编写的客户端代码,   调用扩展方法之间没有明显的区别   以及在类型中实际定义的方法。

Getting Started with LINQ in C#

  

本节包含可以帮助您的基本背景信息   了解其余的LINQ文档和示例。

<强> Int32.TryParse Method

  

将数字的字符串表示形式转换为32位有符号   整数当量。返回值表示是否进行操作   成功了。

<强> params (C# Reference)

  

通过使用params关键字,您可以指定一个方法参数   采用可变数量的参数。

     

您可以发送以逗号分隔的指定类型的参数列表   在参数声明中或指定的参数数组中   类型。你也可以不发送任何参数。如果你没有发送任何参数,那么   参数列表的长度为零。

<强> Enumerable.Where Method (IEnumerable, Func)

  

根据谓词过滤一系列值。

<强> Enumerable.Select Method (IEnumerable, Func)

  

将序列的每个元素投影到新表单中。

<强> Enumerable.Max Method

  

返回值序列中的最大值。

<强> Math.Max Method

  

返回两个指定数字中的较大者。

来自IFebles的评论

  

也可以这样做(在其中分割TextBoxs)   板):

var values = panel1.Controls.Cast<Control>()
                    .Where(obj => obj is TextBox)
                    .Select(obj => int.Parse(obj.Text))
                    .Max(); 

知道如果无法解析任何输入,它可以抛出FormatException。不像给定的答案那样整洁,但也很有用。