Need help to troubleshoot if statement error message

Hello,

I'm trying to validate user input in a text box with an IF statement and I want to limit the number of characters to 60 or less.

Initially this seemed to be working fine, but now I get the error message no matter what I enter in the text box:

Private Function GetLine(ByRef validLine As Boolean) As Boolean

validLine = False

'check to see if the user has entered anything in the txt box

If Not (txtEnterLine.Text.Trim() = "") Then

'check to see if the txt box contains 60 characters or less

If txtEnterLine.MaxLength <= 60 Then

'if validated, assign input to class level variable

entry = txtEnterLine.Text.Trim()

'and assign true to validating variable

validLine = True

Else

'if user enters more than 60 characters show an error message

ShowErrorMessage(RECORD_ERROR)

End If

'if user does not enter any text in text box, show an error message

Else : ShowErrorMessage(RECORD_NULL_ERROR)

End If

'assign a value to class level variable

Return validLine

End Function



Answer this question

Need help to troubleshoot if statement error message

  • gpetrosh

    Your problem with you code is probably then line

    If txtEnterLine.MaxLength <= 60 Then

    this probably needs to be


    If txtEnterLine.Length <= 60 Then


    Some suggestions with some code, The following modifies you code a little to allow you to define the maximum number of characters rather than hardcoding it to 60 and allows you to call it for different textboxes - this makes it more flexible.

    Public Class Form1

    Private sValidContents As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If GetLine(Me.TextBox1, 60) Then
    sValidContents = Me.TextBox1.Text.Trim
    End If

    End Sub

    Private Function GetLine(ByRef TextBox1 As TextBox, ByVal Maxcharacters As Integer) As Boolean

    Dim validLine As Boolean = False
    Dim strContents As String = TextBox1.Text.Trim()

    'check to see if the user has entered anything in the txt box
    If Not (strContents = "") Then
    'check to see if the txt box contains 60 characters or less

    If strContents.Length <= Maxcharacters Then
    validLine = True
    Else
    'if user enters more than 60 characters show an error message
    MsgBox("More than " & Maxcharacters.ToString & " characters Entered")
    End If
    Else
    'if user does not enter any text in text box, show an error message
    MsgBox("No characters Entered")
    End If

    'assign a value to class level variable
    Return validLine
    End Function

    End Class

    However to achieve you result of limiting the textbox length entry to less than 60 characters and checking they have entered something. You can use the following. Simply setting the MaxLength property will not even allow them to enter more than 60 characters and therefore no need for the message. You can set this property at design time or in the form load. It makes the code even simpler.

    Public Class Form1

    Private sValidContents As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.TextBox1.MaxLength = 60
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'check to see if the user has entered anything in the txt box
    If Not (strContents = "") Then
    'if user does not enter any text in text box, show an error message
    MsgBox("No characters Entered")
    else
    sValidContents = Me.TextBox1.Text.Trim
    End If

    End Sub
    End Class



  • Need help to troubleshoot if statement error message