What's New
Q & A
Tip Jar
Tips & Tricks
These are brief tips and tricks that can make Visual Basic programming easier.
New Control Usage Numerical Stuff
Starting & Stopping I/O Bug Alerts
Software Engineering Printing System Issues
Graphics Coding Techniques Other Programs
Databases Misc Forms

Control Usage

Make a Property Page

  1. Create a new ActiveX control project. Give the control properties.
  2. Invoke the Project menu's Add Property Page command. It is set to a standard size so don't resize it.
  3. On the property page:
    1. Add controls to let the user view and modify the properties.
    2. Create Boolean variables to indicate when the user changes each value.
    3. In the PropertyPage_SelectionChanged event handler, set the values of the controls on the property page using the property values from the selected controls in the application. Set the property Booleans to False and set Changed to False.
    4. When the user changes a property value, set its Boolean to True and set Changed to True.
    5. In the PropertyPage_ApplyChanges event handler, see which properties have been changed and set those values for the selected controls.
  4. Connect the property page:
    1. Open the control's form window.
    2. In the Properties window, click on the Property Pages entry. Then double click the ellipsis (...) to the right.
    3. Check the box next to your property page and click Ok.
Now the developer can click on the ellipsis next to the Custom property for your control to see the property pages.

Click here to download an example program.

Use the Forms ComboBox
(By Ganesh R)

The traditional combo box on typing a character doesn't lists the matching values in the combo box. For this purpose we can use Microsoft Forms 2.0 Library under references. This combo box is a very good one where in on typing a single character the first matching value is selected and on selecting the second the next closer matching value is displayed and so on.

Aykut Cantürk points out:
Microsoft does not recommend the usage of this library. It differs from Office version to version, language to language. As a result, the installation process can damage office applications. This is an experimental knowledge and not accepted by Microsoft.

Giving a Control an About Dialog
When you place an ActiveX control on a form, you can click on the ellipsis to the right of the About entry in the control's Properies window to see information about the control. Here's how to add this to your ActiveX controls.
  1. Create the dialogs form.
  2. Create a ShowAbout subroutine in the control that displays the dialog.
  3. Select the control's project in the Project Group window and open the Tools menu's Procedure Attributes command.
    1. In the Name combo box, select ShowAbout.
    2. Click the Advanced button.
    3. In the Procedure ID box, select "About Box."
    4. Click the Ok button.
Now you are ready to go. When you click the ellipsis next to About in the control's Properties window, the development environment calls your ShowAbout procedure.

My book Custom Controls Library shows how to do a lot of other ActiveX control stuff and includes the complete source code for 101 ready-to-run ActiveX controls.

Reduce Media Player Control Flicker
(By Luis Ferro)

To use the media player (in Windows NT) one have to be sure that the avi has at least 5 seconds of frames or some flicker and black frames will appear on screen. The media player has also some strange behaviors about what should be shown or not. One must make sure that if the controls are not visible, then the control itself must be visible. To make the control "disapear" just change the top to out of the screen image. Otherwise some phantom controls and sliders will surface somewhere on the screen.

Upgrading a Control
(By Jake Lake)

After editting a control, re-OCXing it, loading the project and an error occurs do this:

  1. Load the whole project
  2. Say "yes" and "ok" to the messages that refer to continuing to load process
  3. Second-Click on the side tool-bar and re-load you upgraded control.
  4. Click Open from the file menu
  5. When it asks you to save your project, ONLY save the .vbp files, ONLY!
  6. When it asks you to load, re-load you project and your control should be upgraded!!!

Disabling Picture Previews
(By Daniel Fabian)

Daniel wanted to load Web pages with the Web Browser control without loading pictures. With a little digging, he found that whether the pictures are loaded is controlled by the registry setting (all on one line):

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\
Unfortunately this is the same key used by Internet Explorer so if the control doesn't load pictures, neither does IE.
Upgrading Controls
(By saso.tomazic@fe.uni-lj.si)

When you change versions of a control, particularly ActiveX controls you are building, old projects may be unable to corrcetly load the control. When you load the project, Visual Basic replaces the control with a PictureBox having the control's name.

You can delete the PictureBoxes and replace them with new controls. If you have a lot of controls, that is a hassle.

Another solution is to open the project in a text editor and correct the control's version number. If the program does not use any features no longer supported by the control, it should work.

Faster FlexGrids
(By Bill Meister)

I found that populating the MSHFlexgrid control can be incredibly slow compared to populating the older MSFlexgrid control. If you do not need to use the hierarchical properties of the MSHFlexgrid then definitely use the MSFlexgrid..

In a recent application that I wrote to fill a large grid from a complex SQL call, I initially used the new MSHFlexgrid control and populated it for the user to view. The populating and updating of the grid took over 45 seconds. Just switching to the older MSFlexgrid control knocked down this to less than 15 seconds. This improvement made the wait just bearable!

