0

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
Richard
  • 179
  • 2
  • 10

2 Answers2

1

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
sloth
  • 99,095
  • 21
  • 171
  • 219
  • AH yes this is something to more to what i am after I also have controls with panels can there be a way to pick all of them up again without having to name each panel? – Richard Feb 18 '15 at 14:20
  • Also once corrected the error ! stays is there something to remove this once data has been entered – Richard Feb 18 '15 at 14:25
  • @RichardGlass You can easily clear all errors, see my update. As for your controls with panels: It depends on which controls you want to check. Simply all TextBoxes on the entire window? Then it's easy to get them recursively. – sloth Feb 18 '15 at 15:17
  • Is there a way to only check say the main textboxs with out long individual statments? – Richard Feb 18 '15 at 16:24
  • @RichardGlass If you can define "main textboxes", sure. – sloth Feb 19 '15 at 08:11
  • just say for examples sake txtFirstname txtSurename txtAddressline1 – Richard Feb 19 '15 at 10:07
  • @RichardGlass For only three controls, I suggest simply using something like `For Each control In {txtFirstname, txtSurename, txtAddressline1}` – sloth Feb 19 '15 at 10:12
  • yeah this way is alot handier than the previous way i was coding this thanks. This part of the code doesnt seem to work though "control is a type and cannot be used as expression" `For Each ControlChild In Me.Controls errorProvider.SetError(Control, "") Next ` @sloth – Richard Feb 19 '15 at 10:45
  • 1
    Should be `For Each ControlChild In Me.Controls errorProvider.SetError(ControlChild, "") Next` => `ControlChild` instead of `Control` – sloth Feb 19 '15 at 10:51
0

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.

Community
  • 1
  • 1
Derek
  • 7,615
  • 5
  • 33
  • 58
  • Would there be some way to link them with corresponding names for example txtFirstname with lblFirstname? – Richard Feb 18 '15 at 13:23
  • It is possible, I'd recommend throwing some sort of error if you can't find the corresponding label name, so you can be sure you haven't made any spelling problems. – Derek Feb 18 '15 at 13:50
  • Another option would be to use a Dictionary to do the lookup. But then you'd have to manually add all of the Textbox, Label pairs to the Dictionary.https://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx – Derek Feb 18 '15 at 13:52
  • yeah see I am kind of looking for a way that will save me time without having to hard code all of the connections so it can be reused in different forms – Richard Feb 18 '15 at 13:54
  • You might want to look at creating a user control that contains a label and a textbox. See: http://stackoverflow.com/questions/4730807/winforms-is-there-a-concept-of-associating-a-label-with-a-textbox – Derek Feb 18 '15 at 13:56
  • A nice thing about that is that you may not always want to error on empty. Maybe you will only want to do that on certain properties. That might end up being a property on your use control "AllowEmpty". – Derek Feb 18 '15 at 13:58