将标签链接到相应的文本框

时间:2015-02-18 12:06:20

标签: vb.net

嘿,我这里有一段代码,我想要它做的是注意是否有任何文本框中输入的数据,如果没有则会出现错误**警告 - 数据中的错误&# 34; "请参考红框" ...我使用类似这样的东西工作了

    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

`

但它变得非常长,所以我试图将其剪切成如下所示:但我想不出一种方法将Lable链接到相应的文本框是否有某种方法可以做到这一点,例如,如果我所有的名称为txtFirstname或其他内容的文本框和相应的标签是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

这是我感兴趣的主要部分:

        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

2 个答案:

答案 0 :(得分:1)

我建议使用ErrorProvider来显示错误;所以你可以简化你的代码:

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

答案 1 :(得分:0)

我想你会想要使用一个函数来获取所有的TextBox控件。

请参阅:loop over all textboxes in a form, including those inside a groupbox

见蒂姆接受的答案。我不知道你怎么会知道它的标签。