question

RachelBillings-9127 avatar image
0 Votes"
RachelBillings-9127 asked karenpayneoregon answered

MS Forms Restrictions

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

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 _);
 }





5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.