VB.Net text box prompt and one-time clearing

[Preface]

     The second basic computer room charging system has more understanding in many aspects of system optimization. For the prompt whether the content of the text box in the form is empty and the one-time clearing of its content, these are carried out in the first computer room charging system. In order to try, in the process of personal reconstruction of the computer room charging system this time, the demand for this aspect arose again, how to reduce the repetition rate of the code, so this aspect was explored.

[specific operation]

Operation one:

For the problem of judging whether the text box in the form is empty and clearing the content of the text box at one time, we need to create a public class in the UI layer, and write the corresponding definitions and functions in this class.

                                                                   

Operation two:

1. Declare the public content part in the newly created public class TextModel:

<span style="font-size:18px;">Imports System.Windows.Forms.Control
Imports System.Object
Public Class TextModel

    'define a structure Term
    Public Structure Term
        Dim controlSub As System.Windows.Forms.Control
        Dim strText As String
        Sub New(ByVal controlSub As System.Windows.Forms.Control, ByVal strText As String)
            With Me
                .controlSub = controlSub
                .strText = strText
            End With
        End Sub
    End Structure

    'Define a structure with a Term type array
    Public Shared arrayControl() As Term</span>
     

2. Determine whether the content in the text box is empty

(1) Write the corresponding function in the TextModel class in the public class

<span style="font-size:18px;"> 'Function to check if the textbox is empty
    Public Shared Function CIsEmpty(ByVal ArrayControl() As Term) As Boolean

        'define a term variable
        Dim termControl As Term

        'Iterate over all elements in the structure array, if the textbox is empty, prompt accordingly
        For Each termControl In ArrayControl

            If TypeOf termControl.controlSub Is System.Windows.Forms.TextBox Then
                If termControl.controlSub.Text.Trim = "" Then
                    Windows.Forms.MessageBox.Show(termControl.strText & "Can not be empty", "remind", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Exclamation)
                    termControl.controlSub.Focus()
                    Return True
                    Exit Function
                End If
            End If

        Next
    End Function</span>

      (2) In the actual form, for example, in the frmOnlineRecord form, determine whether the content in the text box is empty

<span style="font-size:18px;">Public Class frmOnlineRecord

    'Initialize the defined structure and define a process Rdim(),to complete the function
    Private Sub Rdim()
        ReDim Preserve TextModel.arrayControl(1)

        TextModel.arrayControl(0) = New TextModel.Term(txtCardID, "card number")
    End Sub

    Private Sub btnInquire_Click(sender As Object, e As EventArgs) Handles btnInquire.Click

        'transfer Rdim procedure for array initialization
        Call Rdim()
        If TextModel.CIsEmpty(TextModel.arrayControl) = True Then
            Exit Sub
        End If
   
   End Sub

End Class</span>

3. Clear the contents of the text box at one time

(1) Write the corresponding function in the public class TextModel

<span style="font-size:18px;"> 'Set to clear the text box with one key
    Public Shared Function AllEmpty(ByVal arraycontrol() As Term) As Boolean
        Dim termControl As Term

        For Each termControl In arraycontrol
            If TypeOf termControl.controlSub Is Windows.Forms.TextBox Then
                termControl.controlSub.Text = ""

            End If
        Next
        Return True
    End Function</span>

    (2) Call in the corresponding form AllEmpty function, realize one-time clearing of the content in the text box
<span style="font-size:18px;"> Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Hide()

        'transfer Rdim(process and AllEmpty function)
        Call Rdim()
        If TextModel.AllEmpty(TextModel.arrayControl) Then
            Exit Sub
        End If
    End Sub</span>

[Learning Comprehension]

Just like Teacher Mi said to us: From now on, we must start not to settle down. If there are unsatisfactory parts, we need to change and do better. This process requires us to actively explore, and what we are most afraid of is "just like this", "make a living", "almost enough", because once we have such a mentality, it is difficult for us to make new discoveries. During the reconstruction of the computer room, I thought of how to solve the problem of the text box. After searching for some information on the Internet, I couldn’t find what I wanted. When I wanted to ask other students for help, Shuo Shuo and Song Rongkai gave me Said: There are a lot of information about this on the Internet, look for it yourself. I am very grateful to them for giving me a chance to explore by myself. It is under such circumstances that unexpected gains have been produced!



Posted by drkylec on Mon, 02 Jan 2023 07:18:33 +0530