[I have also noticed that you can speed the control up if you set Visible = False when you begin the update and then Visible = True when you are finished. The control doesn't actually go away in between, it just doesn't update every time you change a cell's value. -- Rod]

Disable All Controls On A Form
(By Theodore Fattaleh)
    ' Be careful using this option because if you
    ' have a code that says "Call Commad1_click"
    ' then it will not fire.
    Dim i As Integer

        For i = 0 To Form1.Controls.Count - 1
            Form1.Controls(i).Enabled = False
        Next i

Display a Picture in a Status Bar
Right click on the Status Bar control and select the Properties command. Click the Panels tab. Add a new panel. In the Picture area, click the Browse area and select the picture you want displayed in that panel.
Load a Blank Page in WebBrower Control
When you run a program that contains a WebBrower Control, by default it comes up invisibly. That can make the program look rather strange.

To make the WebBrowser appear and look empty but normal, execute this code when the form loads:

    Private Sub Form_Load()
        wbrMainBrowser.Navigate "about:blank"
    End Sub

Creating an ActiveX Control
Creating an ActiveX control that does something useful is a bit too complicated to describe here, but the basics are simple. Start a new project. Select the ActiveX Control option from the new project dialog.

Give the control a meaningful name. If you call the control MyControl, then when the developer creates instances of the control, they will be named MyControl1, MyControl2, etc.

Give the control public properties and methods to let the developer control it. The ActiveX Control Interface Wizard can help with some of this.

Set the control's ToolboxBitmap property to the bitmap you want to display in the developer's toolbox. This bitmap should be 16 pixels wide and 15 tall. If it is not, Visual Basic will stretch it to fit. That usually produces an ugly result.

When you have given the control the properties and methods it needs, select the Project menu's Properties command at the bottom. Enter a meaningful project name and description on the General tab. On the Make tab, you can also enter the company name, copyright information, etc.

Now you are ready to build the OCX file. Select the File menu's Make Project_Name.ocx command. That's all there is to it.

To use the control, create a new Standard EXE project. Select the Project menu's Components command. In the dialog, find the new control. It will be listed using the description for the project that you entered in the project properties General tab.

When you click Ok, you should see the control's bitmap in the toolbar. You can now use the control just like any other.

There is a LOT more to say about ActiveX controls. For more information, look in my book Custom Controls Library. In addition to giving more information on control creation in general, this book shows how to build 101 useful custom controls. Complete source code is included on the CD-ROM. For more information including readers' comments and a table of contents, click here.

See Which Items Are Selected
When multiple selection is allowed in a ListBox, use a loop to see if the Selected property is true for each item.
    Dim i As Integer

        For i = 0 To List1.ListCount - 1
            If List1.Selected(i) Then
                MsgBox List1.List(i) & " is selected."
            End If
        Next I

Autoselect Text
To make a TextBox automatically select its text when focus enters it, give it a GotFocus event handler like this:
    Private Sub Text1_GotFocus()
        SendKeys "{home}+{end}"
    End Sub
Or use SelStart and SelLength:
    Private Sub Text1_GotFocus()
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1.Text)
    End Sub

Understand filters
The CommonDialog control's Filter property lists text strings alternating with filters separated by pipe | symbols. For example:
    CommonDialog1.Filter = _
        "Bitmap Files (*.bmp)|*.bmp|All Files (*.*)|*.*"
Here there are two filters. The first displays the string "Bitmap Files (*.bmp)" to the user. When the user selects the filter, the dialog applies the filter "*.bmp" The second filter displays "All Files (*.*)" and applies "*.*"
Scroll TextBoxes
You can make a TextBox scroll to the bottom with:
    Text1.SelStart = Len(Text1.Text)

Learn Control Array Bounds
You can use a control array's UBound and LBound properties to determine how many controls are loaded. For example, the following code fills a string with the captions in the Label1 control array.
    For i = Label1.LBound To Label1.UBound
        txt = txt & Label1(i).Caption & ", "
    Next i

Make Return Act Like Tab
Set the form's KeyPreview property to true. In the form's KeyPress event handler, check for the carriage return. If you get one, use SendKeys to generate a TAB instead. Set KeyAscii to 0 so the carriage return is discarded.
    Private Sub Form_KeyPress(KeyAscii As Integer)
        If KeyAscii = 13 Then
            SendKeys "{TAB}"
            KeyAscii = 0
        End If
    End Sub

Use Pictures on CommandButtons
To use a picture on a CommandButton, you must set its Picture property. You must also set its Style to 1 - Graphical. If you do not, the picture is ignored.
Send your Tips and Tricks to feedback@vb-helper.com.

Subscribe to the VB Helper newsletter
Copyright © 1997-2001 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
www.vb-helper.com/tips1.htm Updated