The Basics of VB6

From Free Knowledge Base- The DUCK Project: information for everyone
Revision as of 11:14, 23 September 2007 by Admin (Talk | contribs)

Jump to: navigation, search

These notes can be organized later.

Open a Form From Another Form

Open the form called frmCompanies

 frmCompanies.Show

Open it so that the parent form cannot be clicked. To suspend execution of the first form until after the second form is done with, add the keyword constant vbModal as an argument to the Show method.

 frmCompanies.Show vbModal

The Hide method of a form removes the form from the screen (makes it invisible), but the form still remains in memory.

 frmCompanies.Hide
 Me.Hide
 Hide

You can load a form in memory and not display it.

 Load frmCompanies

Unload form from memory (which also closes it)

 Unload Me

Example

 Private Sub Form_Unload(Cancel As Integer)
   If MsgBox("Are you sure you want to quit?", vbYesNo + vbQuestion, "Unload Test") = vbNo Then
     Cancel = 1
   End If
 End Sub

From Dimensions and Twip Units

The default unit is the twip, which is 1/1440 of an inch for height, width, top, and left.

There are 15 twips per pixel.

Pixels to Twip:

 W = W * Screen.TwipsPerPixelX
 H = H * Screen.TwipsPerPixelY 

Twips to Pixel:

 W = W / Screen.TwipsPerPixelX
 H = H / Screen.TwipsPerPixelY

Resizing Forms

  • Width: Specifies the width of the control. The default unit is the twip, which is 1/1440 of an inch.
  • Height: Specifies the height of the control. The default unit is the twip, which is 1/1440 of an inch.
  • Left: Specifies the distance between the internal left edge of a control and the left edge of its containter.The default unit is the twip, which is 1/1440 of an inch.
  • Top: Specifies the distance between the internal top edge of a control and the top edge of its containter.The default unit is the twip, which is 1/1440 of an inch.


  • ScaleWidth: The width of the client area.
  • ScaleHeight: The height of the client area.
  • ScaleLeft: Coordinate horizontal of upper left client area.
  • ScaleTop: Coordinate vertical of upper left client area.


  • ClientWidth:
  • ClientHeight:
  • ClientLeft:
  • ClientTop:

TabStrip Control

Tabs are not containers! In design time you cannot just select a tab and drop a control on it, assigning a different control to each tab. It just isn't how tabs work.

You must trap the click event and determine which tab is being clicked.

 Private Sub TabStrip1_Click()
   SelectedTab = TabStrip1.SelectedItem.Index
 End Sub

Setting the default tab

Private Sub Form_Load()
  'default to first tab (note: indexing starts at 1 - this is how control is designed)
  TabStrip1.Tabs(1).Selected = True
End Sub

Another Example

 Private Sub TabStrip1_Click
   TabFrame(TabStrip1.SelectedItem.Index-1).Zorder 0
 End Sub 

Tabs That Are Containers:

If you wish to use tabs that are containers, look at SStab, which is in the component Microsoft Tabbed Dialog Control 6.0 (SP5). The .OCX is TABCTL32.ocx.

Adding Controls to Tabs

  1. Add and setup the tabstrip control with the number of tabs desired.
  2. Add 1 frame control for each tab. Make them each the same height and width. Name each with something like fraFirstTab, fraSecondTab, etc.
  3. Add the controls that you want to appear when the first tab is clicked to fraFirstTab. Repeat this for each remaining tab (use fraSecondTab for the second tab, etc.)
  4. set the Top and Left properties for all frames to the same value so that it appears within the tabstrip control. You may need to adjust the tabstrip height and width to accomidate this.
  5. Set the Visible property for all frame to True.
  6. Select fraFirstTabe and insure that it is on top of the ZOrder. Do this by Selecting Format -> Order and see what options are available. If "Bring to Front" is grayed out and "Send to Back to in black, then its ok, If not, click "Bring to Front".
  7. Add the following code to the Click event procedure of your tabstrip control:
 Select Case TabStrip1.SelectedItem.Index
   Case 1: fraFirstTab.ZOrder 0
   Case 2: fraSecondTab.ZOrder 0
   ' add 1 CASE statement for each tab. Change the frame control name to correspond to the tab that has been selected.
 End Select
8. What SHOULD happen is the the frame that corresponds to the tab selected is moved to the top of the ZOrder, overlaying the other frames.

Select Case (Switch)

CASE in Visual BASIC does not "fall through" like in C.

 Select Case Button.Key
   Case "ListCache"
     mnuListCache_Click
   Case "Refresh"
     mnuRefresh_Click
   Case "simmode"
     cmdSimMode_Click
 End Select
 Select Case tcpClient.State
   Case 0
     tcpClient.RemoteHost = cboRemoteHost
     SessionConnect
   Case 7
     lblStatus.Caption = "Connected"
   Case 9
     lblStatus.Caption = "Connection Error"
 End Select

Case based on more than one condition

 Select Case True
   Case strMessage Like "*OK*" And lblStatus.Caption = "Connected"
   blnReceivedAll = True
 Case strMessage Like "*OK*" And lblStatus.Caption = "retrieving."
   lblStatus.Caption = "retrieving.."
   blnReceivedAll = True
 Case Else
   Set LoopTime = New TimerClass: tmrDisconnect.Enabled = True
 End Select

CommonDialog

ListView

Add the Component "Microsoft Windows Common Controls 5.0 (SP2)" by right-clicking on the Toolbox and selecting "Components", then the "controls" tab.

Create a ListView called "lvCustomDraw"

The "View" Property controls the appearance of the ListView. Try 3 - lvwReport.

 Private Sub Form_Load()
   ' fill the listview with some stuff....
   With lvCustomDraw
     .ColumnHeaders.Add , , "Item Column"
     .ColumnHeaders.Add , , "Subitem 1"
     .ColumnHeaders.Add , , "Subitem 2"
     
     Dim i&
 
     For i = 0 To 99
       With .ListItems.Add(, , "Item " & CStr(i))
         .SubItems(1) = "Subitem 1"
         .SubItems(2) = "Subitem 2"
       End With
     Next
   End With                     
 End Sub