简体   繁体   English

C#WinForms-如何使用2个值之间的几个文本框(执行操作)

[英]C# WinForms - How to (do something) with several textboxes between 2 values

I have a form, where a 'user' inputs values into several textboxes and if the values are between certain ranges I want it to do something. 我有一个表单,其中“用户”将值输入到多个文本框中,并且如果值在某些范围之间,我希望它执行某些操作。

Is it possible to check multiple textboxes simultaneously as opposed to having them checked like so: 是否可以同时检查多个文本框,而不是像这样检查它们:

if (int.Parse(textbox1.Text) >= 100 && int.Parse(textbox1.Text) <= 2000)
{
    mylist.Add(int.parse(textbox1.text));
}
if (int.Parse(textbox2.Text) >= 100 && int.Parse(textbox2.Text) <= 2000)
{
    mylist.Add(int.parse(textbox1.text));
}
if (int.Parse(textbox3.Text) >= 100 && int.Parse(textbox3.Text) <= 2000)
{
    mylist.Add(int.parse(textbox3.text));
}

One possibility would be to put them in a list: 一种可能性是将它们放在列表中:

var textboxes = new List<TextBox>(new[] { textbox1, textbox2, textbox3 });

and then loop through this list and probably parse the value only once: 然后遍历此列表,可能只解析一次该值:

foreach (var t in textboxes) 
{
    int value = int.Parse(t.Text);
    if (value >= 100 && value <= 2000)
    {
        mylist.Add(value);
    }
}

or if you prefer using LINQ you could also do this one liner: 或者,如果您更喜欢使用LINQ,也可以执行以下操作:

IList<int> mylist = textboxes
    .Select(t => int.Parse(t.Value))
    .Where(value => value >= 100 && value <= 2000)
    .ToList();

Extract method: 提取方法:

public void AddValue(string text, List<int> list)
{
    var value = int.Parse(test);
    if (value >= 100 && value <= 2000)
    {
        list.Add(value);
    }
}

And call it: 并称之为:

var list = new List<int>();
AddValue(textbox1.Text, list);
AddValue(textbox2.Text, list);
AddValue(textbox3.Text, list);

You could write a simple boolean method that does this work on a textbox passed to it and tells the calling code if the parsing is successful 您可以编写一个简单的布尔方法,对传递给它的文本框执行此操作,并告诉调用代码解析是否成功

private bool ParseText(TextBox txt, List<int> theList)
{
    int value;
    if(Int32.TryParse(txt.Text, out value))
    {
        if(value >= 100 && value <= 2000)
        {
            theList.Add(value);
            return true;
        }
     }
    return false;
} 

Now you could call this method 现在您可以调用此方法

if(!ParseText(textBox1, mylist))
    MessageBox.Show("Invalid value in TextBox1");
if(!ParseText(textBox2, mylist))
    MessageBox.Show("Invalid value in TextBox2");
if(!ParseText(textBox3, mylist))
    MessageBox.Show("Invalid value in TextBox3");

Notice that a textbox whose text is expected to be an integer but is typed by the user should be always parsed using Int32.TryParse instead of Parse because Parse raises an exception if the text cannot be converted to an integer 请注意,应始终使用Int32.TryParse而不是Parse解析其文本应为整数但由用户键入的文本框,因为如果文本无法转换为整数,则Parse引发异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Winforms / C#:在2次运行之间存储/检索(持久)TextBoxes的内容 - Winforms/C#: Store/Retrieve (Persist) Content of TextBoxes between 2 Runs 如何不断检查WinForms / C#中的内容? - How to constantly check something in WinForms/C#? 如何在 c# winforms 中的所有文本框不为空后启用按钮? - How to enable button after all textboxes are not empty in c# winforms? 如何在 forms C# Winforms 之间转换? - How do I transition between forms C# Winforms? 在 C# Winforms 中同时自动完成两个文本框 - Autocomplete Two TextBoxes Simaltaneously in C# Winforms 如何使用C#将SQL数据库中的值获取到文本框中? - How do I get values from a SQL database into textboxes using C#? 单击“下一步”时,如何用一个消息框验证多个文本框? 视觉工作室(C#Winforms) - how to validate multiple TextBoxes with a MessageBox when clicked on “next”? visual studio(c# Winforms) 如何在C#Winforms中使用numericUpDown1生成动态文本框和标签 - How to generate dynamic textboxes and label using numericUpDown1 in C# Winforms 在C#Winforms之间传递值间歇工作 - Passing Values Between C# Winforms Working Intermittently 如何在C#中的文本框之间复制所选日期 - how to copy a selected date between textboxes in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM