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?
 
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
0
 
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)
0
 
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
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
mrichmonCommented:
Well you could do something like testing

txtEdit.Text.StartsWith("0")
0
 
Fernando SotoRetiredCommented:
Hi mrichmon;

This should do it for you.

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

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

0
 
Fernando SotoRetiredCommented:
Hi tbaseflug;

This should do it for you.

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

Fernando
0
 
tbaseflugAuthor Commented:
Fernando -

If plug your code in - it still allows me to enter with a starting "0"
0
 
Fernando SotoRetiredCommented:
Can you post the soubroutine you put the code in?
0
 
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;
        }
0
 
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 -
0
 
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.