MS Forms Restrictions

Rachel Billings 1 Reputation point
2021-04-14T18:03:16.853+00:00

I want to force the participates to only put a link in a textbox but the restriction are only for numbers; "Equal to" says the value has to be a number. How can I do text? There isn't a custom option in the dropdown.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,922 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,636 Reputation points
    2021-04-15T01:37:49.657+00:00

    Hi RachelBillings-9127,
    Do you want to enter only numbers in the textbox?
    If so, you can determine the value of keychar in the textBox_KeyPress event.
    The keychar corresponding to the numbers 0-9 is 48-57, the decimal point is 46, and the backspace is 8.
    Here is a code example you can refer to.

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)  
    {  
        // Determine whether the key is the type to be input.  
        if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)  
            e.Handled = true;  
      
        //Handling of decimal points.  
        if ((int)e.KeyChar == 46)                           //Decimal point  
        {  
            if (textBox1.Text.Length <= 0)  
                e.Handled = true;   //The decimal point cannot be in the first place  
            else  
            {  
                float f;  
                float oldf;  
                bool b1 = false, b2 = false;  
                b1 = float.TryParse(textBox1.Text, out oldf);  
                b2 = float.TryParse(textBox1.Text + e.KeyChar.ToString(), out f);  
                if (b2 == false)  
                {  
                    if (b1 == true)  
                        e.Handled = true;  
                    else  
                        e.Handled = false;  
                }  
            }  
        }  
    }  
    

    Best Regards,
    Daniel Zhang


    If the response 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

  2. Karen Payne MVP 35,551 Reputation points
    2021-04-15T01:46:30.367+00:00

    Without seeing an example, seems the rule is numbers and period (maybe more).

    NumericTextBox with paste from clipboard restriction

    using System;  
    using System.ComponentModel;  
    using System.Windows.Forms;  
      
    namespace YourNamespace  
    {  
        public class NumericTextBox : TextBox  
        {  
            private int WM_KEYDOWN = 0x0100;  
            int WM_PASTE = 0x0302;  
      
            public override bool PreProcessMessage(ref Message msg)  
            {  
                if (msg.Msg == WM_KEYDOWN)  
                {  
                    var keys = (Keys)msg.WParam.ToInt32();  
      
                    bool numbers =   
                        ((keys >= Keys.D0 && keys <= Keys.D9) ||  
                         (keys >= Keys.OemPeriod) ||  
                         (keys >= Keys.NumPad0 && keys <= Keys.NumPad9)) &&   
                        ModifierKeys != Keys.Shift;  
      
                    bool ctrl = keys == Keys.Control;  
      
                    bool ctrlZ = keys == Keys.Z && ModifierKeys == Keys.Control,  
                        ctrlX = keys == Keys.X && ModifierKeys == Keys.Control,  
                        ctrlC = keys == Keys.C && ModifierKeys == Keys.Control,  
                        ctrlV = keys == Keys.V && ModifierKeys == Keys.Control,  
                        del = keys == Keys.Delete,  
                        bksp = keys == Keys.Back,  
                        arrows = (keys == Keys.Up)  
                        | (keys == Keys.Down)  
                        | (keys == Keys.Left)  
                        | (keys == Keys.Right);  
      
                    if (numbers | ctrl | del | bksp | arrows | ctrlC | ctrlX | ctrlZ)  
                    {  
                        return false;  
                    }  
                    else if (ctrlV)  
                    {  
                        // handle pasting from clipboard  
                        var clipboardData = Clipboard.GetDataObject();  
                        var input = (string)clipboardData.GetData(typeof(string));  
                          
                        foreach (var character in input)  
                        {  
                            if (!char.IsDigit(character)) return true;  
                        }  
                        return false;  
                    }  
                    else  
                    {  
                        return true;  
                    }  
                }  
                else  
                {  
                    return base.PreProcessMessage(ref msg);  
                }  
            }  
            /// <summary>  
            /// Get int value from Text property or 0 if not an int  
            /// </summary>  
            [Browsable(false)]  
            public int AsInt => int.TryParse(Text, out var value) ? value : 0;  
            /// <summary>  
            /// Monitor for non-numeric pasted from clipboard  
            /// </summary>  
            /// <param name="m">Windows message <see cref="Message"/></param>  
            protected override void WndProc(ref Message m)  
            {  
                if (m.Msg == WM_PASTE)  
                {  
                    var clipboardData = Clipboard.GetDataObject();  
                    var input = (string)clipboardData?.GetData(typeof(string));  
                      
                    foreach (var character in input)  
                    {  
                        if (!char.IsDigit(character))  
                        {  
                            m.Result = (IntPtr)0;  
                            return;  
                        }  
                    }  
                }  
      
                base.WndProc(ref m);  
      
            }  
        }  
    }  
      
    

    There is also using a mask edit control or using event driven validation.

    Or after validation by using someTextBox.IsNumeric();

    public static class StringExtensions      
    {  
        public static bool IsNumeric(this string text) => double.TryParse(text, out _);  
    }  
    
    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.