Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input Limitation of Textbox

Status
Not open for further replies.

Panavia

Technical User
Nov 14, 2010
15
DE
Hi,
I'm trying to get two textboxes to behave in a certain way to minimize user input errors.
Unfortunately I'm stuck with a problem I dont't get resolved...
I have two textboxes in which the user may enter geographic coordinates. As an example here the format of the latitude coordinate: Textbox 1 = XX° Textbox 2 = XX.XXX minutes.
With my problem peace of code below I want to achieve two thinks. First of all it shall not be possible to enter latitudes > 90°00.000" in both textboxes and secondly it shall not be possible to enter more than 59.999" in the second textbox.
Code:
        Dim MTI As Double = Double.Parse(Textbox2.Text.Replace(".", ","))
        Select Case Int(MTI)
            Case 0.001 To 59.999
                If Textbox1.Text >= 90 Then
                    Dim msg As String
                    Dim title As String
                    Dim style As MsgBoxStyle
                    Dim response As MsgBoxResult
                    msg = "Wrong Value." & vbNewLine & "Maximum allowable Latitude = 90°00.000"
                    style = MsgBoxStyle.Information
                    title = "Message"
                    response = MsgBox(msg, style, title)
                    Textbox2.Text = "00.000"
                    Textbox1.Focus()
                End If
            Case Is > 59.999
                Dim msg As String
                Dim title As String
                Dim style As MsgBoxStyle
                Dim response As MsgBoxResult
                msg = "Wrong Value." & vbNewLine & "No values > 59.999 allowed"
                style = MsgBoxStyle.Information
                title = "Message"
                response = MsgBox(msg, style, title)
                Textbox2.Text = "59.999"
                Textbox2.Focus()
        End Select
If I just program the second Case with > 59.999 everything is working fine. If I then add the case with 0.001 to 59.999 the code peace doesn't do it's job.
Any idea why that might happen and how to solve it?
Thanks!
Tobias
 
Regular expressions will give good results once you set the check. Another train of thought is to use numericupdown controls instead of textboxes. They have properties for maximum, minimum, decimal places and increment amount. You don't need to much additional checking...the control just won't allow values outside the limits to be entered.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer
 
Thanks for the tips.
I hesitated to include more, too specific features.
Up to now I rather restrict certain input devices my own.
I this context I would think that numericupdowns, at least for my application, are a litte too circuitous for the user.
In future I will definetely look deeper into the regular expressions.
The problem above I solved different by myself. The problem was that the Select Case event can handle only whole integers.
So the working solution looks like this:
Code:
        Dim MTI As Double = Double.Parse(Textbox2.Text.Replace(".", ",")[!] * 1000[/!])
        Select Case Int(MTI)
            Case[!] 1 To 59999[/!]
                If Textbox1.Text >= 90 Then
                    Dim msg As String
                    Dim title As String
                    Dim style As MsgBoxStyle
                    Dim response As MsgBoxResult
                    msg = "Wrong Value." & vbNewLine & "Maximum allowable Latitude = 90°00.000"
                    style = MsgBoxStyle.Information
                    title = "Message"
                    response = MsgBox(msg, style, title)
                    Textbox2.Text = "00.000"
                    Textbox1.Focus()
                End If
            Case Is >[!] 59999[/!]
                Dim msg As String
                Dim title As String
                Dim style As MsgBoxStyle
                Dim response As MsgBoxResult
                msg = "Wrong Value." & vbNewLine & "No values > 59.999 allowed"
                style = MsgBoxStyle.Information
                title = "Message"
                response = MsgBox(msg, style, title)
                Textbox2.Text = "59.999"
                Textbox2.Focus()
        End Select
Thanks a lot for your help!
Tobias
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top