WHAT'S NEW?
Loading...

How to create my own class in VB.Net


This simple line of codes and Screenshot determines the invalid type required in textbox. I created my own class to control the invalid typing of data required in textbox. Example, the textbox required only a data that accepts all string input and if the user’s prompt to input the numeric value, invalid message display that the field required only text input.
Here are the example Screenshot:
Message box display field is required.


Message box display numeric input is not allowed.
----------------------------------------------------------------------------------
Here are the codes:
This is the class Declaration
Public Class validator
    Private Shared title As String = "Invalid Entry"
    Public Shared Property mytitle()
        Get
            Return title
        End Get
        Set(ByVal value)
            title = value
        End Set
    End Property

    Public Shared Function ispresent(ByVal textbox As TextBox) As Boolean
        If textbox.Text = "" Then
            MessageBox.Show(textbox.Tag & " Field is required.", mytitle)
            textbox.Select()
            Return False
        Else
            Return True
        End If
    End Function
    Public Shared Function notrequirenumeric(ByVal textbox As TextBox) As Boolean
        If Microsoft.VisualBasic.IsNumeric(textbox.Text) Then
            MessageBox.Show(textbox.Tag & " Numeric is not allowed", mytitle)
            Return False
        Else
            Return True
        End If
    End Function
End Class
This is  the windows form application
Public Class FrmEnterData

    Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
                    End Sub

          Call isvalid() 'call my method name isvalid

    Private Function isvalid() As Boolean ' this is my method name isvalid that return value.
        Return validator.ispresent(txtinputdata) AndAlso validator.notrequirenumeric(txtinputdata)
    End Function
End Class
Steps on how to create my own class

1.  Open your Visual Basic.Net and add Windows form application
2.  Put 1 textbox and 1 button in your windows form
3.  To create a class, click the project menu and add windows form
4.  Noticed: the default name is the name of the form.
5.  Now,in your templates, locate the name Class
6.  The default name of your class is Class1.vb, you can change it.
7.  Finally, class is created and that’s the time you can create your own class.

Actually the code above is not appropriate for formative invalid input, rather it shows how to create class. I created code to detect if the users prompt to input invalid data. So, try this code.
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
         'for invalid numeric input
        Dim testChars() As Char = "0123456789".ToCharArray        If testChars.Contains(e.KeyChar) = False Or e.KeyChar = Chr(Keys.Back) Then
            e.Handled = False
            ErrorProvider1.Clear()        Else
            e.Handled = True
            ErrorProvider1.SetError(TextBox1, "Numeric is not allowed")        End If
         'for invalid letters input
        Dim test() As Char = "0123456789".ToCharArray        If Not test.Contains(e.KeyChar) = False Or e.KeyChar = Chr(Keys.Back) Then
            e.Handled = False
        Else
            e.Handled = True
            MessageBox.Show("Letter is not allowed")        End If
    End Sub

0 comments:

Post a Comment