简体   繁体   English

C# - 文本框输入范围

[英]C# - range for textbox inputs

I have a form with 4 text boxes.我有一个带有 4 个文本框的表单。 I'm trying to code a method that checks to make sure the inputs in the 1st and 3rd textbox are between 0 and 100. This is an example that I found.我正在尝试编写一种方法来检查以确保第一个和第三个文本框中的输入介于 0 和 100 之间。这是我找到的一个示例。 But, I'm confused on how you can check both text boxes if there is only 1 parameter for the textbox.但是,如果文本框只有 1 个参数,我对如何检查两个文本框感到困惑。 I know that you need to convert the inputs because text boxes only accept strings.我知道您需要转换输入,因为文本框只接受字符串。 What I'm having trouble with is after converting the 2 text boxes, how do you check both of them in this if statement.我遇到的问题是在转换了 2 个文本框之后,你如何在这个 if 语句中检查它们。 Do you convert both textboxes, give them different variable names, and then use those variables in 2 different if statements?您是否转换两个文本框,给它们不同的变量名称,然后在 2 个不同的 if 语句中使用这些变量? Would it be correct to just repeat this statement for the other textbox?对另一个文本框重复此语句是否正确?

public bool IsWithinRange(TextBox textBox, string name, decimal min, decimal max)
        {
            number = txtOperand1.Text, txtOperand2.Text;

            if (number < 0 || number > 100)
            {
                MessageBox.Show(name + " must be between " + min + " and " + max + ".", "Entry Error");
                textBox.Focus();
                return false;
            }
            return true;
        }

To validate the values of TextBox you just need to parse each fileds data/text as int simply parsing & a checking method is enough.要验证TextBox的值,您只需要将每个字段数据/文本解析为int简单解析和检查方法就足够了。

As @Jimi pointed out, for WinForms :正如@Jimi 指出的,对于WinForms

private void button1_Click_1(object sender, EventArgs e)
{
    int f = 0, t = 0;

    if (Int32.TryParse(textBox1.Text, out f))
    {
        // successfully parsed 
    }

    if (Int32.TryParse(textBox3.Text, out t))
    {
        // successfully parsed 

    }

    // or just use parse..
    int f1 = Int32.Parse(textBox1.Text);
    int t1 = Int32.Parse(textBox3.Text);


    if (rangeCheck(f, t))
    {
       // Success 
    }
}
bool rangeCheck(int first, int third)
{
    return (first >= 0 && first <= 100 && third >= 0 && third <= 100);
}

设计你的表格,只是一个例子:Your Form Designer code: modify according to your needs您的表单设计器代码:根据您的需要修改

            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(230, 75);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.textBox1.TabIndex = 0;
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(230, 123);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 20);
            this.textBox2.TabIndex = 1;
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(230, 168);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(100, 20);
            this.textBox3.TabIndex = 2;
            // 
            // textBox4
            // 
            this.textBox4.Location = new System.Drawing.Point(230, 211);
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(100, 20);
            this.textBox4.TabIndex = 3;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(230, 262);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 4;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click_1);

Using WPF使用WPF

 <TextBox Name="box1"  ...other_properties  />
 <TextBox Name="box2"  ...other_properties  />
 <TextBox Name="box3"  ...other_properties  />
 <TextBox Name="box4"  ...other_properties  />

 <Button Click="Button_Click" Content="Check for validity" ...other properties />

Now handle those:现在处理那些:

private void Button_Click(object sender, RoutedEventArgs e)
        {

            int f = 0, t = 0;

            if (Int32.TryParse(box1.Text, out f))
            {
               // successfully parsed 
            }

            if (Int32.TryParse(box3.Text, out t))
            {
                // successfully parsed 

            }

            // or just use parse..
            int f1 = Int32.Parse(box1.Text);
            int t1 = Int32.Parse(box3.Text);


            if (rangeCheck(f, t))
            {
                Debug.WriteLine("Both are within 0 and 100");
            }
        }

        bool rangeCheck(int first, int third)
        {
            return (first >= 0 && first <= 100 && third >= 0 && third <= 100);
        }

You can use the following code to make sure the inputs in the 1st and 3rd textbox are您可以使用以下代码来确保第一个和第三个文本框中的输入是

between 0 and 100. 0 到 100 之间。

private void button1_Click(object sender, EventArgs e)
        {
            int m = 0;
            int n = 0;
            bool a = int.TryParse(textBox1.Text, out  m);
            bool b = int.TryParse(textBox3.Text, out n);
            if(a&b==false)
            {
                MessageBox.Show("please enter number");
            }
            else
            {
                bool t = IsWithinRange(m,"textbox1", 1, 100);
                t= IsWithinRange(n, "textbox3", 1, 100);
            }

        }
        public bool IsWithinRange(int a, string name,decimal min, decimal max)
        {
            if(a>=min&&a<=max)
            {
                MessageBox.Show(name + " is between " + min + " and " + max + ".", "Right");
            }
            else
            {
                MessageBox.Show(name + " must be between " + min + " and " + max + ".", "Entry Error");
            }
            return true;
        }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM