Basic ActiveX Controls VB Help

In the previous chapters, we explored the envi ronment of Visual Basic and the principles of event-driven programming, which is the core of Visual Basic’s programming model. In the precess, we briefly examined a few basic controls through the examples. Visual Basic provides many more controls, and all of them have a multitude-of properties. Most of the properties have obvious names, and you can set them-either from the Properties window or from within your code. , This chapter explores several of the basic AchveX controls in depth. These are the controls you’ll be using most often in your applications because they are the basic building blocks of the Window user interface. Rather than look at controls’ background and foreground color font, and other trivial properties, we’ll look at the properties unique to each control and how these properties are ‘used in building a user interface.

The TextBox Control

The textbox is the primary mechanism for displaying and er.rering text and is one of the most commor; elements of the Windows us-r inter/au’ TI,e TextBox control is a small text editor that provides all the basic tex; f.icilities: inserting and selecting- text, scrolling the text if it doesn’t fit in the Loni rol’ s “rea, and even exchanging text with other applications through the Clipboard, The textbox is an extremely versatile can be used for entering a single line of text, such as a number or a },aS5war J, or Eor entering simple text files. Figure 5.1 shows a few typical examples created with th.-Tcxtflox control. All the textboxes in Figure 5.1 contain text, some a smg] ( line, some several lines, The scroll bars you see in some textboxes (HL~part C1f the control. These scrollbars are attached to the control automat.cally whenever the contents exceed
the visible area of the control. With the exception of graphics applications, the t~xt130) control is the bread and butter of any Windows application. By exarrurung its prop ert h and designing a text editor based on the TextBox control, you’ll see that most of the application’s functionality is already built into the control.

Basic Properties

Let’s start with the properties that determine the appearance and, to some degree. the functionality of the TextBox control, which can be set through the Properties window. Then, let’s look at the properties that allow you to manipulate the control’s contents.


This property determines whether the TextBox control will hold a single line or multiple lines of text. By default, the control holds a single line of text. To change this behavior, set the MultiLine property to True.


This property controls the attachment of scroll bars to the TextBox control if the text exceeds the control’s dimensions. Single line textboxes can have a horizontal scroll bar so that the user can view any part of a lopg line of text. Multi-line textboxes can have a horizontal or’ vertical scroll bar or both. Scroll bars will appear in multi-line textlx xes even if tht>.,aren’t needed or the text doesn’t exceed 1} f’ dimensions of the control.
If you attach a horizontal scroll bar to the TextBox control, the text won’t wrap automatically as the user,types. To start a new line, the user must press Enter. This  arrangement is useful in implementing editors for programs in which lines must break explicitly, If the horizontal scroll bar is missing, the control inserts soft line breaks when the text reaches the end of a line, and the text is wrapped automatically.


This property determines the number of characters the TextBox control will accept. Its default value is zero, which means the text may be of any length, up to the control’s capacity limit (discussed in a moment). To restrict the number of characters the user can type, set the value of this property accordingly.

A TextBox control with its MaxLength property set to 0, itsMultil.ine propertyset to True, and its ScrollBars property set to 2 (vertical) is, on its ow n, a functional text editor. Place a textbox with these settings on a Form, run the application, and check out the following:
•.” Enter text and manipulate it with the usual editing keys, such as Delete, Insert, Home, and End.
• Select multiple characters with the mouse or with the arrows while holding down the Shift key.
• Move segments of text around with Copy (Ctrl+C) and Paste (Ctrl+V) operations.
• Exchange data with other applications through the Clipboard. You can do all this without a single line of code! Shortly you’ll see what you can do with the TextBox control if you add some code to your application, but first, let’s look at a few more properties of TextBox control.

The TextBox Maximum: 64Kb

The amount of text you can place in a TextBox control is limited to approximately 64Kb (a single line textbox can hold only 255 characters). This is adequate for moderately sized text files, but you may run into files that won’t fit In a textbox. (In this case, you must resort to a ~ichTextBoxcontrol, which is covered in Chapter 9, More Advanced ActiveX Controls.)

Manipulating the Control’s Text

Most of the properties for manipulating text in.a TextBox control are available at runtime only. Here’s a breakdown 9f each property.


The most important property of the TextBox control is the Text property, which holds the control’s text’. This property is also available at design time so that you can assign some initialtext to the control. At runtime, use this pr<?perty to extract the text entered by the user or to replace the existing text by assigning a new vaIhe to the Text property. The Text property is a string and can be used as’an argument with the usual string manipulation functions of VISual Basic. The following function returns the number of characters in the textbox:

Len(Textl. Text)

The following Instrsfunction returns the location of the first occurrence of the string “Visual” ~ the text:

InstrS(Textl.~ext. “Visual”)

To store the control’s contents in a file, use a statement sumas the following:

Similarly, you can read the contents of a text file into a TextBox control with a statement such as the following:

Textl.Text-InputS(lOF(fnum), fnum)

The fnum entry is the file number, which is presumably opened. for input with an Open statement. The functions and statements for reading from and writing to files are described in the File Input/Output tutorial on the CD.


Available at design time, the Password Char property turns the characters typed into any character’you specify If you don’t want to display the actual characters typed by the user(when entering a password, for instance), use this property to . .define the character to appear in place of each character the user types. The default value of this property is an empty string,which tells the control to display the characters as entered. If y~u set this value to an asterisk, for exampk the user sees an ,asterisk in the place of every character typed. This property doesn’ affect the control’s Text properly, which contains the actual characters, If a textbox’ PasswordChar property is set to any character, the user can’t even copy or cut the text. Any text that’s pasted on the control will appear as a sequence of asterisks or; whatever Character has been specified with the PasswordChar property.

Posted on November 4, 2015 in Basic ActiveX Controls

Share the Story

Back to Top
Share This