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

This is where you talk about Macros, show examples of your macro scripting and SHARE handy segments of script code as examples.

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

Postby Robertspark » Fri Mar 15, 2019 7:45 am

Thanks Dan

Yes there is a bit that you did not know as I was already limiting the technical characters to 2 via the max length property, and the numbers were purely integers (decimal point excluded from the char)

The bit that I am not getting is the "and just check for 11", as that is the bit I cannot seem to get.
Robertspark
 
Posts: 1892
Joined: Sat Sep 03, 2016 4:27 pm

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

Postby Dan911 » Fri Mar 15, 2019 10:06 am

Robertspark wrote:Thanks Dan

Yes there is a bit that you did not know as I was already limiting the technical characters to 2 via the max length property, and the numbers were purely integers (decimal point excluded from the char)

The bit that I am not getting is the "and just check for 11", as that is the bit I cannot seem to get.



What I should of said is check for 10,11,12 and would of eliminated any confusion. if you don't care or want backspace to work remove !char.IsControl(e.KeyChar)

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

          if (textbox.TextLength == 1 && e.KeyChar != '0' & e.KeyChar != '1'& e.KeyChar != '2' & !char.IsControl(e.KeyChar))

            {
                e.Handled = true;

            }
        }
Dan911
 
Posts: 613
Joined: Mon Oct 31, 2016 1:22 am
Location: USA

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

Postby Robertspark » Fri Mar 15, 2019 1:47 pm

Dan,

Thanks very much, I did not realise that the e.handled would be done twice and I've not used the *.index before for validation learnt something new thanks very much.
Robertspark
 
Posts: 1892
Joined: Sat Sep 03, 2016 4:27 pm

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

Postby Robertspark » Fri Mar 15, 2019 3:58 pm

Dan,

Thanks very very much, that code worked a treat!

Always learning something and always something to learn especially with code.
Robertspark
 
Posts: 1892
Joined: Sat Sep 03, 2016 4:27 pm

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

Postby Dan911 » Fri Mar 15, 2019 6:08 pm

Happy to help Rob, yes always something new to learn, I barely scratched the surface.

Actually looking at code it's not foolproof for 12 or lower, you would need another check for first digit to be 1.
Dan911
 
Posts: 613
Joined: Mon Oct 31, 2016 1:22 am
Location: USA

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

Postby Dan911 » Fri Mar 15, 2019 10:18 pm

Hey Rob,

I'm sure you figured this part out but posting for others and for sake of completion. It does show a variety of ways to manipulate keypress for textbox.

Will only allow 0-12 in a textbox. textbox.MaxLength set to 2

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

           if(textbox.TextLength == 1 && textbox.Text != "0" & textbox.Text != "1"& !char.IsControl(e.KeyChar)) { e.Handled = true; return; }
            else if (textbox.TextLength == 1 && textbox.Text == "0") { return; }
            else if (textbox.TextLength == 1 && e.KeyChar != '0' & e.KeyChar != '1'& e.KeyChar != '2' & !char.IsControl(e.KeyChar)) {  e.Handled = true;}
        }
Dan911
 
Posts: 613
Joined: Mon Oct 31, 2016 1:22 am
Location: USA

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

Postby Dan911 » Sat Mar 16, 2019 11:03 am

Playing with this with my coffee this morning and here's a more universal way for different amounts or needs.

Code: Select all

if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
            {
                 e.Handled = true;
                 return;
            }
           
            if (!char.IsControl(e.KeyChar )&& textbox.TextLength == 1)
            {
              int i = Convert.ToInt32(textbox.Text+ e.KeyChar);
                if (i > 12) { e.Handled = true; }
            }


Last edited by Dan911 on Sat Mar 16, 2019 11:08 am, edited 1 time in total.
Dan911
 
Posts: 613
Joined: Mon Oct 31, 2016 1:22 am
Location: USA

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

Postby Robertspark » Sat Mar 16, 2019 11:07 am

That is a better way of doing it imo
And was sort of what I was trying to do but could not figure out the syntax.

Thanks again Dan
Robertspark
 
Posts: 1892
Joined: Sat Sep 03, 2016 4:27 pm

Previous

Return to Macros

Who is online

Users browsing this forum: No registered users and 0 guests