What's new
  • Welcome to PHCorner forums. Take a moment to sign up and gain unlimited access and extra privileges that guests are not entitled to, such as: All that and more! Registration is quick, simple and absolutely free. Join our community today!

C# Help po Calculator

Murrzz

Established
Gumawa po ako ng Calculator using C# hindi ko po magawa yung gantong operations 3+4+5-6*3 , Yung nagawa ko po is click pa yung '=' sign to compute.
Ito po yung pic:
1613629917240.png


Ito po yung Code ko
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
    public partial class Form1 : Form
    {
        int val=0;
        char op;
       Boolean  cho =true ;

        public Form1()
        {
            InitializeComponent();
        }

      

        private void btnAdd_Click(object sender, EventArgs e)
        {

                op = '+';
                val = int.Parse(txtVal.Text);
                txtVal.Text = "";
            
        
        }
      
        private void btnSubt_Click(object sender, EventArgs e)
        {
          
                op = '-';
                val = int.Parse(txtVal.Text);
                txtVal.Text = "";
          
        }

        private void btnDiv_Click(object sender, EventArgs e)
        {
            
                op = '/';
                val = int.Parse(txtVal.Text);
                txtVal.Text = "";
        

        }

        private void btnMult_Click(object sender, EventArgs e)
        {
        
                op = '*';
                val = int.Parse(txtVal.Text);
                txtVal.Text = "";
        
        }
        private void btnEqual_Click(object sender, EventArgs e)
        {
            Condition();
        }
        public void Condition()
        {
            if (cho == true)
            {
                if (op == '+')
                {
                    val += int.Parse(txtVal.Text);
                    txtVal.Text = val.ToString();
                    
                }
                else if (op == '-')
                {
                    val = val - int.Parse(txtVal.Text);
                    txtVal.Text = val.ToString();
                    
                }
                else if (op == '/')
                {
                    val = val / int.Parse(txtVal.Text);
                    txtVal.Text = val.ToString();
                   
                }
                else if (op == '*')
                {
                    val = val * int.Parse(txtVal.Text);
                    txtVal.Text = val.ToString();
                    
                }
            }
        }

      
      
    
    }
}
 

Attachments

Murrzz

Established
Gawa ka ng void method for calculation then pag click mo ng buttong call mo lang yung method
Meron na sir
Condition()
Then tinawag ko sa Loob ng = na button
 
1 Comment
Z
zackmark29
gawin mo nalang, ilagay mo yung Condition method sa bawat button

example:


C#:
private void btnMult_Click(object sender, EventArgs e)
{
    op = '*';
    val = int.Parse(txtVal.Text);
    txtVal.Text = "";
    Condition();

}
 

Murrzz

Established
Yung na Encounter kong Problem is when I inputted first number(First Input) and click the operator is nag error sya kasi nilagay ko yung Condition() sa loob ng Operator Buttons pra hindi nako mag click ng equal button, but nag error sya kasi wala pang laman yung Second Number(Second Input ) at nag execute agad sya.
So
binago ko yung approach sa operator buttons so I have two container the var Val and the txtVal(The textbox)
so Kaylangan kong gawin is when i Inputted the first number is hindi sya mag execute agad so I made these code
C#:
  private void btnAdd_Click(object sender, EventArgs e)
        {
            if (val != 0)
            {
                btnEqual.PerformClick();
                op = "+";
                txtVal.Text = "";
            }
            else
            {
                op = "+";
                val = int.Parse(txtVal.Text);
                txtVal.Text = "";
            }   
 
        }
bali ito lang yung sagot dun sa tanong ko hahaha
 
X
Xlayer

Online statistics

Members online
2,122
Guests online
1,917
Total visitors
4,039

Forum statistics

Threads
1,070,752
Messages
18,106,299
Members
1,515,651
Comments
1,076,383
Attachments
1,385,831
Profile posts
97,569
Top