【问题标题】:Linking labels to corresponding Textbox将标签链接到相应的文本框
【发布时间】:2015-02-18 12:06:19
【问题描述】:

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

    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

`

但它会变得很长,所以我试图将其缩减为如下所示:但我想不出一种将标签链接到相应文本框的方法是否有某种方法可以做到这一点,例如,如果我所有的名称为 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

【问题讨论】:

  • 你的问题是?
  • @sloth 是否有某种方法可以将文本框链接到相应的标签,但不必硬编码每个人都说它是 txtFirstName 和 lblFirstname,它会发现这些是对应的?跨度>
  • 谢谢,现在清楚了。

标签: vb.net


【解决方案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

【讨论】:

  • 啊,是的,在我也有面板控件之后,这对我来说更重要,有没有一种方法可以在不必命名每个面板的情况下再次选择它们?
  • 也曾经纠正过错误!输入数据后有什么可以删除的吗
  • @RichardGlass 您可以轻松清除所有错误,请参阅我的更新。至于带有面板的控件:这取决于您要检查哪些控件。只是整个窗口上的所有文本框?然后很容易递归地得到它们。
  • 有没有办法只检查主文本框而没有长的单个语句?
  • 应该是For Each ControlChild In Me.Controls errorProvider.SetError(ControlChild, "") Next => ControlChild 而不是Control
【解决方案2】:

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

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

查看 Tim 接受的答案。我不知道你怎么知道它的标签。

【讨论】:

  • 是否有某种方法可以将它们与相应的名称联系起来,例如 txtFirstname 和 lblFirstname?
  • 有可能,如果您找不到相应的标签名称,我建议您抛出某种错误,这样您就可以确定您没有遇到任何拼写问题。
  • 另一种选择是使用字典进行查找。但是您必须手动将所有文本框、标签对添加到字典中。msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx
  • 是的,我正在寻找一种可以节省时间的方法,而无需对所有连接进行硬编码,以便可以以不同的形式重复使用
  • 您可能希望创建一个包含标签和文本框的用户控件。见:stackoverflow.com/questions/4730807/…
猜你喜欢
  • 1970-01-01
  • 2014-08-02
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 2017-04-10
  • 2011-10-20
  • 2017-12-14
  • 2018-09-15
相关资源
最近更新 更多