Dave123432
Dave123432

Reputation: 233

Linking labels to corresponding Textbox

Hey I have a piece of code here and What I want it to do is notice if there is data entered in any of the text boxes and if there is not then there will be an error ** Warning - Errors in Data" "Please refer to red boxes"... I had it working by using something like this `

    Dim bErr As Boolean

    ' Initialise Error Checking
    Dim uStackframe As New Diagnostics.StackFrame
    Try


        ' Clear Previous Errors
        For Each ControlChild In Me.Controls
            If TypeOf ControlChild Is Label Then
                ControlChild.forecolor = Color.Black
            End If
        Next


        ' Check Data
        If cmbApplianceType.Text = "" Then
            bErr = True
            lblApplianceType.ForeColor = Color.Red
        Else
            cmbApplianceType.ForeColor = Color.Black
        End If

`

but it gets very long so I have tried to cut it down to something like this below: But i cant think of a way to link the Lable to the corresponding text box is there some way to do this for example if all my textboxes where names txtFirstname or something and the corresponding lable was lblFirstname?

 Private Sub tsbSaveProperty_Click(sender As Object, e As EventArgs) Handles tsbSaveProperty.Click
    '** Save Property Data

    Dim bSaved As Boolean
    Dim cSaved As Boolean
    Dim dSaved As Boolean


    ' Error Checking
    Dim uStackframe As New Diagnostics.StackFrame
    Dim bErr As Boolean
    Dim myControl As Control = Me
    Dim mylbl As Control = Me
    Try


        Do
            If TypeOf myControl Is TextBox And TypeOf mylbl Is Label And myControl.Text = String.Empty Then
                bErr = True
                mylbl.ForeColor = Color.Red
            End If
            myControl = Me.GetNextControl(myControl, True)
        Loop Until myControl Is Nothing



            bSaved = SaveProperty()
            cSaved = SavePropertyDetails()
            dSaved = SavePropertyExpiries()

            ' PropertyMaster()
        If bErr Then
            MsgBox("** Warning - Errors in Data" & vbCrLf & "Please refer to red boxes")
        Else

            If bSaved = True And cSaved = True And dSaved = True Then
                WriteAuditLogRecord(Me.Name, "SaveProperty", "OK", "Save Property" & lblPropertyIDValue.Text, 0)


                bDataChanged = False
                MsgBox("Property Master Data saved successfully", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "AztecCRM - Contact Information")
                LoadPropertyTree()
            Else
                WriteAuditLogRecord(Me.Name, "SaveProperty", "FAIL", "Save Property", 0)
                txtAddress1.Select()
                MsgBox("Property Master Update Failed", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "AztecCRM - Contact Information")
            End If
        End If

    Catch ex As Exception
        ' Catch Error
        If Err.Number <> 0 Then
            WriteAuditLogRecord(uStackframe.GetMethod.DeclaringType.FullName, uStackframe.GetMethod.Name.ToString, "Error", Err.Description & vbCrLf & vbCrLf & ex.StackTrace, 0)
            MsgBox("System Error Ref: " & sAuditID & vbCrLf & uStackframe.GetMethod.DeclaringType.FullName & " / " & uStackframe.GetMethod.Name.ToString & vbCrLf & Err.Description & vbCrLf & vbCrLf & ex.StackTrace & Chr(13) & sErrDescription, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Business Management System - Unexepected Error Ref: " & sAuditID)
        End If

    Finally
        LoadPropertyTree()

    End Try

this is the Maint Part I am interested in:

        Dim myControl As Control = Me
    Dim mylbl As Control = Me
    Try


        Do
            If TypeOf myControl Is TextBox And TypeOf mylbl Is Label And myControl.Text = String.Empty Then
                bErr = True
                mylbl.ForeColor = Color.Red
            End If
            myControl = Me.GetNextControl(myControl, True)
        Loop Until myControl Is Nothing

Upvotes: 0

Views: 227

Answers (2)

sloth
sloth

Reputation: 101142

I suggest using an ErrorProvider to display errors instead; so you could simplify your code to something like this:

Dim errorProvider = new ErrorProvider()

...

' Clear Previous Errors
For Each ControlChild In Me.Controls
    errorProvider.SetError(control, "")
Next

For Each childcontrol In Me.Controls
    If TypeOf childcontrol Is TextBox AndAlso
       String.IsNullOrWhiteSpace(childcontrol.Text) Then
          errorProvider.SetError(childcontrol, "Please enter something")
    End If
Next

Upvotes: 1

Derek
Derek

Reputation: 8783

I think you will want to use a function to get all of the TextBox controls.

See: loop over all textboxes in a form, including those inside a groupbox

See Tim's accepted answer. I'm not sure how you will know what it is labeled by.

Upvotes: 0

Related Questions