Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

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"
0
tbaseflug
Asked:
tbaseflug
  • 6
  • 4
  • 3
1 Solution
 
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
 
mrichmonCommented:
Well you could do something like testing

txtEdit.Text.StartsWith("0")
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Fernando SotoCommented:
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 SotoCommented:
You are correct, Sorry.

0
 
Fernando SotoCommented:
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 SotoCommented:
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
 
Fernando SotoCommented:
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
 
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 SotoCommented:
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now