Click here to Skip to main content
15,911,306 members
Please Sign up or sign in to vote.
2.60/5 (5 votes)
See more:
C#
namespace shuvo
{
    public partial class Form1 : Form
    {
        bool div = false;
        bool multply = false;
        bool minus = false;
        bool plus = false;
        bool equal = false;
        bool pluseminuse = false;

        public Form1()

        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CheckifEqual();
            textBox1.Text = textBox1.Text + "0";
        }

        private void CheckifEqual()
        {
            if (equal)
            {
                textBox1.Text = "";
                equal = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CheckifEqual();
            textBox1.Text = textBox1.Text + "1";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CheckifEqual();
            textBox1.Text = textBox1.Text + "2";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            CheckifEqual();
            textBox1.Text = textBox1.Text + "3";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            CheckifEqual();
            textBox1.Text = textBox1.Text + "4";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            CheckifEqual();
            textBox1.Text = textBox1.Text + "5";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            CheckifEqual();
            textBox1.Text = textBox1.Text + "6";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            CheckifEqual();
            textBox1.Text = textBox1.Text + "7";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            CheckifEqual();
            textBox1.Text = textBox1.Text + "8";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            CheckifEqual();
            textBox1.Text = textBox1.Text + "9";
        }

        private void buttoncon_Click(object sender, EventArgs e)
        {
            CheckifEqual();
            if (textBox1.Text.Contains("."))
            {
                return;
            }
            else
            {
                textBox1.Text = textBox1.Text + ".";
            }
        }




        private void buttondiv_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                div = true;
                textBox1.Tag = textBox1.Text;
                textBox1.Text = "";

            }
        }

        private void buttonminus_Click(object sender, EventArgs e)
        {
            if(textBox1.Text=="")
            {
                return;
            }
            else
            {
                minus=true;
                textBox1.Tag=textBox1.Text;
                textBox1.Text="";

            }
        }

        private void buttonplus_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                plus = true;
                textBox1.Tag = textBox1.Text;
                textBox1.Text = "";
            }
        }

        private void buttonequal_Click(object sender, EventArgs e)
        {
            equal = true;
            if (div)
            {
                decimal doc = Convert.ToDecimal(textBox1.Tag) / Convert.ToDecimal(textBox1.Text);
                textBox1.Text = doc.ToString();
            }
            if (plus)
            {
                decimal doc = Convert.ToDecimal(textBox1.Tag) + Convert.ToDecimal(textBox1.Text);
                textBox1.Text = doc.ToString();
            }
            if (minus)
            {
                decimal doc = Convert.ToDecimal(textBox1.Tag) - Convert.ToDecimal(textBox1.Text);
                textBox1.Text = doc.ToString();
            }
            if (multply)
            {
                decimal doc = Convert.ToDecimal(textBox1.Tag) * Convert.ToDecimal(textBox1.Text);
                textBox1.Text = doc.ToString();
            }
        }

        private void buttonpluseminuse_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Contains("-"))
            {
                textBox1.Text = textBox1.Text.Remove(0, 1);
            }
            else
            {
                textBox1.Text = "-" + textBox1.Text;
            }
        }

        private void buttonmultply_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                return;
            }
            else
            {
                multply = true;
                textBox1.Tag = textBox1.Text;
                textBox1.Text = "";
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            plus = minus = multply = div=equal = false;
            textBox1.Text = "";
            textBox1.Tag = "";
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
Posted

Try
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
      {
          if (!(e.KeyChar >= '0' && e.KeyChar <= '9'))
              e.Handled = true;
      }
 
Share this answer
 
Comments
shakil442 28-May-11 2:42am    
thank you very much.
another question division 6/0 then message continue,quit,details. but i am wanted to can't to divide by zero message only.
First off, don't just dump your code: post only the relevant fragments, rather than the whole program.
Secondly, stop using the default name for things. Having a button1 which is the zero, and button7 which is the six, is lazy, stupid and unnecessarily confusing.
Thirdly, stop duplicating code. It's is wasteful, time consuming, and bad practice. Instead, write a single generic routine which handles them all the same way. Set each numeric buttons Tag property to the appropriate digit, then set all of them to have the same click handler:
private void NumericButton_Click(object sender, EventArgs e)
   {
   Button b = sender as Button;
   if (b != null)
      {
      CheckifEqual();
      textBox1.Text = textBox1.Text + b.Tag.ToString();
      }
   }
Now, when you need to make a change, you only have to do it in one place!

When you have done all that, use the Improve Question widget to change your question - and this time, try to explain what your problem is?
 
Share this answer
 
Comments
[no name] 27-May-11 15:55pm    
Have a 5, cause really he didn't even post a question but yet you gave him sound advice :D
Sergey Alexandrovich Kryukov 18-Jun-11 20:20pm    
Agree with all that. I tried it to the other member, no success at all. Hope for more luck in this case, vote 5 for the nice try.
--SA
I thing your are trying to validate the input.Try to use regular expressions to validate .check following link
Validation with Regular Expressions Made Simple[^]
 
Share this answer
 
Check out MaskedTextBox[^]. It is a TextBox control that supports a declarative syntax for accepting or rejecting user input.

You can define how a input is handled.
 
Share this answer
 
hi,

this will help you..

//-----------------------------only number

XML
<script type="text/javascript">
        function onlyNumbers(event) {
            var e = event || evt; // for trans-browser compatibility
            var charCode = e.which || e.keyCode;
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
            return true;
        }
    </script>
//In page load of the aspx page
   txtAcStNo.AddTextBoxAttribute("onkeypress", "return onlyNumbers(event);");
 
Share this answer
 
Use filtered textbox like,-
<cc1:filteredtextboxextender id="FilteredTextBoxExtender4" runat="server" filtertype="Numbers" xmlns:cc1="#unknown">
                                                            TargetControlID="txtGsmNo">
                                                        </cc1:FilteredTextBoxExtender></cc1:filteredtextboxextender>


Or have a look on this code,-

Text box to accept only number[^]

Hope this can help you.
 
Share this answer
 
Comments
RaviRanjanKr 28-May-11 1:42am    
OP using Windows Application so it will not work :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900