Chapter2

What's New with Visual Basic 6

  • Getting a Quick Overview of VB6
  • Getting More Power from Enhanced Controls
    • The Validate Event and the CausesValidation Property
    • Adding Excitement with the New Graphical Enhancements
  • Working with the New Controls
    • Selecting Dates with the MonthView and DateTimePicker Controls
    • Making Movable Toolbars with the CoolBar
    • Using Graphics with an ImageCombo
    • The FlatScrollBar Control
  • Working with the New Language Features
    • File System Objects
    • New String Functions
    • True Dynamic Control Creation
  • Learning About VB and the Internet
  • Finding Out About the New Data Capabilities
  • New Object Programming Additions

Getting a Quick Overview of VB6

If you're new to Visual Basic, the title of this chapter might be a little confusing. Clearly if you are a novice to the language, everything about VB is new. Even so, you shouldn't avoid this chapter. There's something in it for you, particularly in the sections focusing on the new ActiveX controls. For those of you who have done some work in other versions of Visual Basic, you'll find this chapter very relevant.

Getting More Power from Enhanced Controls

Before we look at the completely new additions to VB6, let's look at some enhancements to the features from the previous version.


The Validate Event and the CausesValidation Property

The first enhancement that affects just about all intrinsic ActiveX controls is the addition of the Validate event and the CausesValidation property. Before VB6, if you had to check to see if a word was typed properly in a TextBox, you most likely would program the TextBox's LostFocus event handler to see if, indeed, the user entered correct data. If they hadn't, you could invoke the control's SetFocus method in which validation was occurring to keep the user from proceeding. Sometimes the logic of this programming could cause your users problems. If they never entered the right data, they would be locked into that one control--they couldn't even click a Help button. The new Validate event procedure and the CausesValidation property address this issue.

Listing 2.1 shows how to use the Validate event procedure to check a TextBox's data. The code relates to the illustration in Figure 2.1. If users don't type the word Cherries in the first TextBox, they can't proceed to the second TextBox. However, because the value CausesValidation property of the CommandButton is set to False, users can click it to help determine the right word to enter.

LISTING 2.1 02LIST01.TXT--Allowing Some Controls Event Handling Activity
During Data Validation

01 Private Sub Text1_Validate(Cancel As Boolean)
02 `Make it so that if the user does not
03 `enter the word, "Cherries" in the TextBox
04 `the cursor will be returned this TextBox
05 If Text1.Text <> "Cherries" Then
06 MsgBox "You cannot go on!"
07 Cancel = True
08 End If
09 End Sub
10 Private Sub Command1_Click()
11 Dim strMsg As String
12 Dim strQuote As String
13 strQuote = """"
14 `Make an instructional message
15 strMsg = "You must type the word," & strQuote
16 strMsg = strMsg & "Cherries" & strQuote & " "
17 strMsg = strMsg & "in the first TextBox."
18 MsgBox strMsg, vbInformation, "Instructions"
19 `The reason that you can click on this
20 `CommandButton even though the Cancel parameter
21 `of the Validate event handler is set to True
22 `is because the value of CauseValidation
23 `property of this CommandButton is set to false.
24 End Sub

Commenting code
The apostrophe (`) before a line of code denotes a commented line. That line of code isn't run by Visual Basic.

FIGURE 2.1 The Validate event procedure allows you to check for correct data while still allowing you to access other controls during the validation process.

Adding Excitement with the New Graphical Enhancements

Visual Basic, as always, allows you to use graphics to make your programs come alive, and Microsoft has enhanced the graphical capabilities of many controls. First, the ImageList control now supports .gif files. This enhancement is substantial because just about every control that uses graphics has an ImageList control associated with it.

The ListView and TabStrip controls have been enhanced to allow you to use pictures and icons for decoration and description. Figure 2.2 shows you the new ListView control, to which you can add a background picture in the client area. The background picture can be centered, tiled, or placed at any corner. You can add check boxes to ListItems child objects within the control by setting the ListView's CheckBoxes property to True. Also, you can make the scrollbars of the ListView appear in the new flat style by setting the FlatScrollBars property to True. (If you want to see some of the ListView control enhancements demonstrated within a programming context, download the file VB6Ch2.zip and look at the project prjListView.vbp.)

