none
Onlick event of label to close form not working RRS feed

  • Question

  • I have an onclick event of a label (as i have the borders of the windows form hidden for aesthetics) and it works fine to close the form but if i insert a conditional IF statement to check if a field has been populated before closure then the form closes regardless ?? this is baffling me, im a bit new to VB.net though, having come from a VBA background

        Private Sub lblX_Click(sender As Object, e As EventArgs) Handles lblX.Click
            If lblJSONPath.Text = "" Then
                MsgBox("JSON Path must be populated with a value path to where the JSONs are being created", vbCritical)
    
            Else
                If lblJSONPath.Text <> "" Then Me.Close()
            End If
        End Sub

    Monday, March 25, 2019 12:57 PM

Answers

All replies

  • To best test this add two buttons and then run the code

    Public Class Form2
        Private Sub lblX_Click(sender As Object, e As EventArgs) Handles lblX.Click
            If String.IsNullOrWhiteSpace(lblJSONPath.Text) Then
                MsgBox("JSON Path must be populated with a value path to where the JSONs are being created", vbCritical)
            ElseIf Not String.IsNullOrWhiteSpace(lblJSONPath.Text) Then
                Me.Close()
            End If
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            lblJSONPath.Text = "Karen"
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            lblJSONPath.Text = String.Empty
        End Sub
    End Class

    The above is for Framework greater than 3.5 for 3.5

    Public Class Form2
        Private Sub lblX_Click(sender As Object, e As EventArgs) Handles lblX.Click
            If String.IsNullOrEmpty(lblJSONPath.Text) Then
                MsgBox("JSON Path must be populated with a value path to where the JSONs are being created", vbCritical)
            ElseIf Not String.IsNullOrEmpty(lblJSONPath.Text) Then
                Me.Close()
            End If
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            lblJSONPath.Text = "Karen"
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            lblJSONPath.Text = String.Empty
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Monday, March 25, 2019 1:42 PM
    Moderator
  • Ignore me, oh dear, embarrassing ... i was referencing the label not the text box :(
    • Marked as answer by Djdave022002 Monday, March 25, 2019 1:47 PM
    Monday, March 25, 2019 1:46 PM