Win App - TxtBox - Not start with 0

I have a win app (specifcally a mobile/pocket pc app - but do not think the distinction matters, as it relates to teh following) - on the form, Ihave a text box - currently, I hev the below set to allow only numerics and to allow a "." within the string - what I wanted to additionaly do is to disallow an entry to begin with a "0"
tbaseflugAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrichmonCommented:
A regular expression validator using this regular expression will do it:

^[1-9]\d*(\.)?\d*$

(I also assmued that you cannot start with just a decimal, but can have just once somewhere els in the string)
tbaseflugAuthor Commented:
Well, here is what I have below (for numerics and allowing ".")

                if (e.KeyChar.Equals('.') && txtEdit.Text.IndexOf('.') != -1)
                    e.Handled = true;

Would it be possible to continue along these lines - as I am not familar with regex so much
mrichmonCommented:
Well you could do something like testing

txtEdit.Text.StartsWith("0")
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Fernando SotoRetiredCommented:
Hi mrichmon;

This should do it for you.

      if ( e.KeyChar.Equals('0') && txtEdit.Text.Length == 0 )
            e.Handled = true;

Fernando
mrichmonCommented:
Hi think you meant to direct that comment to tbaseflug and not me :o)
Fernando SotoRetiredCommented:
You are correct, Sorry.

Fernando SotoRetiredCommented:
Hi tbaseflug;

This should do it for you.

     if ( e.KeyChar.Equals('0') && txtEdit.Text.Length == 0 )
          e.Handled = true;

Fernando
tbaseflugAuthor Commented:
Fernando -

If plug your code in - it still allows me to enter with a starting "0"
Fernando SotoRetiredCommented:
Can you post the soubroutine you put the code in?
tbaseflugAuthor Commented:
       private void HandleKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (Settings.InventoryMode == "Inventory" &&
                grid.TableStyles["MobileInventory"].GridColumnStyles.Contains("QuantityOnHand") &&
                 grid.TableStyles["MobileInventory"].GridColumnStyles.Contains("OrderQuantity"))
            {
                if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && !e.KeyChar.Equals('.'))
                    e.Handled = true;
                if (e.KeyChar.Equals('.') && txtEdit.Text.IndexOf('.') != -1)
                    e.Handled = true;
            }
            else if (Settings.InventoryMode == "Inventory" ||
                grid.TableStyles["MobileInventory"].GridColumnStyles.Contains("QuantityOnHand") &&
                 !grid.TableStyles["MobileInventory"].GridColumnStyles.Contains("OrderQuantity"))
            {
                if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && !e.KeyChar.Equals('.'))
                    e.Handled = true;
                if (e.KeyChar.Equals('.') && txtEdit.Text.IndexOf('.') != -1)
                    e.Handled = true;
            }
            else
            {
                if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                    e.Handled = true;

                if (!txtEdit.Text.StartsWith("0") && txtEdit.Text.IndexOf('0') != -1)
                    e.Handled = true;
            }

            if (e.KeyChar.Equals((Char)Keys.Back))
            {
                txtEdit.Text = "0";
                txtEdit.SelectAll();
                e.Handled = true;
            }

            //ignore if more than 3 digits
            //if (this.txtEdit.Text.Length >= 3 && !char.IsControl(e.KeyChar))
            //    e.Handled = true;
        }
Fernando SotoRetiredCommented:
Hi tbaseflug;

This is the line of code I posted above but does not show up in your code.

     if ( e.KeyChar.Equals('0') && txtEdit.Text.Length == 0 )
          e.Handled = true;

Also watch out when using something like this:

      if (!txtEdit.Text.StartsWith("0") && txtEdit.Text.IndexOf('0') != -1)
            e.Handled = true;

Because txtEdit.Text has not been added with the most resent key pressed yet.

Fernando

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tbaseflugAuthor Commented:
Fernando -

Ok, got everything almost working with your code - the only problem is that in some cases the default value of the textbox may be 0 and when it is, I cannot edit -
Fernando SotoRetiredCommented:
Hi tbaseflug;

Using your code from the last post I can not see anything that will stop you from entering data into the text box. In fact looking at the code I gave you it will permit you to type more digits into the text box if the text box starts out with an initial 0 in it. If you are not going to allow a 0 in the first position of the text box you will have to decide what you want to do when a user leaves the 0 in and adds more digits to the right of it. If you post the new code I will take a look at it.

Fernando
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.