Getting files From the Web site
The files referenced in this book can be downloaded from the Web site dedicated to this book. The source code can be found at http://www.mcp. com/info. When you get the URL, enter 078971633x, and then click the Search button to go to the Book Info page.


FIGURE 2.2 You now can have a background graphic in the ListView control client area.

Figure 2.3 shows you the new TabStrip control, which now allows you to place graphics in each tab. The control also has a new property, HotTracking, that you can assign at design time. If you set the value of HotTracking to True, the tab highlights when you pass the mouse pointer over it. As mentioned earlier, as with all the new controls, the TabStrip control has a Validate event procedure. (The example code for this control is in project prjTabStrip.vbp, in the VB98Ch2.zip file on the book's Web site.)

FIGURE 2.3 The TabStrip control has been enhanced to allow for icons on each tab.
The Slider control has a new property, Text. When you set a string to the value of the Text property, that string appears within a ToolTip window. The position of the ToolTip window is determined by the Slider control's new TextPosition property (see Figure 2.4). To see this code in action, review the project prjSlider.vbp from the book's Web site.

FIGURE 2.4 The Slider control now has a Text property that shows you a ToolTip window.


Many enhancements have been made to the ToolBar control. You can now add drop-down menus to the toolbar by using a ButtonMenu. The Style property supports a transparent setting (tbrTransparent) that allows you to have the buttons on your toolbar appear and behave like the buttons on the Internet Explorer toolbar.

Figure 2.5 shows the effect of the new transparent setting and a collection of buttons with each button having its own ButtonMenu. If you want to find out how to set up your toolbar as shown in the figure, download the project prjToolBar.vbp from the book's Web site. Open the project, select the form, and then right-click the ToolBar control to view the property page for the control. You then can review the various settings for the control.

FIGURE 2.5 The ToolBar control is enhanced to provide ButtonsMenus and hotspots. The control's property page allows you to configure a ToolBar at a very detailed level.




Working with the New Controls


Visual Basic 6 ships with many new ActiveX controls. Some of these controls apply to stand-alone desktop programs; others pertain to enterprise-wide data access development projects. In the following sections, you'll see the new controls that relate to your desktop development efforts.

Selecting Dates with the MonthView and DateTimePicker Controls

VB6 has two controls that provide you with a new way to view and select dates: MonthView and DateTimePicker. What's interesting about these controls is that they let you view and select dates within the context of a calendar. The MonthView control presents a full calendar that you can traverse from day to day or month to month. The DateTimePicker control is similar to the MonthView except that its calendar drops down when the user clicks the control's down arrow. Figure 2.6 shows you the Using all the new controls

All the controls discussed in this section are displayed and used within a single project, WhatsNew.vbp. You can download this code from http:// www.mcp.com/info, as explained earlier.MonthView and DateTimePicker controls.

FIGURE 2.6 All the new Windows common controls are embedded in the OCXs MSCOMCTRL.OCX, MSCOMCT2.OCX, and COMCT332.OCX.

Making Movable Toolbars with the CoolBar


The new CoolBar control is similar to the Coolbars you've been using in Office 97 and Internet Explorer. A CoolBar is a toolbar that you can move on a form, and the CoolBar control is a container for other ActiveX controls. Thus, you can offer your users more flexibility and utility by embedding other controls within the CoolBar. Figure 2.6 shows a CoolBar with three Band objects. The first Band contains two OptionButtons, the second contains a ToolBar control with three buttons, and the third band contains a ComboBox control. At runtime you can move the Bands around the Coolbar's client area any way you want.

CoolBar bands


A band is a bar within a CoolBar. A CoolBar can have many bands that can be moved about in the CoolBar client area.

Using Graphics with an ImageCombo


Working with ImageLists


An ImageList is a "windowless" control that you use to store different types of graphics: bitmaps, gifs, icons, and so on. You associate an ImageList with a control by assigning the ImageList in question to a control's ImageList property. For example, to assign an ImageList named ImageList1 to an ImageCombo, the code is ImageList1.ImageList = ImageList1.

The ImageCombo control, new with Visual Basic 6, allows you to embed graphics within a ComboBox. To accomplish this, Visual Basic introduces a new object, the ComboItem, which encapsulates all layout and data properties that are needed to work with an ImageCombo. A ComboItem has a Text property for string display of a line within the ImageCombo and an Image property that can define the graphic to show on a line within the ImageCombo. As with most of the new graphical ActiveX controls, the ImageCombo has an ImageList from which it references images and pictures.

Use the code in Listing 2.2 to add a ComboItem that has a graphic to an ImageCombo. This code is from the cmdAddCbo_Click() event procedure of the CommandButton cmdAddCbo, which is part of the code from the project WhatsNew.vbp. You can download the code from the Web side dedicated to this book.

LISTING 2.2 02LIST02.TXT--Creating ComboItems and Adding Them to an
ImageCombo

01 Static i% `A counting integer
02 Static imgi% `A index for a graphic
03 `within an ImageList
04 Dim ci As ComboItem `Object variable for a
05 `ComboItem
06
07 `Get a reference to ComboItem object using
08 `a ComboItems Add method.
09 Set ci = ImageCombo1.ComboItems.Add
10 `Assign some data a line of text
11 `ci.Text = "My Caption " & CStr(i%)
12 `Add a graphic by referencing an index in the
13 `associated ImageList
14 ci.Image = imgi% + 1
15 `Make sure that you haven't exceeded the number
16 `of images in the ImageList
17 If imgi% = ImageList1.ListImages.Count - 1 Then
18 imgi% = 0
19 Else
20 imgi% = imgi% + 1
21 End If
22 `Increment the string counter
23 i% = i% + 1

Code for the ComboItem


If you're beginning programmer, the code shown in Listing 2.2 is probably a little beyond your needs right now. This code is provided to show those with some VB background how to work with the newer features of the ComboItem object.

Figure 2.6 shows you the result of clicking the cmdAddCbo button. Notice that the ImageCombo contains a graphic and a string.

The FlatScrollBar Control


Visual Basic 6 provides a new variation of the familiar scrollbar control: the FlatScrollBar. The FlatScrollBar is fundamentally the same as the ScrollBar, except that the FlatScrollBar has three Appearance styles: a standard style, a three-dimensional (beveled) style, and a two-dimensional style that becomes beveled when the mouse pointer hovers over it (refer to Figure 2.6).

Working with the New Language Features


In addition to enhanced and new ActiveX controls, VB6 also provides new features within the Visual Basic language itself. Again, if you have no previous experience with Visual Basic, the following sections might seem a bit baffling. You still might want to read through it anyway, however; these new features will make your programming efforts easier regardless of degree of prior experience.

File System Objects

In older versions of Visual Basic, if you wanted to get file system information or perform file system tasks, such as writing to a file or changing a directory, you had a number of techniques available, most of which were quite laborious. In Visual Basic 6, all these chores have been contained with a new object, FileSystemObject, which is part of the Visual Basic Scripting Library. Thus, when you learn to use this object within VB6, you can easily transfer your knowledge to scripting within Internet Explorer or Internet Information Server to do system-level programming.

Internet Information Server


Internet Information Server (IIS) is a complex program, the function of which is to deliver information and files over the Internet. An Internet server is a physical computer, and IIS is software that runs on an Internet server. As you read in this book, you'll learn that you can use Visual Basic to write programs that are extensions of IIS, called IIS Applications. An application that runs on an Internet server is called a server-side application.

The FileSystemObject is quite complex. It's made of a number of objects and methods that encapsulate the file system functions, which you use to work with drives and files on a local machine or over the network. Table 2.1 describes the new FileSystemObject objects.
Microsoft Scripting RunTime component


If you plan to program with the FileSystemObject, make sure that you include a reference in your project to the Microsoft Scripting RunTime ActiveX component. You include a reference to the MS Scripting RunTime through the References dialog (choose References from the Project menu). Then select the Microsoft Scripting RunTime in the References list.

TABLE 2.1 FileSystemObject Objects










No hay comentarios:

Publicar un comentario