我有兩個關於這個簡單的計算器應用程序的簡單問題,我想在C#中構建。 (而不是作業順便說一句)我試圖讓MessageBox.Show消息顯示在我的代碼的乘法和加法部分中,但即使輸入負值,它們似乎也不顯示。該應用程序似乎無論如何做數學。此外,這可能是一個愚蠢的,我如何擺脫應用程序中出現的label5文本,而無需在屬性窗口中刪除它?簡單的計算器應用程序在C#
任何幫助將不勝感激,謝謝!
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
}
}
}
我在應用程序中看不到任何名爲label5的標籤 – Sajeetharan 2015-04-05 04:46:33
是while循環導致值始終爲true並且不允許控件通過。用If替換它,它應該可以工作。 – 2015-04-05 04:56:45
我試圖改變while循環來if語句和相同的事情不斷髮生@CHash_Mike – jugo 2015-04-05 20:37:30