我有一个带有4个文本框的表单。我正在尝试编写一种方法来检查以确保第1个和第3个文本框中的输入在0到100之间。这是我发现的一个示例。但是,如果文本框只有1个参数,我对如何检查两个文本框感到困惑。我知道您需要转换输入,因为文本框仅接受字符串。我遇到的麻烦是在转换了两个文本框后,如何在此if语句中检查这两个文本框。您是否转换两个文本框,给它们指定不同的变量名,然后在2个不同的if语句中使用这些变量?对其他文本框重复此语句是否正确?
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;
}
[为了验证TextBox
的值,您只需要解析每个字段的数据/文本,因为int
只需解析,并且一种检查方法就足够了。
正如@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);
}
// 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);
使用
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 />
现在处理那些:
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);
}
您可以使用以下代码来确保第一和第三文本框中的输入为
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;
}