简体   繁体   English

C#中的简单计算器应用程序

[英]Simple Calculator Application in C#

I have two quick question about this simple calculator application I am trying to build in C#. 关于这个我要在C#中构建的简单计算器应用程序,我有两个快速问题。 (not homework by the way) I am trying to get the MessageBox.Show message to show in the multiply and add sections of my code, but they don't seem to be displaying even if I enter a negative value. (顺便说一下,这不是家庭作业)我试图让MessageBox.Show消息显示在代码的乘法和加法部分中,但是即使我输入了负值,它们也似乎没有显示。 The application just seems to do the math anyways. 该应用程序似乎仍然可以进行数学计算。 Also, this may be a dumb one, how do I get rid of the label5 text that appears in the application with out deleting it in the properties window? 另外,这可能是一个愚蠢的问题,如何摆脱应用程序中出现的label5文本,而又不删除其在属性窗口中的内容?

Any help will be greatly appreciated, thanks! 任何帮助将不胜感激,谢谢!

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
    }
}

} }

1)you should change the while to "if": 1)您应将while更改为“ if”:

private void btnAdd_Click(object sender, EventArgs e) { double firstValue; 私有无效btnAdd_Click(object sender,EventArgs e){double firstValue; double secondValue; double secondValue; double answer; 双重答案;

    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) where is Ladel5 ? 2)Ladel5在哪里? It doesn't seem to be exist... 它似乎不存在...

You can try the following code to show the MessageBox then the value is less than 0: 您可以尝试以下代码来显示MessageBox,然后该值小于0:

if (n1 < 0 || n2 < 0)
                MessageBox.Show("Value less than ZERO ", "Value less than ZERO",MessageBoxButtons.OK , MessageBoxIcon.Exclamation);

As for getting rid of the label you can try : 至于摆脱标签,您可以尝试:

label5.Visible = false;

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

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