devxlogo

Programmatically Clearing a Text Box

Programmatically Clearing a Text Box

Question:
I have a series of text boxes, any subset of which can be used for input of numerical data, followed by a command box to “compute” from that input and write results back to the boxes. If a second, new set of data is to be entered, I would like to automatically clear the boxes when I click a “new” control button. Using the syntax Text1=”” results in a string being read the next time “compute” is clicked and a data type error when a data check is made for non-zero data having been entered. Using the syntax Text1=0 puts a zero in the box, but when I begin typing the new number in, it is as if the keyboard mode is in Ins, rather than Typeover (neither keyboard mode affects the result), and an integer entered is read in with the extra trailing zero making it 10X too large.How can I clear the text boxes back to their uninitialized state that they had when the program was launched, such that they are blank and read as numerical zeros unless non-zero data is entered?

Answer:
Try the .Clear method on the text box. It is designed not to generate events. Alternatively, if the Clear does generate a Change event, simply set a flag before you clear the box. In the Change event for the text box, if the flag is set, ignore the change that was made. I’ve used this method several times successfully.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist