创建一个包含两个文本框和两个按钮的Windows应用程序
本文关键字:按钮 两个 Windows 应用程序 文本 一个 包含两 创建 | 更新日期: 2023-09-27 18:08:14
当我尝试使用黄色字体时,我把代码搞砸了。请帮忙!!
说明:创建一个包含两个文本框和两个按钮的Windows应用程序。文本框应用于允许用户输入两个正数值。按钮应标记为"相加"answers"相乘"。创建事件处理程序方法,用于检索值、预处理计算并在标签上显示计算结果。结果标签最初应设置为不可见,字体颜色为黄色。如果输入了无效数据,请将结果标签上的字体颜色更改为红色,并显示消息"Value must be numeric and>0"。当显示最终答案时,字体颜色应为黄色。文本框标题将需要额外的标签。不允许输入非数字字符。调用TryParse((方法来检索值。程序语句中涉及的所有控件都应命名。右对齐文本框中的值。
这是我在表格1页上得到的内容
namespace Add_and_Multiply
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int val1;
int val2;
val1 = Convert.ToInt32(textBox1.Text);
val2 = Convert.ToInt32(textBox2.Text);
label3.Text = Convert.ToString(val1 + val2); //after this line I tried putting the code to give me yellow color text and now I have so many errors:(
}
private void button2_Click(object sender, EventArgs e)
{
int val1;
int val2;
val1 = Convert.ToInt32(textBox1.Text);
val2 = Convert.ToInt32(textBox2.Text);
label3.Text = Convert.ToString(val1 * val2);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
有人能帮帮我吗?我不太擅长这个。感谢
有人叫我把它修成这样。
namespace Add_and_Multiply
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int val1;
int val2;
if(!int.TryParse(textBox1.Text.Trim(), out val1))
{
lblYellow.ForeColor = Color.Yellow;
lblYellow.Visible = true;
lblYellow.Text = "First value is invalid";
return;
}
if(!int.TryParse(textBox2.Text.Trim(), out val2))
{
lblYellow.ForeColor = Color.Yellow;
lblYellow.Visible = true;
lblYellow.Text = "Second value is invalid";
return;
}
lblYellow.Visible = false;
label3.Text = Convert.ToString(val1 + val2);
}
private void button2_Click(object sender, EventArgs e)
{
int val1;
int val2;
if(!int.TryParse(textBox1.Text.Trim(), out val1))
{
lblYellow.ForeColor = Color.Yellow;
lblYellow.Visible = true;
lblYellow.Text = "First value is invalid";
return;
}
if(!int.TryParse(textBox2.Text.Trim(), out val2))
{
lblYellow.ForeColor = Color.Yellow;
lblYellow.Visible = true;
lblYellow.Text = "Second value is invalid";
return;
}
lblYellow.Visible = false;
label3.Text = Convert.ToString(val1 * val2);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
但我收到一个错误"名称'lblYellow'在当前上下文Form1.cs中不存在",我应该在private void button1_click行之前声明它吗?
您有什么错误?,我想你必须删除一个}后行:
label3.Text = Convert.ToString(val1 + val2);
label3.ForeColor = Color.Yellow;
应该将标签颜色更改为黄色。。。
但你还有另一个问题。您不是在检查无效输入以将标签更改为红色,也不是在调用TryParse
作为指令命令。
此外,指令规定用户应该能够输入任何正数值。这将包括8.9和216.75。我想如果你想要尽可能好的成绩,你需要考虑到这一点。看看C#内置的数据类型。
看起来您正在定义一个没有类或在类之外的方法,请检查所有方法是否都在Form1内部,并检查是否有额外的或缺失的括号。
如果有人在这个项目上遇到问题,那么这里是最终产品。它一旦运行起来就很好看。
一切都很好,应该如何!
{公共分部类Form1:Form{公共窗体1(({InitializeComponent((;}
private void button1_Click(object sender, EventArgs e)
{
int val1;
int val2;
int valid1 = 0;
int valid2 = 0;
if(int.TryParse(textBox1.Text.Trim(), out val1))
{
if(val1>0)
{
valid1 = 1;
caption1.ForeColor = Color.Yellow;
caption1.Visible = true;
caption1.Text = "First value is valid";
}
else
{
caption1.ForeColor = Color.Red;
caption1.Visible = true;
caption1.Text = "Value must be Greater than 0";
}
}
else
{
caption1.ForeColor = Color.Red;
caption1.Visible = true;
caption1.Text = "Value must be Numeric";
}
if(int.TryParse(textBox2.Text.Trim(), out val2))
{
if (val2 > 0)
{
valid2 = 1;
caption2.ForeColor = Color.Yellow;
caption2.Visible = true;
caption2.Text = "First value is valid";
}
else
{
caption2.ForeColor = Color.Red;
caption2.Visible = true;
caption2.Text = "Value must be Greater than 0";
}
}
else
{
caption2.ForeColor = Color.Red;
caption2.Visible = true;
caption2.Text = "Value must be Numeric";
}
if((valid1 > 0)&& (valid2 > 0))
{
label3.Visible = true;
label3.ForeColor = Color.Yellow;
label3.Text = Convert.ToString(val1 + val2);
}
else
{
label3.Visible = false;
}
}
private void button2_Click(object sender, EventArgs e)
{
int val1;
int val2;
int valid1 = 0;
int valid2 = 0;
if(int.TryParse(textBox1.Text.Trim(), out val1))
{
if (val1 > 0)
{
valid1 = 1;
caption1.ForeColor = Color.Yellow;
caption1.Visible = true;
caption1.Text = "First value is valid";
}
else
{
caption1.ForeColor = Color.Red;
caption1.Visible = true;
caption1.Text = "Value must be Greater than 0";
}
}
else
{
caption1.ForeColor = Color.Red;
caption1.Visible = true;
caption1.Text = "Value must be Numeric";
}
if(int.TryParse(textBox2.Text.Trim(), out val2))
{
if (val2 > 0)
{
valid2 = 1;
caption2.ForeColor = Color.Yellow;
caption2.Visible = true;
caption2.Text = "First value is valid";
}
else
{
caption2.ForeColor = Color.Red;
caption2.Visible = true;
caption2.Text = "Value must be Greater than 0";
}
}
else
{
caption2.ForeColor = Color.Red;
caption2.Visible = true;
caption2.Text = "Value must be Numeric";
}
if ((valid1 > 0) && (valid2 > 0))
{
label3.Visible = true;
label3.ForeColor = Color.Yellow;
label3.Text = Convert.ToString(val1 * val2);
}
else
{
label3.Visible = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
我知道这是两年后的事了,但我认为人们可能仍在寻求帮助。
private void btnAdd_Click(object sender, EventArgs e)
{
int firstNumber, secondNumber;
if (!int.TryParse(txtBxFirstNumber.Text, out firstNumber) || firstNumber < 1)
{
lblResults.Text = "First number needs'n to be numeric and > 0";
lblResults.ForeColor = Color.Red;
lblResults.Visible = true;
txtBxFirstNumber.Clear();
return;
}
if (!int.TryParse(txtBxSecondNumber.Text, out secondNumber) || secondNumber < 1)
{
lblResults.Text = "Second number need'n to be numeric and > 0";
lblResults.ForeColor = Color.Red;
lblResults.Visible = true;
txtBxSecondNumber.Clear();
return;
}
lblResults.ForeColor = Color.Yellow;
lblResults.Visible = true;
lblResults.Text = Convert.ToString(firstNumber + secondNumber);
}
private void btnMultiply_Click(object sender, EventArgs e)
{
int firstNumber, secondNumber;
if (!int.TryParse(txtBxFirstNumber.Text, out firstNumber) || firstNumber < 1)
{
lblResults.Text = "First number needs'n to be numeric";
lblResults.ForeColor = Color.Red;
lblResults.Visible = true;
txtBxFirstNumber.Clear();
return;
}
if (!int.TryParse(txtBxSecondNumber.Text, out secondNumber) && secondNumber < 1)
{
lblResults.Text = "Second number need'n to be numeric";
lblResults.ForeColor = Color.Red;
lblResults.Visible = true;
txtBxSecondNumber.Clear();
return;
}
lblResults.ForeColor = Color.Yellow;
lblResults.Visible = true;
lblResults.Text = Convert.ToString(firstNumber * secondNumber);
}
您应该将该标签的设计名称更改为"lblYellow";然后就会工作。。。转到该标签的属性并向下滚动,有一个名为design的部分,在那里你会发现";(姓名(";将该默认名称更改为";lblYellow";。。。
在此处输入图像描述