Automatically determines whether textbox and cmobobox are empty (traversal Control)

Source: Internet
Author: User

Is it crazy when we have to judge whether each widget is empty on every form? After judging one is the next and the other, let's look at the long string of IF... else! In fact, in the field of object-oriented, we should think of one of the three major features of object-oriented as long as we repeat the same or similar code three or more times: encapsulation.

The commonly used input data controls are textbox and cmobobox. Here I will take them as an example (the tabindex and tag attributes of the control need to be used, the text property does not have the running effect if it replaces the tag property ):

This example is not achieved overnight. It has undergone three major changes:

 

Function Code

First time: the code is relatively long, And the traversal is performed from the control's tabindex attribute to the smallest, the running result is not satisfactory ~

Public shared function isemptytext (byval objs as windows. forms. control. controlcollection) as Boolean 'declares an instance of the control class dim objcon as Control' to start traversing every control in the control class for each objcon in obj' typeof to determine the type if typeof (objcon) is textbox then if objcon. TEXT = "" Then msgbox (objcon. tag. tostring + "cannot be blank", vbokonly + vbexclamation, "warning") objcon. focus () return false exit function end if 'Use typeof to view the control type elseif typeof (objcon) is ComboBox then if objcon. TEXT = "" Then msgbox (objcon. tag. tostring + "cannot be blank", vbokonly + vbexclamation, "warning") objcon. focus () return false exit function end if next return true end Function

Second: integrated the first separate judgment

Public shared function isemptytext (byval objs as form) as Boolean for each objcon as control in objs. controls if (typeof objcon is textbox) or (typeof objcon is ComboBox) then if objcon. text. trim = "" Then msgbox (objcon. tag + "cannot be blank! ", Vbokonly + vbinformation," system prompt ") objcon. Focus () return false exit for else return true end if next end Function

Third: the second optimization solves the traversal problem.

Public shared function isemptytext (byval objs as form) as Boolean for I as integer = objs. controls. count-1 to 0 step-1 If (typeof objs. controls (I) is textbox) or (typeof objs. controls (I) is ComboBox) then if objs. controls (I ). text. trim = "" Then msgbox (objs. controls (I ). tag + "cannot be blank! ", Vbokonly + vbinformation," system prompt ") objs. Controls (I). Focus () return false exit for end if next end Function
Call Method
'Call the isemptytext () function to verify whether the form control is empty. Dim strisempty = isempty. isemptytext (me) If strisempty = true then exit sub end if
Running Effect

Maybe the code is not perfect, even if there is a ready-made one (heavyweight query control-sp1234 ). However, in the course of learning, you must first have this kind of consciousness and try to do it, whether it's learned from others or researched by yourself, then, I will gradually learn more about the previous knowledge in the future.

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.