2016-11-19 47 views
0

私のラベル入力をテキストボックスに入力した数値に掛けようとしていますが、問題を抱えています...テキストボックス3,4,5に入力がなければラベル値を変更しないでください。たとえば、テキストボックス3に3.20を置くと、ラベル1 & 4が新しい値で更新されますが、残りのラベルは元の値に保たれません。C#テキストボックス入力によるラベルテキストの乗算。

非常に失われました...どんな助けでも大歓迎です。私が達成しようとしている何

:この段階で

、テキストボックスには、ガロンを入力するために起動するので、また塗りつぶしボタンになります。ユーザーは、テキストボックスにガロン数を入力し、[塗りつぶし]をクリックします。 ガスオーダーのコストは、表示専用の別のテキストボックスに表示されます。 注文価格の計算を関数にパッケージ化します。

[価格]ボタン:フォームの下半分に表示される新しいコントロールセットをレンダリングし、燃料の各グレードごとの価格を表示します[上のラベルとは異なる新しいコントロールです。現在の価格が何であるかをマネージャに示すことを意味する)、それぞれに新しい価格を入力するためのテキストボックス、および3つ目の行の2つのボタンUpdateとCancelを使用します。更新は、テキストボックスから読み取った価格を1ガロン価格の新しい値として更新します。 [これらの新しい値は、フォームの上半分の燃料グレードのボタンの下に表示されているラベルに反映される必要があります。これらのラベルには常に現在の価格が表示されるため、Managerによる価格の変更によってラベルも変更されるはずです。キャンセルは何も変更しません。選択が行われた後、下半分のコントロールが再び消えます。

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

私はgasPrice * numGallonsを乗算しようとしていますが、ガス価格はラベルのテキストとして3.70と表示されており、文字列をintに変換することはできません。 2番目の問題は、テキストボックス3に新しい値を入力すると、ラベル1とラベル4を更新しますが、代わりにラベル2,3,5,6のラベルをテキストに記載された値ではなく$で置き換えます。 – rpdeleon

+0

あなたの最初の問題は、テキストをラベルからintに変換しようとしていて、数字以外の文字がある可能性があるために失敗しているためです。 2番目の問題は、button1_Clickのコードがlabel1と4をtextbox3で更新しているためです。もちろん、これらのラベルが表示されているテキストボックスに何もない場合は、他のラベルにドル記号が付きます。 – CodingYoshi

答えて

0

、あなただけの数値入力を許可したいテキストボックスを選択して[プロパティ]ウィンドウでイベント(lightnig)アイコンを検索し、KeyPressイベントをダブルクリックします。それはハンドラを生成します。

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

このコードでは、ユーザーはテキストボックスに数字と小数点を入力することしかできません。

関連する問題