Thread: lame calculator problem..

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    18

    Talking lame calculator problem..

    im pretty new to C# and am trying to work on a simple calculator, however, i can't figure out what is wrong with what i have, here is the code:

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace WindowsApplication9
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.Label label1;
    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;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    //
    // TODO: Add any constructor code after InitializeComponent call
    //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.textBox3 = new System.Windows.Forms.TextBox();
    this.label1 = new System.Windows.Forms.Label();
    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.SuspendLayout();
    //
    // textBox1
    //
    this.textBox1.Location = new System.Drawing.Point(16, 8);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(48, 20);
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "";
    //
    // textBox2
    //
    this.textBox2.Location = new System.Drawing.Point(176, 8);
    this.textBox2.Name = "textBox2";
    this.textBox2.Size = new System.Drawing.Size(48, 20);
    this.textBox2.TabIndex = 1;
    this.textBox2.Text = "";
    //
    // textBox3
    //
    this.textBox3.Location = new System.Drawing.Point(64, 32);
    this.textBox3.Name = "textBox3";
    this.textBox3.Size = new System.Drawing.Size(120, 20);
    this.textBox3.TabIndex = 6;
    this.textBox3.Text = "";
    //
    // label1
    //
    this.label1.Location = new System.Drawing.Point(16, 32);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(48, 16);
    this.label1.TabIndex = 7;
    this.label1.Text = "Answer";
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(184, 32);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(40, 24);
    this.button1.TabIndex = 8;
    this.button1.Text = "Calc";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // button2
    //
    this.button2.Location = new System.Drawing.Point(72, 8);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(24, 23);
    this.button2.TabIndex = 9;
    this.button2.Text = "+";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    //
    // button3
    //
    this.button3.Location = new System.Drawing.Point(96, 8);
    this.button3.Name = "button3";
    this.button3.Size = new System.Drawing.Size(24, 23);
    this.button3.TabIndex = 10;
    this.button3.Text = "-";
    this.button3.Click += new System.EventHandler(this.button3_Click);
    //
    // button4
    //
    this.button4.Location = new System.Drawing.Point(144, 8);
    this.button4.Name = "button4";
    this.button4.Size = new System.Drawing.Size(24, 23);
    this.button4.TabIndex = 11;
    this.button4.Text = "/";
    this.button4.Click += new System.EventHandler(this.button4_Click);
    //
    // button5
    //
    this.button5.Location = new System.Drawing.Point(120, 8);
    this.button5.Name = "button5";
    this.button5.Size = new System.Drawing.Size(24, 23);
    this.button5.TabIndex = 12;
    this.button5.Text = "*";
    this.button5.Click += new System.EventHandler(this.button5_Click);
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(248, 61);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
    this.button5,
    this.button4,
    this.button3,
    this.button2,
    this.button1,
    this.label1,
    this.textBox3,
    this.textBox2,
    this.textBox1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }

    int method;

    private void button4_Click(object sender, System.EventArgs e)
    {
    method = 4;
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
    method = 1;
    }

    private void button3_Click(object sender, System.EventArgs e)
    {
    method = 2;
    }

    private void button5_Click(object sender, System.EventArgs e)
    {
    method = 3;
    }
    private void button1_Click(object sender, System.EventArgs e)
    {
    int left;
    int right;
    int sum;
    left = int.Parse(textBox1.Text); //change the text to a number
    right = int.Parse(textBox2.Text);

    if (method == 1)
    {
    sum = left + right;
    textBox3.Text = sum.ToString();
    }
    if (method == 2)
    {
    sum = left - right;
    textBox3.Text = sum.ToString();
    }
    if (method == 3)
    {
    sum = left * right;
    textBox3.Text = sum.ToString();
    }
    if (method == 4)
    {
    sum = left / right;
    textBox3.Text = sum.ToString();
    }
    else
    {
    textBox3.Text = "Error";
    }


    }

    }
    }


    Thanks for your time and help

  2. #2
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    Try -

    Code:
    using System; 
    using System.Drawing; 
    using System.Collections; 
    using System.ComponentModel; 
    using System.Windows.Forms; 
    using System.Data; 
    
    namespace WindowsApplication9 
    { 
    	/// <summary> 
    	/// Summary description for Form1. 
    	/// </summary> 
    	public class Form1 : System.Windows.Forms.Form 
    	{ 
    		private int method;
    		private System.Windows.Forms.TextBox textBox1; 
    		private System.Windows.Forms.TextBox textBox2; 
    		private System.Windows.Forms.TextBox textBox3; 
    		private System.Windows.Forms.Label label1; 
    		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; 
    		/// <summary> 
    		/// Required designer variable. 
    		/// </summary> 
    		private System.ComponentModel.Container components = null; 
    
    		public Form1() 
    		{ 
    			// 
    			// Required for Windows Form Designer support 
    			// 
    			InitializeComponent(); 
    
    			// 
    			// TODO: Add any constructor code after InitializeComponent call 
    			// 
    		} 
    
    		/// <summary> 
    		/// Clean up any resources being used. 
    		/// </summary> 
    		protected override void Dispose( bool disposing ) 
    		{ 
    			if( disposing ) 
    			{ 
    				if (components != null) 
    				{ 
    					components.Dispose(); 
    				} 
    			} 
    			base.Dispose( disposing ); 
    		} 
    
    #region Windows Form Designer generated code 
    		/// <summary> 
    		/// Required method for Designer support - do not modify 
    		/// the contents of this method with the code editor. 
    		/// </summary> 
    		private void InitializeComponent() 
    		{ 
    			this.textBox1 = new System.Windows.Forms.TextBox(); 
    			this.textBox2 = new System.Windows.Forms.TextBox(); 
    			this.textBox3 = new System.Windows.Forms.TextBox(); 
    			this.label1 = new System.Windows.Forms.Label(); 
    			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.SuspendLayout(); 
    			// 
    			// textBox1 
    			// 
    			this.textBox1.Location = new System.Drawing.Point(16, 8); 
    			this.textBox1.Name = "textBox1"; 
    			this.textBox1.Size = new System.Drawing.Size(48, 20); 
    			this.textBox1.TabIndex = 0; 
    			this.textBox1.Text = ""; 
    			// 
    			// textBox2 
    			// 
    			this.textBox2.Location = new System.Drawing.Point(176, 8); 
    			this.textBox2.Name = "textBox2"; 
    			this.textBox2.Size = new System.Drawing.Size(48, 20); 
    			this.textBox2.TabIndex = 1; 
    			this.textBox2.Text = ""; 
    			// 
    			// textBox3 
    			// 
    			this.textBox3.Location = new System.Drawing.Point(64, 32); 
    			this.textBox3.Name = "textBox3"; 
    			this.textBox3.Size = new System.Drawing.Size(120, 20); 
    			this.textBox3.TabIndex = 6; 
    			this.textBox3.Text = ""; 
    			// 
    			// label1 
    			// 
    			this.label1.Location = new System.Drawing.Point(16, 32); 
    			this.label1.Name = "label1"; 
    			this.label1.Size = new System.Drawing.Size(48, 16); 
    			this.label1.TabIndex = 7; 
    			this.label1.Text = "Answer"; 
    			// 
    			// button1 
    			// 
    			this.button1.Location = new System.Drawing.Point(184, 32); 
    			this.button1.Name = "button1"; 
    			this.button1.Size = new System.Drawing.Size(40, 24); 
    			this.button1.TabIndex = 8; 
    			this.button1.Text = "Calc"; 
    			this.button1.Click += new System.EventHandler(this.button1_Click); 
    			// 
    			// button2 
    			// 
    			this.button2.Location = new System.Drawing.Point(72, 8); 
    			this.button2.Name = "button2"; 
    			this.button2.Size = new System.Drawing.Size(24, 23); 
    			this.button2.TabIndex = 9; 
    			this.button2.Text = "+"; 
    			this.button2.Click += new System.EventHandler(this.button2_Click); 
    			// 
    			// button3 
    			// 
    			this.button3.Location = new System.Drawing.Point(96, 8); 
    			this.button3.Name = "button3"; 
    			this.button3.Size = new System.Drawing.Size(24, 23); 
    			this.button3.TabIndex = 10; 
    			this.button3.Text = "-"; 
    			this.button3.Click += new System.EventHandler(this.button3_Click); 
    			// 
    			// button4 
    			// 
    			this.button4.Location = new System.Drawing.Point(144, 8); 
    			this.button4.Name = "button4"; 
    			this.button4.Size = new System.Drawing.Size(24, 23); 
    			this.button4.TabIndex = 11; 
    			this.button4.Text = "/"; 
    			this.button4.Click += new System.EventHandler(this.button4_Click); 
    			// 
    			// button5 
    			// 
    			this.button5.Location = new System.Drawing.Point(120, 8); 
    			this.button5.Name = "button5"; 
    			this.button5.Size = new System.Drawing.Size(24, 23); 
    			this.button5.TabIndex = 12; 
    			this.button5.Text = "*"; 
    			this.button5.Click += new System.EventHandler(this.button5_Click); 
    			// 
    			// Form1 
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
    			this.ClientSize = new System.Drawing.Size(248, 61); 
    			this.Controls.AddRange(new System.Windows.Forms.Control[] { 
    																		  this.button5, 
    																		  this.button4, 
    																		  this.button3, 
    																		  this.button2, 
    																		  this.button1, 
    																		  this.label1, 
    																		  this.textBox3, 
    																		  this.textBox2, 
    																		  this.textBox1}); 
    			this.Name = "Form1"; 
    			this.Text = "Form1"; 
    			this.ResumeLayout(false); 
    
    		} 
    #endregion 
    
    		/// <summary> 
    		/// The main entry point for the application. 
    		/// </summary> 
    		[STAThread] 
    		static void Main() 
    		{ 
    			Application.Run(new Form1()); 
    		} 
    
    		 
    
    		private void button4_Click(object sender, System.EventArgs e) 
    		{ 
    			method = 4; 
    		} 
    
    		private void button2_Click(object sender, System.EventArgs e) 
    		{ 
    			method = 1; 
    		} 
    
    		private void button3_Click(object sender, System.EventArgs e) 
    		{ 
    			method = 2; 
    		} 
    
    		private void button5_Click(object sender, System.EventArgs e) 
    		{ 
    			method = 3; 
    		} 
    		private void button1_Click(object sender, System.EventArgs e) 
    		{ 
    			int left, right, sum; 
    			left = int.Parse(textBox1.Text); //change the text to a number 
    			right = int.Parse(textBox2.Text); 
    
    			if (method == 1) 
    			{ 
    				sum = left + right; 
    				textBox3.Text = sum.ToString(); 
    			} 
    			else if (method == 2) 
    			{ 
    				sum = left - right; 
    				textBox3.Text = sum.ToString(); 
    			} 
    			else if (method == 3) 
    			{ 
    				sum = left * right; 
    				textBox3.Text = sum.ToString(); 
    			} 
    			else if (method == 4) 
    			{ 
    				sum = left / right; 
    				textBox3.Text = sum.ToString(); 
    			} 
    			else 
    			{ 
    				textBox3.Text = "Error"; 
    			} 
    
    
    		} 
    
    	} 
    }

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    thanks ginoitalo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates and Macros plus more...
    By Monkeymagic in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2007, 05:53 PM
  2. Lame question on dialog controls appearance
    By Templario in forum Windows Programming
    Replies: 2
    Last Post: 03-18-2003, 08:22 PM
  3. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM
  4. First Solo Program: Lame
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-27-2002, 01:29 PM