Click here to Skip to main content
15,915,164 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to set MaxLength of TextBox when TextChanged in Calculator Program.

İf there is Comma in TextBox I want to set MaxLength 19 if not there is comma in textboxI want to set MaxLength 24

I want this because there are 1000 Separator in TextBox. When user enter digits inside TextBox When put comma then Length must set 24 if not set max 19 digits.


What I have tried:

VB
Private Sub txtInput_KeyDown(sender As Object, e As KeyEventArgs) Handles txtInput.KeyDown
       If Not InStr(1, txtInput.Text, ",") Then
           txtInput.MaxLength = 19
       Else
           txtInput.MaxLength = 24

       End If

   End Sub
Posted
Updated 1-Jan-19 1:43am

1 solution

That's not a good solution, or even close.
What if I type
1,2345678901234567890123
That's got a comma, so it's allowed 24 digits, but it's way outside your limit.
Or what if I type
Hello, this is a test...
That's also 24 characters, with a comma.

Instead of using a TextBox, use a NumericUpDown, and set the max and min values they can enter. It acts like a textbox, but it won't let the user enter invalid values.

And you do realise how large a number 19 digits is? It fits in an unsigned Int64, but it won't fit in a double or even a decimal. Are you sure that is a valid upper limit for your numbers?
 
Share this answer
 
Comments
Nicomendox 1-Jan-19 9:52am    
First of all then for reply.
I make a mistake... I mean to say... in a calculator screen I try to set max digit length. For example:(in my country we use comma for decimal numbers and use dot for tousand seperater.)

1,234,567,890,123,456 (total length is 21) (Digit Count= 16)
12,345,678,901,234.56 (total length is 21) (Digit Count= 16)
123,456,789,012,345.6 (total length is 21) (Digit Count= 16)
123,456,789.0123456 (total length is 19) (Digit Count= 16)
1,234,567,890.123456 (total length is 20) (Digit Count= 16)
1.234567890123456 (total length is 17) (Digit Count= 16)

as u can see. I want to set max digit limit is 16. when digit number count is 16 then textboxt dont allow more digit number.
OriginalGriff 1-Jan-19 9:56am    
Still don't do it: use a NumericUpDown control and let the system do the work for you. It's Culture aware, so it accepts whatever the user has set as his default, without you having to do anything to adapt.
Nicomendox 1-Jan-19 12:46pm    
Thank you at last I solved with your advice. thank you very much.
OriginalGriff 1-Jan-19 13:36pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900