我有两个关于这个简单的计算器应用程序的快速问题,我试图用C#构建。 (顺便说一下不做作业)我试图让MessageBox.Show消息显示在我的代码的乘法和添加部分,但即使我输入负值,它们似乎也不会显示。该应用程序似乎只是做数学。此外,这可能是一个愚蠢的,如何删除应用程序中出现的label5文本,而不删除它在属性窗口中?
非常感谢任何帮助,谢谢!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AddMultiply
{
public partial class AddMultiply : Form
{
public AddMultiply()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void txtFirstValue_TextChanged(object sender, EventArgs e)
{
}
private void btnAdd_Click(object sender, EventArgs e)
{
double firstValue;
double secondValue;
double answer;
while (double.TryParse(txtFirstValue.Text, out firstValue) == false)
{
MessageBox.Show("The value(s) entered must be > 0");
}
while(double.TryParse(txtSecondValue.Text, out secondValue) == false)
{
MessageBox.Show("The value(s) entered must be > 0");
}
answer = firstValue + secondValue;
lblAnswer.Text = answer.ToString();
}
private void btnMultiply_Click(object sender, EventArgs e)
{
double firstValue;
double secondValue;
double answer;
while (double.TryParse(txtFirstValue.Text, out firstValue) == false)
{
MessageBox.Show("The value(s) entered must be > 0");
}
while (double.TryParse(txtSecondValue.Text, out secondValue) == false)
{
MessageBox.Show("The value(s) entered must be > 0");
}
answer = firstValue * secondValue;
lblAnswer.Text = answer.ToString();
}
private void lblAnswer_Click(object sender, EventArgs e)
{
lblAnswer.Text = ""; //tries to get rid of "label5" text in application, but fails to do so
}
}
}
答案 0 :(得分:1)
1)你应该将while更改为" if":
private void btnAdd_Click(object sender,EventArgs e) { double firstValue; double secondValue; 双重答案;
if (double.TryParse(txtFirstValue.Text, out firstValue) == false)
{
MessageBox.Show("The value(s) entered must be > 0");
}
if(double.TryParse(txtSecondValue.Text, out secondValue) == false)
{
MessageBox.Show("The value(s) entered must be > 0");
}
answer = firstValue + secondValue;
lblAnswer.Text = answer.ToString();
}
2)Ladel5在哪里?它似乎不存在......
答案 1 :(得分:1)
您可以尝试以下代码来显示MessageBox,然后该值小于0:
if (n1 < 0 || n2 < 0)
MessageBox.Show("Value less than ZERO ", "Value less than ZERO",MessageBoxButtons.OK , MessageBoxIcon.Exclamation);
至于摆脱标签,你可以尝试:
label5.Visible = false;