![](/img/trans.png)
[英]C# (find second largest number) How to split program in subprograms ?so I can reuse code later…?
[英]c# how to find the largest number of 4 text boxes/results
我有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)
有很多方法可以做到這一點,但這可能會幫助您
通過一些短手伸展方法使生活更輕松
擴展方法
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();
}
}
擴展方法使您可以將方法“添加”到現有類型,而無需創建新的派生類型,重新編譯或修改原始類型。 擴展方法是一種特殊的靜態方法,但是它們的調用就像是擴展類型上的實例方法一樣。 對於用C#,F#和Visual Basic編寫的客戶端代碼,在調用擴展方法和在類型中實際定義的方法之間沒有明顯的區別。
本節包含基本的背景信息,這些信息將幫助您了解LINQ文檔和樣本的其余部分。
將數字的字符串表示形式轉換為其等效的32位有符號整數。 返回值指示操作是否成功。
通過使用params關鍵字,您可以指定一個采用可變數量參數的方法參數。
您可以發送以逗號分隔的參數聲明中指定類型的參數列表或指定類型的參數數組。 您也不能發送任何參數。 如果不發送任何參數,則參數列表的長度為零。
Enumerable.Where方法(IEnumerable,Func)
根據謂詞過濾值序列。
Enumerable.Select方法(IEnumerable,Func)
將序列的每個元素投影為新形式。
返回值序列中的最大值。
返回兩個指定數字中的較大者。
來自IFebles的評論
也可以這樣做(將面板中的TextBox分割):
var values = panel1.Controls.Cast<Control>()
.Where(obj => obj is TextBox)
.Select(obj => int.Parse(obj.Text))
.Max();
知道如果無法解析任何輸入,它將引發FormatException
。 不如給出的答案那么簡潔,但也很高興知道。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.