C# 電卓使えそうなロジック

if (a = 0)

これだめでした。ifがbool型になっているので

 

ですので

if(a == 0)

ならOK

 

Buttonに数値を+する処理

 

  textBox1.Text += ((Button).sender).Tex

ButtonクラスをSenderで送り、Textで Text1.TextBoxにいれる

 

C# 電卓についての質問なんですが、textBoxに0が表示されていて何かの数字ボタン... - Yahoo!知恵袋

 

 

detail.chiebukuro.yahoo.co.jp

 

電卓でのテキストボックスが0の場合。

if (a == 0)


            if (a == 0)
            {
                textBox1.Text = ((Button)sender).Text;
            }

 

として、上書きする。

この時、 += にしない。

 

 

 

途中まとめ作成。

 

ボタン配置。

→全部選択 イベント→Click の項目を Number で纏める。

たしか、

ufcpp.net

ufcpp.netのどっちかに イベント処理はまとめれる。

みたいなことがかいてあった。

 

            // button1
            //
            this.button1.Location = new System.Drawing.Point(36, 180);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(30, 30);
            this.button1.TabIndex = 1;
            this.button1.Text = "1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.Number);

  this.button1.Click += new System.EventHandler(this.Number);

これが選択したボタン全てに入ってる。

 

 

イベントのキャンセルは 

if()

{

return;

}

 

こうしないと、2回数字が入ってしまう。

 

<<ボタン処理>>

        private void Number(object sender, EventArgs e)
        {
            
            double a = double.Parse(textBox1.Text);

            if (a == 0)
            {
                textBox1.Text = ((Button)sender).Text;
                return;
            }
            textBox1.Text += ((Button)sender).Text;
        }

 

テキストボックスは初期値 「0」に設定。