C#通过文本框输入来将标签文本相乘。

问题描述:

我试图乘以一个数字输入到我的标签输入文本框,但不断遇到问题...我也试图保持标签值不变,如果没有输入文本框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; 
+0

你碰到什么问题?请具体说明您需要什么帮助。 – CodingYoshi

+0

两个问题即时运行到... 1是试图乘以gasPrice * numGallons,我有天然气价格列为标签上的文本为3.70,它说不能将字符串转换为整数。第二个问题是当在文本框3中输入一个新值时,它将更新标签1和4,但是在返回时将标签2,3,5,6保留为$而不是标签文本中列出的值。 – rpdeleon

+0

你的第一个问题是因为你试图将文本从标签转换为一个int,它失败了,因为你可能有不是数字的字符。第二个问题是因为button1_Click中的代码正在使用textbox3更新标签1和4,当然,如果这些标签显示的文本框中没有任何内容,其他标签将只有一个美元符号。 – CodingYoshi

选择您只想让数字输入文本框,在属性窗口中找到事件(lightnig)图标,然后双击按键响应事件。这将生成一个处理程序。把这个代码在处理程序:

if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && 
    (e.KeyChar != '.')) 
{ 
     e.Handled = true; 
} 

// only allow one decimal point 
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1)) 
{ 
    e.Handled = true; 
} 

该代码将只允许用户以数字和小数点输入到文本框中。