How to write code to limit textbox input value to letter/number only?

VAer-4038 766 Reputation points
2021-01-02T15:34:53.177+00:00

There are 26 lowercase letters, 26 uppercase letters, and 10 digital numbers (0, 1, 2,.... 9)

I would like the textbox value limit to these 62 characters.

Here is the ASCII Reference: ref_html_ascii.asp

Three questions:

  1. Attached screenshot is from Excel textbox property, there is ControlTipText, I would like to do the same thing for Visual Studio textbox, but I could not find it in textbox property. Where is it?
  2. Attached code structure is just my logic, it is more in VBA format. How should I write C# based on the code structure logic? I would like to put the code in TextBox_Change(), how to write TextBox_Change() in Visual Studio?
  3. I will need to use the code to run on many textbox, so I would like to put the code somewhere, something like function, then called by many textbox. How to write it, where to put the code, how to call it for each textbox?

52800-textboxletternumber.jpg

Private Sub TextBoxUsername_Change()  
With TextBoxUsername  
        If Right(.Text, 1) = " " Then  
            MsgBox "Error: Space is not allowed.", vbExclamation + vbOK  
            .Text = Trim(.Text)  
 Elseif Asc(Right(.Text, 1)) <48 OR ( Asc(Right(.Text, 1)) > 57 AND Asc(Right(.Text, 1)) < 65  )  OR ( Asc(Right(.Text, 1)) > 90 AND Asc(Right(.Text, 1)) < 97  ) OR Asc(Right(.Text, 1)) > 122 Then  
 MsgBox "Error: Please only enter letter and/or number.", vbExclamation + vbOK  
            //I need a line of code to remove invalid character. Maybe I can use below logic. Check the length of textbox value, then remove the one on the right, the one is invalid character, which is just entered.  
 .Text = Left(TextBoxUser.Value, Len(TextBoxUser.Value)-1)  
  
        End If  
  
  
  
  
    End With  
End Sub  
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,204 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 85,956 Reputation points
    2021-01-02T16:40:10.237+00:00

    A way is to use an API from the Shell, which has a built-in Tooltip =>

    Test with a textBox1 TextBox :

    SHLimit-Input-Test.jpg

    LIMITINPUT li = new LIMITINPUT();
    li.pszFilter = Marshal.StringToHGlobalAuto("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + (char)8);
    // li.pszFilter = (IntPtr)(LICF_DIGIT | LICF_UPPER | LICF_LOWER | LICF_CNTRL);
    li.pszMessage = Marshal.StringToHGlobalAuto("Please enter Letter or Number");
    li.pszTitle = Marshal.StringToHGlobalAuto("Information");
    li.dwFlags = LIF_INCLUDEFILTER ;
    // li.dwFlags = LIF_INCLUDEFILTER | LIF_CATEGORYFILTER;
    li.dwMask = LIM_FLAGS | LIM_FILTER | LIM_MESSAGE | LIM_TITLE;
    HRESULT hr = SHLimitInput(textBox1.Handle, ref li);
    

    Declarations :

    public enum HRESULT : uint
    {
        S_OK = 0,
        S_FALSE = 1,
        E_NOINTERFACE = 0x80004002,
        E_NOTIMPL = 0x80004001,
        E_FAIL = 0x80004005,
        E_INVALIDARG = 0x80070057,
        E_UNEXPECTED = 0x8000FFFF
    }
    
    [DllImport("Shell32.dll", SetLastError = true, CharSet = CharSet.Auto, EntryPoint = "#754")]
    public static extern HRESULT SHLimitInput(IntPtr hWndEdit, ref LIMITINPUT pli);
    
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct LIMITINPUT
    {
        public int cbSize;
        public int dwMask;
        public int dwFlags;
        public IntPtr hinst;
        public IntPtr pszFilter;
        public IntPtr pszTitle;
        public IntPtr pszMessage;
        public IntPtr hIcon;
        public IntPtr hWndNotify;
        public int nTooltipTimeout;
        public int nTooltipWidth;
    }
    
    public const int LIF_INCLUDEFILTER = 0x0;
    public const int LIF_EXCLUDEFILTER = 0x1;
    public const int LIF_CATEGORYFILTER = 0x2;
    public const int LIF_HIDETIPONVALID = 0x200;
    
    public const int LIM_FLAGS = 0x1;
    public const int LIM_FILTER = 0x2;
    public const int LIM_HINST = 0x8;
    public const int LIM_TITLE = 0x10;
    public const int LIM_MESSAGE = 0x20;
    public const int LIM_ICON = 0x40;
    
    public const int LICF_UPPER = 0x1;
    public const int LICF_LOWER = 0x2;
    public const int LICF_DIGIT = 0x4;
    public const int LICF_SPACE = 0x8;
    public const int LICF_PUNCT = 0x10;
    public const int LICF_CNTRL = 0x20;
    public const int LICF_BLANK = 0x40;
    public const int LICF_XDIGIT = 0x80;
    public const int LICF_ALPHA = 0x100;
    

  2. Dylan Zhu-MSFT 6,421 Reputation points
    2021-01-04T05:34:28.793+00:00

    Hi VAer,

    >Most importantly, I need to put the code somewhere, which will be called by many textbox from multiple form. Where to put the code?

    You can create a class into your project, like this:
    53044-image.png

    Then call this public method in the form you want: 53107-image.png Best Regards, Dylan

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our * *documentation* to enable e-mail notifications if you want to receive the related email notification for this thread.**

    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.