Special characters in a textbox

Shaifali jain 420 Reputation points
2023-09-22T09:07:13.48+00:00

Hi ,

i have crated a textbox which shows current date . i want that user cannot enter special characters inside that except - or / . can you please suggest how to do it .

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,884 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,843 questions
0 comments No comments
{count} votes

Accepted answer
  1. Brian Zarb 1,650 Reputation points
    2023-09-22T09:47:07.42+00:00

    Here’s an example of how you can set up a MaskedTextBox to allow date input in the format yyyy-MM-dd or yyyy/MM/dd:

    using System;
    using System.Windows.Forms;
    namespace WindowsFormsApp
    {
        public partial class MainForm : Form
        {
            public MainForm()
            {
                InitializeComponent();
                // Create a MaskedTextBox
                MaskedTextBox maskedTextBox = new MaskedTextBox
                {
                    // Set the mask to accept date in yyyy-MM-dd or yyyy/MM/dd format
                    Mask = "0000-00-00",
                    // Set the prompt character to a space
                    PromptChar = ' ',
                    // Set the valid input to digits and the date separator characters
                    ValidChars = "0123456789-/",
                    // Set the location and size of the MaskedTextBox
                    Location = new System.Drawing.Point(50, 50),
                    Size = new System.Drawing.Size(100, 20)
                };
                // Add the MaskedTextBox to the form
                Controls.Add(maskedTextBox);
            }
        }
    }
    

    In this example, we've set the mask to "0000-00-00" to allow input in the yyyy-MM-dd format. We've also set the ValidChars property to "0123456789-/", which means the user can only enter digits and the '-' or '/' characters. The PromptChar is set to a space, which will be displayed in place of the mask characters until the user enters something.

    You can also use the TypeValidationCompleted event to perform additional validation on the input and provide feedback to the user.


2 additional answers

Sort by: Most helpful
  1. Olaf Helper 44,296 Reputation points
    2023-09-22T09:11:42.3933333+00:00

    USe a MaskedTextBox Class, you can define the valid/allowed entry format.


  2. KOZ6.0 6,395 Reputation points
    2023-09-22T16:30:02.72+00:00

    If you use TextBox, input only numbers. Edit when focus leaves the control.

    If 20230922 is entered, edit it to 2023/09/22.

    Completion functions are also nice.

    22 → Complete year and month → 2023/09/22

    922 → Complement year → 2023/09/22

    In that case, the six months before and after the system date will be used as the reference point.

    For example, if you enter December in January, it will be December of the previous year.

    Conversely, if you enter January in December, it will be set as January of the next year.

    It is also interesting to paste a Button control inside a TextBox and give it a function like DateTimePicker.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.