Community discussion forum

How to Display 4 digits in textbox control

Tags: India
  • 6 months ago

    Hi,

    I want to display the 4 digit number in textbox control which having some limitations. Actually Textbox control having the limitation to display maximum value 999 (3 digit only) and minimum 000. But it's having a facility or functionality that it can show the no. 1 from the left side of digits as a flag only. Supposed I want to display a value 12.43 then the limitation of 3 digits it cannot show all the number in textbox control. But having facility of flag 1 it will be able to display the value 12.43 in textbox. But if I want to display 23.45 in textbox then it will display only 23.4 as the first digit of the value (23.45) is other than 1. How I can do this in VB.net through programming ? Please help me….

     Thanks.

     
    Regards,

    Ashwin
     

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 6 months ago

     I think so you had not understood my problem. Please read my post again. As I'm able to display the values in text but whenever I'm handling the value as string the limitations as per application need it cannot exceed the value more than 3 digits if the first digit of value is other than 1. If the first digit of the value is 1 then the textbox control can able to display it upto 4 digit only.

     

    Thanks. 

  • 6 months ago

    Something like this? 

    Dim number As Decimal = 23.45
    Dim str As String = number.ToString

    If str.Chars(0) = "1" Then

                'if you don't want the flag removed delete the first line
                str = str.Remove(0, 1)
                If str.Length > 5 Then str = str.Remove(5)
            Else
                str = str.Remove(0, 1)
                If str.Length > 4 Then str = str.Remove(4)
            End If

  • 6 months ago

     For single case it is fine.. But how I can generate the following output, -

     Input Value             Output Value (Should be)
     

    20.01                        20.0

    19.99                        19.99

    30.25                        30.2

    15.12                        15.12

    156.55                        156.5

    279.6                        279 

    0.001                        0.00

    1.001                        1.001

     

    I've given the Input values in the Input coloumn and expected output should be in Output coloumn. It's good example to self explanatory.

     Thanks for all of you and plz. help me little bit to solve this problem.

     
    Ashwin.
          

  • 6 months ago

    Like I said just remove the

    str = str.Remove(0, 1)

    lines...

  • 6 months ago

     Not Understood.... Could u plz explain it in details As I've done your stuff in my application but no luck.

     Thanks.

     

    Ashwin.
     

  • 6 months ago

    Damn, forgot about the decimal point. 

            Dim num As Decimal = 123.456
            Dim str As String = num.ToString
            Dim dot As Byte = 0
            Dim dotindex As Byte = str.IndexOf(".")

            If str.Chars(0) = "1" Then
                If dotindex > 0 And dotindex < 4 Then dot = dot + 1
                If str.Length > 4 Then str = str.Remove(dot + 4)
            Else
                If dotindex > 0 And dotindex < 3 Then dot = dot + 1
                str = str.PadRight(5)
                str = str.Remove(dot + 3)
            End If

     

     

  • 6 months ago

    How I can take the input from input box or Textbox without assigning the value to num as decimal directly in programing...

    Could you please explain the same as I tried to num = textbox1.text but I'm getting the 0 value every time.

     Thanks for extra-ordinary help.

     
    Ashwin.
     

  • 6 months ago

    Im fairly sure this is what you want: 

            Dim sTemp As String = vbNullString
            Dim sTemp2 As String = vbNullString
            Dim iMaxLen As Integer = 4
            Dim iPos As Integer = 0
            sTemp = TextBox1.Text
            If sTemp <> vbNullString Then
                If sTemp.StartsWith("1") Then
                    iMaxLen = 4
                Else
                    iMaxLen = 3
                End If

                If Len(sTemp) > iMaxLen Then
                    If InStr(sTemp, ".") > 0 And InStr(sTemp, ".") < iMaxLen Then
                        iMaxLen = iMaxLen + 1
                        sTemp = Microsoft.VisualBasic.Left$(sTemp, iMaxLen)
                    ElseIf InStr(sTemp, ".") > iMaxLen Then
                        sTemp2 = Microsoft.VisualBasic.Left$(sTemp, InStr(sTemp, "."))
                        sTemp = Microsoft.VisualBasic.Left$(sTemp2, iMaxLen)
                        For iPos = iMaxLen To Len(sTemp2)
                            sTemp = sTemp & "0"
                        Next
                    ElseIf InStr(sTemp, ".") = iMaxLen Then
                        sTemp = Microsoft.VisualBasic.Left$(sTemp, iMaxLen + 1)
                    End If
                End If

                TextBox1.Text = sTemp
            End If

  • 6 months ago

     Hey williamsg,

     Really impressive code dude. It's resolve the issue where I stuck from last 2 weeks and you had done massive job for me. Really appreciated.

    Thanks a lot.

     
    Best Regards,

    Ashwin.

     

Post a reply

Enter your message below

Sign in or Join us (it's free).