我想给文本框没有输入字符.只放号码 [英] i want to give text box no input character. only put the number

查看:68
本文介绍了我想给文本框没有输入字符.只放号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)
        {

        }

    }
}

推荐答案

数字文本框:允许您的用户轻松输入数字数据 [ ^ ]
TNumEditBox:在C#应用程序中具有ContextMenu的数字编辑控件 [
Numeric TextBox : Allow your users to enter numeric data the easy way[^]
TNumEditBox: A Numeric Edit Control with ContextMenu in C# Applications[^]


尝试
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
      {
          if (!(e.KeyChar >= '0' && e.KeyChar <= '9'))
              e.Handled = true;
      }


首先,不要只是转储代码:仅发布相关的片段,而不发布整个程序.
其次,停止使用事物的默认名称.拥有一个为零的button1和一个为六个的button7是懒惰的,愚蠢的并且不必要地造成混淆.
第三,停止重复代码.这是浪费,费时和不正确的做法.而是编写一个通用例程,以相同的方式处理它们.将每个数字按钮的Tag属性设置为适当的数字,然后将所有数字按钮设置为具有相同的单击处理程序:
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?


这篇关于我想给文本框没有输入字符.只放号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