Question: C# Hello, i need help adding a working Decimal button to my c# calculator program, thanks CODE Form.cs using System; using System.Windows.Forms; namespace CalculatorForms {

C# Hello, i need help adding a working Decimal button to my c# calculator program, thanks

CODE

Form.cs

using System; using System.Windows.Forms;

namespace CalculatorForms { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

int numerator; int denominator; string num=""; string denom=""; bool flag = false; double result;

private void button1_Click(object sender, EventArgs e) { if (flag == false) { num += "1"; } else { denom = "1"; } }

private void button2_Click(object sender, EventArgs e) { if (flag == false) { num += "2"; } else { denom = "2"; } }

private void button3_Click(object sender, EventArgs e) { if (flag == false) { num += "3"; } else { denom = "3"; } }

private void button4_Click(object sender, EventArgs e) { if (flag == false) { num += "4"; } else { denom = "4"; }

}

private void button5_Click(object sender, EventArgs e) { if (flag == false) { num += "5"; } else { denom = "5"; } }

private void button6_Click(object sender, EventArgs e) { if (flag == false) { num += "6"; } else { denom = "6"; } }

private void button7_Click(object sender, EventArgs e) { if (flag == false) { num += "7"; } else { denom = "7"; } }

private void button8_Click(object sender, EventArgs e) { if (flag == false) { num += "8"; } else { denom = "8"; } }

private void button9_Click(object sender, EventArgs e) { if (flag == false) { num += "9"; } else { denom = "9"; } }

private void button11_Click(object sender, EventArgs e) { if (flag == false) { num += "0"; } else { denom = "0"; } }

private void button12_Click(object sender, EventArgs e) { flag = true; }

private void button10_Click(object sender, EventArgs e) { numerator = Convert.ToInt16(num); denominator = Convert.ToInt16(denom); try { result = numerator / denominator; textBox1.Text = Convert.ToString(result); }

catch { MessageBox.Show( "Division by zero is not possible"); }

} } }

Designer code will be:

namespace CalculatorForms { partial class Form1 { ///

/// Required designer variable. /// private System.ComponentModel.IContainer components = null;

///

/// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }

#region Windows Form Designer generated code

///

/// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.button5 = new System.Windows.Forms.Button(); this.button6 = new System.Windows.Forms.Button(); this.button7 = new System.Windows.Forms.Button(); this.button8 = new System.Windows.Forms.Button(); this.button9 = new System.Windows.Forms.Button(); this.button12 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.button10 = new System.Windows.Forms.Button(); this.button11 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(59, 39); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(46, 29); this.button1.TabIndex = 0; this.button1.Text = "1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(152, 39); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(46, 29); this.button2.TabIndex = 1; this.button2.Text = "2"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.Location = new System.Drawing.Point(249, 39); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(46, 29); this.button3.TabIndex = 2; this.button3.Text = "3"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // button4 // this.button4.Location = new System.Drawing.Point(60, 119); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(46, 29); this.button4.TabIndex = 3; this.button4.Text = "4"; this.button4.UseVisualStyleBackColor = true; this.button4.Click += new System.EventHandler(this.button4_Click); // // button5 // this.button5.Location = new System.Drawing.Point(152, 119); this.button5.Name = "button5"; this.button5.Size = new System.Drawing.Size(46, 29); this.button5.TabIndex = 4; this.button5.Text = "5"; this.button5.UseVisualStyleBackColor = true; this.button5.Click += new System.EventHandler(this.button5_Click); // // button6 // this.button6.Location = new System.Drawing.Point(249, 119); this.button6.Name = "button6"; this.button6.Size = new System.Drawing.Size(46, 29); this.button6.TabIndex = 5; this.button6.Text = "6"; this.button6.UseVisualStyleBackColor = true; this.button6.Click += new System.EventHandler(this.button6_Click); // // button7 // this.button7.Location = new System.Drawing.Point(59, 213); this.button7.Name = "button7"; this.button7.Size = new System.Drawing.Size(46, 29); this.button7.TabIndex = 6; this.button7.Text = "7"; this.button7.UseVisualStyleBackColor = true; this.button7.Click += new System.EventHandler(this.button7_Click); // // button8 // this.button8.Location = new System.Drawing.Point(152, 213); this.button8.Name = "button8"; this.button8.Size = new System.Drawing.Size(46, 29); this.button8.TabIndex = 7; this.button8.Text = "8"; this.button8.UseVisualStyleBackColor = true; this.button8.Click += new System.EventHandler(this.button8_Click); // // button9 // this.button9.Location = new System.Drawing.Point(249, 213); this.button9.Name = "button9"; this.button9.Size = new System.Drawing.Size(46, 29); this.button9.TabIndex = 8; this.button9.Text = "9"; this.button9.UseVisualStyleBackColor = true; this.button9.Click += new System.EventHandler(this.button9_Click); // // button12 // this.button12.Location = new System.Drawing.Point(404, 119); this.button12.Name = "button12"; this.button12.Size = new System.Drawing.Size(46, 29); this.button12.TabIndex = 11; this.button12.Text = "/"; this.button12.UseVisualStyleBackColor = true; this.button12.Click += new System.EventHandler(this.button12_Click); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(69, 340); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(37, 13); this.label1.TabIndex = 12; this.label1.Text = "Result"; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(128, 333); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 20); this.textBox1.TabIndex = 13; // // button10 // this.button10.Location = new System.Drawing.Point(387, 219); this.button10.Name = "button10"; this.button10.Size = new System.Drawing.Size(75, 23); this.button10.TabIndex = 14; this.button10.Text = "calculate"; this.button10.UseVisualStyleBackColor = true; this.button10.Click += new System.EventHandler(this.button10_Click); // // button11 // this.button11.Location = new System.Drawing.Point(152, 286); this.button11.Name = "button11"; this.button11.Size = new System.Drawing.Size(46, 23); this.button11.TabIndex = 15; this.button11.Text = "0"; this.button11.UseVisualStyleBackColor = true; this.button11.Click += new System.EventHandler(this.button11_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(494, 384); this.Controls.Add(this.button11); this.Controls.Add(this.button10); this.Controls.Add(this.textBox1); this.Controls.Add(this.label1); this.Controls.Add(this.button12); this.Controls.Add(this.button9); this.Controls.Add(this.button8); this.Controls.Add(this.button7); this.Controls.Add(this.button6); this.Controls.Add(this.button5); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Calculator"; this.ResumeLayout(false); this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button5; private System.Windows.Forms.Button button6; private System.Windows.Forms.Button button7; private System.Windows.Forms.Button button8; private System.Windows.Forms.Button button9; private System.Windows.Forms.Button button12; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button10; private System.Windows.Forms.Button button11; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!