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