我試圖乘以一個數字輸入到我的標籤輸入文本框,但不斷遇到問題...我也試圖保持標籤值不變,如果沒有輸入文本框3,4,5。例如,如果我將3.20放在文本框3中,它將使用新值更新標籤1 & 4,但不保留剩餘標籤的原始值。C#通過文本框輸入來將標籤文本相乘。
非常失落......任何幫助,非常感謝。
我試圖做到:
在這個階段,文本框進入加侖被激活,所以還填充按鈕。用戶在文本框中輸入加侖數,然後單擊填充。 天然氣訂單的成本顯示在另一個文本框中,僅用於顯示。 將訂單價格計算打包成函數。
價格按鈕:在窗體的下半部分顯示一組新控件,顯示每種燃料的每加侖價格[這些是與上半部分標籤不同的新控件,是旨在向經理顯示當前價格是多少],以及用於輸入每個新價格的文本框以及第三行中的兩個按鈕Update和Cancel。更新將更新從文本框中讀取的價格作爲每加侖價格的新值。 [請注意,這些新值應該反映在表格上半部分的燃料等級按鈕下面的標籤中。這些標籤應始終顯示當前價格,因此經理在價格上的任何更改都會更改這些標籤。]取消不會改變任何內容。選擇完成後,下半部分的控件再次消失。
private int gasPrice = 0;
private int gasPrice1 = 0;
private int gasPrice2 = 0;
private int numGallons = 0;
private double total = 0;
private bool regularButton;
private bool premiumButton;
private bool xtraButton;
public Form1 ()
{
InitializeComponent();
}
private void regButton_Click (object sender, EventArgs e)
{
regButton.BackColor = Color.Green;
regButton.ForeColor = Color.Red;
textBox1.Enabled = true;
regularButton = true;
xtraButton = false;
premiumButton = false;
}
private void extraButton_Click (object sender, EventArgs e)
{
extraButton.BackColor = Color.Green;
extraButton.ForeColor = Color.Red;
textBox1.Enabled = true;
regularButton = false;
xtraButton = true;
premiumButton = false;
}
private void premButton_Click (object sender, EventArgs e)
{
premButton.BackColor = Color.Green;
premButton.ForeColor = Color.Red;
textBox1.Enabled = true;
regularButton = false;
xtraButton = false;
premiumButton = true;
}
private void fillButton_Click (object sender, EventArgs e)
{
//double price;
int gallons;
int price, price1, price2;
gasPrice = Convert.ToInt32(label1.Text);
gasPrice1 = int.Parse(label2.Text);
gasPrice2 = int.Parse(label3.Text);
numGallons = int.Parse(textBox1.Text);
//gallons = int.Parse(textBox1.Text);
//price = int.Parse(label1.Text);
//price1 = int.Parse(label2.Text);
//price2 = int.Parse(label3.Text);
if (regularButton == true)
{ total = gasPrice * numGallons; }
else if (xtraButton == true)
{ total = gasPrice1 * numGallons; }
else
{ total = gasPrice2 * numGallons; }
textBox2.Text = total.ToString("c");
textBox2.Visible = true;
if (textBox1.Text == "0")
{ MessageBox.Show("Operation Cancelled"); }
ResetData();
}
private void finishButton_Click (object sender, EventArgs e)
{
ResetData();
}
private void ResetData ()
{
//Resets buttons back to default settings
regButton.BackColor = default(Color);
regButton.ForeColor = default(Color);
extraButton.BackColor = default(Color);
extraButton.ForeColor = default(Color);
premButton.BackColor = default(Color);
premButton.ForeColor = default(Color);
//Clears out both text boxes
textBox1.Clear();
textBox2.Clear();
//textboxes are returned to original state
textBox1.Enabled = false;
textBox2.Visible = false;
}
private void salesButton_Click (object sender, EventArgs e)
{
}
private void button1_Click (object sender, EventArgs e)
{
// updates gas price if/when supervisor changes
label1.Text = "$" + textBox3.Text;
label2.Text = "$" + textBox4.Text;
label3.Text = "$" + textBox5.Text;
label4.Text = "$" + textBox3.Text;
label5.Text = "$" + textBox4.Text;
label6.Text = "$" + textBox5.Text;
}
private void pricesButton_Click (object sender, EventArgs e)
{
label4.Visible = true;
label5.Visible = true;
label6.Visible = true;
textBox3.Visible = true;
textBox4.Visible = true;
textBox5.Visible = true;
updateButton.Visible = true;
cancelButton.Visible = true;
你碰到什麼問題?請具體說明您需要什麼幫助。 – CodingYoshi
兩個問題即時運行到... 1是試圖乘以gasPrice * numGallons,我有天然氣價格列爲標籤上的文本爲3.70,它說不能將字符串轉換爲整數。第二個問題是當在文本框3中輸入一個新值時,它將更新標籤1和4,但是在返回時將標籤2,3,5,6保留爲$而不是標籤文本中列出的值。 – rpdeleon
你的第一個問題是因爲你試圖將文本從標籤轉換爲一個int,它失敗了,因爲你可能有不是數字的字符。第二個問題是因爲button1_Click中的代碼正在使用textbox3更新標籤1和4,當然,如果這些標籤顯示的文本框中沒有任何內容,其他標籤將只有一個美元符號。 – CodingYoshi