Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#如何找到最多4个文本框/结果_C# - Fatal编程技术网

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

c#如何找到最多4个文本框/结果,c#,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

我有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)

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

使用一些简单的扩展方法使生活更轻松

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();
   }
}
扩展方法

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 _);
   }
}
获取最大值的辅助函数

// 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();
   }
}
额外资源

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

本节包含有助于您的基本背景信息 了解LINQ文档和示例的其余部分

将数字的字符串表示形式转换为其32位有符号 整数等价。返回值指示操作是否正确 成功了

通过使用params关键字,可以指定 接受数量可变的参数

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

基于谓词筛选值序列

将序列中的每个元素投影到新形式中

返回值序列中的最大值

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


来自IFebles的评论


也可以通过此操作完成(在 面板):

var values=panel1.Controls.Cast()
.其中(obj=>obj是文本框)
.Select(obj=>int.Parse(obj.Text))
.Max();

知道如果任何输入无法解析,它会抛出
FormatException
。虽然没有给出的答案那么简洁,但知道也很好。

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

使用一些简单的扩展方法使生活更轻松

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();
   }
}
扩展方法

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 _);
   }
}
获取最大值的辅助函数

// 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();
   }
}
额外资源

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

本节包含有助于您的基本背景信息 了解LINQ文档和示例的其余部分

将数字的字符串表示形式转换为其32位有符号 整数等价。返回值指示操作是否正确 成功了

通过使用params关键字,可以指定 接受数量可变的参数

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

基于谓词筛选值序列

将序列中的每个元素投影到新形式中

返回值序列中的最大值

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


来自IFebles的评论


也可以通过此操作完成(在 面板):

var values=panel1.Controls.Cast()
.其中(obj=>obj是文本框)
.Select(obj=>int.Parse(obj.Text))
.Max();

知道如果任何输入无法解析,它会抛出
FormatException
。虽然没有给出的答案那么简洁,但知道也很好。

您是否打算在您的
if
语句中使用位AND?我不知道,但它在@eatatjoe中有效。如果没有,那么您不应该这样做。你知道它是怎么工作的吗@你想找到文本框中最大的数字或总和吗?Arrray或其他集合可以对结果进行排序,很容易得到最大的数字或它们的总和。还有一件事,我想你