Page 1 of 2

Macro Wizard / Form >> TextField Limit to a number range?

PostPosted: Thu Mar 14, 2019 9:44 pm
by Robertspark
Odd ball question----

Macro Wizard / Form >> TextField Limit to a number range?

I know how to limit a textfield to 2 digits and I know how to limit to numbers only.

But, is there a way to limit the typed range to say 0-12??

Thanks,

Re: Macro Wizard / Form >> TextField Limit to a number range

PostPosted: Thu Mar 14, 2019 10:13 pm
by Vmax549
Set up text box validation and then define your parameters.

Just a thought, (;-) TP

Re: Macro Wizard / Form >> TextField Limit to a number range

PostPosted: Thu Mar 14, 2019 10:22 pm
by Robertspark
Well I got this far:

Code: Select all
   void textBox1_KeyPress(object sender, KeyPressEventArgs e)
      {
         if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) )
         {
            e.Handled = true;
         }
      }

Re: Macro Wizard / Form >> TextField Limit to a number range

PostPosted: Thu Mar 14, 2019 10:35 pm
by Vmax549
Textbox validation is your friend ;)

private void textBox1_Validating(object sender, CancelEventArgs e)
{
if (textBox1.Text != "something")
e.Cancel = true;
}

Re: Macro Wizard / Form >> TextField Limit to a number range

PostPosted: Thu Mar 14, 2019 11:04 pm
by Dan911
Hmmm, don't think you answered his question.

Don't let textbox value > 12. I have a routine but want to see what you come up with first. Don't worry about posting something silly you can just delete.

Re: Macro Wizard / Form >> TextField Limit to a number range

PostPosted: Thu Mar 14, 2019 11:17 pm
by Robertspark
The validating didn't work

I've had a few goes along the lines of:

Code: Select all
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                if (textBox1.Text == "0" || textBox1.Text == "1" || textBox1.Text == "3")
                {
                    e.Handled = true;
                }
            }
        }



Code: Select all
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                int i = Convert.ToInt32(textBox1.Text);
                if (i >=0 && i >= 12)
                {
                    e.Handled = true;
                }
            }
        }

Re: Macro Wizard / Form >> TextField Limit to a number range

PostPosted: Thu Mar 14, 2019 11:23 pm
by Robertspark
I guess I could always use a numeric UP Down control....

Re: Macro Wizard / Form >> TextField Limit to a number range

PostPosted: Thu Mar 14, 2019 11:25 pm
by Robertspark
I'll use the numeric up down it will meet my needs easier for what I want to do

Re: Macro Wizard / Form >> TextField Limit to a number range

PostPosted: Thu Mar 14, 2019 11:31 pm
by Dan911
Hey Rob, possibly only allowing digits and using index of textbox and add should work

textBox.Text.Substring(index, 0) + textBox.Text.Substring(index, 1)

of course do your conversions along the way.

Re: Macro Wizard / Form >> TextField Limit to a number range

PostPosted: Fri Mar 15, 2019 7:31 am
by Dan911
Just wanting to limit value in textbox 0-12 you can just use the textbox MaxLength property for char amount and set to 2 and just check for 11. If allowing a decimal it can get a little more tricky.
Code: Select all
textbox.MaxKength = 2;