Anjello Joshua Anjello Joshua - 17 days ago 5
Vb.net Question

Show Error using Tag Property C#

I want to know on how to error using

Tag
Property? I have code in VB.Net but I do not know to convert it to C#.

Scenario: The
textbox_qty
only accepts integers. If the user types a non-numeric character, it shows
Tag
property saying
Invalid Characters
.

This is the code I used in VB.Net. I use this if the for has null fields.

Private Function ValidField(ByVal ParamArray ctl() As Object) As Boolean
For i As Integer = 0 To UBound(ctl)
If ctl(i).Text = "" Then
Error_reg.SetError(ctl(i), ctl(i).tag)
Return False
Exit Function
End If
Next
Return True
End Function

If ValidField(TextBox_userName, TextBox_password, TextBox_retypePassword, TextBox_lastName, TextBox_firstName, ComboxBox_group, ComboBox_question, TextBox_answer) = False Then
Exit Sub


If this code does not work? Is there another code that can show error in
Tag
Property without converting my VB.Net code to C#?

Thank you for helping me!

Answer

It may be worthwhile to check into the Information Class, it has methods for validating objects. While it is a VisualBasic class, it can still be used in C# which is one of the main benefits of the .Net Framework.

In Visual Basic you do not need to add any references to your project.

If you want to use the Information Class in C# make sure to add a reference to Microsoft.VisualBasic in your project, then add using Microsoft.VisualBasic; to the class or module that you are adding the following code to.

You can use the Information.IsNumeric method to validate a numeric entry.

Example VB.Net code...

Private Function ValidField(ByVal ParamArray ctl() As Object) As Boolean
    For i As Integer = 0 To UBound(ctl)
        Dim tB As TextBox = DirectCast(ctl(i),TextBox)
        If Not IsNumeric(tB.Text) Then
            Error_reg.SetError(tB, tB.Tag)
            Return False
        End If
    Next
    Return True
End Function

Example C# code...

private bool ValidField(params object[] ctl)
{
    for (int i = 0; i <= Information.UBound(ctl); i++) {
        TextBox tB = (TextBox)ctl[i];
        if (!Information.IsNumeric(tB.Text)) {
            Error_reg.SetError(tB, tB.Tag);
            return false;
        }
    }
    return true;
}

Also the Exit Function after the Return statement is not needed, Return automatically exits the function with the result.

Comments