Control collection vb.net a control that is empty using the Controls collection __.net

Source: Internet
Author: User

Preface: Write this blog to the Lord is to deepen their understanding of the control set and summing up the implementation of the algorithm when some of the test and Xun, if there are errors please point out, extremely grateful.

I. Demand

In the face of more complex systems, most of us need to stop repeating judgments on text boxes and combo boxes to ensure they are not empty.

second, the plan

1. Each form has a controls collection representing all the controls (such as text boxes, command buttons, and labels) that appear in the form, and the Controls collection makes it easy to implement the validation of user input (vb.net).

Extensions: The Forms collection contains all the forms in the VB.net project.

2. Example

Defines a class that is used specifically to stop a judgment. Other forms directly call the methods in the class to stop the judgment.

Stop Judgment Class public Classverifyuser public Function Verify (log AsWindows.Forms.Control.ControlCollection) as Boolean Dim conobject As Control declares an instance of the control class Dim flag as Boolea                                                          N ' declares a Boolean variable used to return information to the form calling this method flag = True                                             ' Acquiesce to values within the control ' begins to traverse each of the controls in the control class for every conobject in log                             ' Use TypeOf to view control types If TypeOf (conobject) is Textboxthen ' If the text box is empty, the message is Conobject.text = ' Then '. Tag represents the currently empty control MsgBox (ConObject.Tag.ToString + "cannot be empty, please enter full", vbOKOnly, "hint") flag = False ' This control is empty, returns information End If ElseIf TypeOf (conobje

 CT) iscombobox Then ' combo box is empty, pop-up message               If Conobject.text = "" Then MsgBox (ConObject.Tag.ToString + cannot be empty, enter full "+ vbOKOnly +" hint

    ") flag = False End If End If Next return flag End Function End Class
A daily truth
Time is like a stream, it can attract us to the sea of life, time is like skiff, it will help us sail to the other side of the ideal, time is like a brush, it will guide us to depict the picture of life.
Call this class method

 ' to determine if NULL, stop hint

          Dim myobjectas New verifyuser

           ifmyobject.verify (Controls) = False Then

          Txtuserid.focus ()

          txtuserid.selectall ()

              Exit Sub End

            If

Note:

(1) For Each loop, each control in the Controls collection is disposed of individually. The If statement stops detection for the control type. If the control is a text control, the Text property is applied to detect the texts in the text box. If the text box is empty, the prompt appears.

(2) must have the corresponding judgment, when calls this class, the logic will be clear.

(3) control is a class provided by. NET, which is the base class for all controls, in the System.Windows.Form namespace.

(4) Type ... The IS operator detects an object type. Returns True if the object is of the specified type or derives from the specified type.

third, insufficient

This method is used to eject the dialog box from the back forward. For example: username and password are empty, it will eject the password can not be empty, and then pop-up user name can not be empty. If we take this approach, we need to pay attention to the order when we place the controls.

Four, draw lessons from

Wanghaitao Shige: It involves stopping detection of partial controls

http://blog.csdn.net/xiaoduishenghuogo/article/details/7291167

The end of the article to share some of the programmer's jokes quotes: "Nokia investment mobile phone browser UCWeb, funds unknown or holding" cups, not easy to raise the girl to marry a foreign country. (Do you keep it?) In the early days of entrepreneurship in China, which borrowed money from state-owned banks. )

---------------------------------original article by
Controls and collections
---------------------------------good text to pay attention to my collection of this article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.