Numeric text block

I am new to programming, and I don’t know much about it, but I am making a calculator, and I want to use a text field that only accepts numbers and decimal numbers, and when the user paste text from the clipboard, the text field deletes any literal characters such like MS calc.

Please take the time to explain each part so that I can recognize or write it and say what to look for.

thanks

EDIT: I will make it more specific:

How can I make a numeric text box in C #? I used a masked text field, but it will not take decimal values.

I read all about overloading the OnKeyPress method so that it corrects any invalid characters, but I don't know how to do this.

+3
source share
13 answers

The easiest way:)

in a Keypress event in a text box


if ((e.KeyChar <= 57 && e.KeyChar >= 48) || e.KeyChar == 13 || e.KeyChar == 8)
{
}
else
{
     e.Handled = true;
}

+4
source

Add an event handler for the text field, which should be only numeric, and add the following code:

private void textBoxNumbersOnly_KeyPress(object sender, KeyPressEventArgs e)
{
   if (Char.IsDigit(e.KeyChar) || e.KeyChar == '\b')
   {
      e.Handled = false;
   }
   else
   {
      e.Handled = true;
   }
}

This allows you to use numbers from 0 to 9, as well as spaces (useful IMHO). Allow through. character if you want to support decimal places

+11
source

, Windows , ( ). .

, KeyPressed KeyDown .

+7

, : NumericUpDown. .

+2
        if ("1234567890".IndexOf(e.KeyChar.ToString()) > 0)
        {
            e.Handled = false;
        }
        else
        {
            e.Handled = true;
        }
+1

, vb.net

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    Dim reg As New System.Text.RegularExpressions.Regex("[^0-9_ ]")
    TextBox1.Text = reg.Replace(TextBox1.Text, "")
End Sub

0

MaskedTextBox.

, . , , " ". ( ", , ..." ), .

0

, .NET Framework ( , 2.0) , . :

  • , . , .
  • KeyPress charCode . , .
0

, , , .

:

for (each item in the input string) {
   if (!match(some regular expression, item)) {
        toss it out
   } else {
        add item to text box or whatever you were going to do with it
   }

}
0

, (?) . , double , .

0

, , (rightclick paste) .: D

 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        string original = (sender as TextBox).Text;
        if (!char.IsDigit(e.KeyChar))
        {
            e.Handled = true;
        }
        if (e.KeyChar == '.')
        {
            if (original.Contains('.'))
                e.Handled = true;
            else if (!(original.Contains('.')))
                e.Handled = false;

        }
        else if (char.IsDigit(e.KeyChar)||e.KeyChar=='\b')
        {
            e.Handled = false;
        }

    }
0

, mahasen, . , . , Toolbox, Form.

using System;
using System.Linq;
using System.Windows.Forms;

namespace MyApp.GUI
{
    public class FilteredTextBox : TextBox
    {
        // Fields
        private char[] m_validCharacters;
        private string m_filter;
        private event EventHandler m_maxLength;

        // Properties
        public string Filter
        {
            get
            {
                return m_filter;
            }
            set
            {
                m_filter = value;
                m_validCharacters = value.ToCharArray();
            }
        }

        // Constructor
        public FilteredTextBox()
        {
            m_filter = "";
            this.KeyPress += Validate_Char_OnKeyPress;
            this.TextChanged += Check_Text_Length_OnTextChanged;
        }

        // Event Hooks
        public event EventHandler TextBoxFull
        {
            add { m_maxLength += value; }
            remove { m_maxLength -= value; }
        }

        // Methods
        void Validate_Char_OnKeyPress(object sender, KeyPressEventArgs e)
        {
            if (m_validCharacters.Contains(e.KeyChar) || e.KeyChar == '\b')
                e.Handled = false;
            else
                e.Handled = true;
        }
        void Check_Text_Length_OnTextChanged(object sender, EventArgs e)
        {
            if (this.TextLength == this.MaxLength)
            {
                var Handle = m_maxLength;
                if (Handle != null)
                    Handle(this, EventArgs.Empty);
            }
        }
    }
}

, , , , 3 Form TextBoxFull . 4 IP-. ...

    private bool ValidateAddressChunk(string p_text)
    {
        byte AddressChunk = new byte();
        return byte.TryParse(p_text, out AddressChunk);
    }
    private void filteredTextBox1_TextBoxFull(object sender, EventArgs e)
    {
        var Filtered_Text_Box = (FilteredTextBox)sender;

        if (!ValidateAddressChunk(Filtered_Text_Box.Text))
            filteredTextBox1.Text = "255";
        else
            filteredTextBox2.Focus();
    }
    private void filteredTextBox2_TextBoxFull(object sender, EventArgs e)
    {
        var Filtered_Text_Box = (FilteredTextBox)sender;

        if (!ValidateAddressChunk(Filtered_Text_Box.Text))
            filteredTextBox2.Text = "255";
        // etc.
    }
0

As a newbie, you might be better off investing in good third-party tools. Telerik's Radcontrols, for example, has a numeric text field that will do what you are looking for.

-1
source

Source: https://habr.com/ru/post/1702653/


All Articles