Developers Dev Header Right.gif (1326 bytes)


bullet_brown.gif (945 bytes)Developer Home
bullet_brown.gif (945 bytes)Tricks and Tips
Page 1
Page 2
Page 3
Page 4
bullet_brown.gif (945 bytes)Submissions
bullet_brown.gif (945 bytes)Developer Links
bullet_brown.gif (945 bytes)Graphic Art Links

bullet_brown.gif (945 bytes)Home Page

Developers

Tricks and Tips

PAGE 1 - Click on 'Tricks and Tips' to return to the index page.

Is VB Running? A Simple Function Will Tell You

You can write a one-line function that will tell you if Visual Basic 5.0 is in run mode or design mode. The InRunMode() function is as follows:

  Function InRunMode(VBinst As VBIDE.VBE) As Boolean

    InRunMode = (VBinst.CommandBars("Run").Controls("End").Enabled = True)

  End Function

10 April 1997
by David Mendlen

Tricks and Tips Index                  Top of Page

 

Drag-and-drop in Visual Basic 5.0.

Drag-and-drop — It's not just for Word anymore! Visual Basic 5.0 offers control properties and events that let you drag and drop just as you can in Word and other Microsoft Office applications. To test this functionality, place two text boxes on a Visual Basic form. Set the text boxes' OLEDragMode property to 1-Automatic and their OLEDropMode property to 2-Automatic, then run the form. You can now select a word and drag it from one text box to the other. You can also use this functionality to drag between a Visual Basic text box and Word (or any application that uses drag and drop).

Anonymous

Tricks and Tips Index                  Top of Page

 

Determining whether a file exists

Visual Basic doesn't provide any built-in means for telling whether a particular file exists. However, you can use the DIR or DIR$ function to check a file's existence. Simply pass the filename to the DIR function and check the function's return value-if the function returns nothing (""), then there's no such file.
To try this technique, create a VB project, place a button on Form1, and enter the following code in the button's Click event:

  Private Sub Command1_Click()

    FileInQuestion = Dir("C:\AUTOEXEC.BAT")
    If FileInQuestion = "" Then
      MsgBox "No such file!"
    Else
      MsgBox "File exists!"
    End If

  End Sub

When you run the form and click the button, you'll see the success message box. To complete the test, edit the code so it searches for a file you know doesn't exist on your C: drive.

Anonymous

Tricks and Tips Index                  Top of Page

 

Auto-searching by entering text in a combo box

An auto-searching combo box finds a possible selection as the user enters information directly into the text portion of the combo box. For this example, you can use the Authors table from Visual Basic's BIBLIO.MDB database. Place the following code in a standard module (BAS file):

  Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    lParam As Any) As Long

  Public Const CB_FINDSTRING = &H14C

(If you're using 16-bit VB, make the appropriate adjustments to the declare statement.)

You'll need a static variable-one that remembers its value from each previous call to that subroutine or function-to keep track of how many characters you've entered in the combo box at any given time.

Enter the following code in the combo box's Change event:

  Private Sub Combo1_Change()

  Dim iStart As Integer
  Dim sString As String
  Static iLeftOff As Integer
 
    iStart = 1
    iStart = Combo1.SelStart
 
    If iLeftOff <> 0 Then
      Combo1.SelStart = iLeftOff
      iStart = iLeftOff
    End If

    sString = CStr(Left(Combo1.Text, iStart))
    Combo1.ListIndex = SendMessage(Combo1.hwnd, _
            CB_FINDSTRING, -1, ByVal CStr(Left( _
            Combo1.Text, iStart)))

    If Combo1.ListIndex = -1 Then
      iLeftOff = Len(sString)
      Combo1.Text = sString
    End If

    Combo1.SelStart = iStart
    iLeftOff = 0

  End Sub

The static variable iLeftOff helps determine how many characters you need to use in the sString variable that you then pass to the API call.

Run the application and notice how the cursor position stays at the proper position as you enter characters. The combo box displays a name if VB finds a match; otherwise, only the entered text appears.

May 19, 1997
by Chuck Kraatz

Tricks and Tips Index                  Top of Page

 

Creating Win32 region windows

The Win32 API includes a really amazing feature called region windows. A window under Win32 no longer has to be rectangular! In fact, it can be any shape that may be constructed using Win32 region functions. Using the SetWindowRgn Win32 function from within VB is so simple, but the results are unbelievable. The following example shows a VB form that is NOT rectangular. Enjoy!

  'This goes into the General Declarations section:
 
  Private Declare Function CreateEllipticRgn Lib "gdi32" _
          (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, _
          ByVal Y2 As Long) As Long
 
  Private Declare Function SetWindowRgn Lib "user32" _
          (ByVal hWnd As Long, ByVal hRgn As Long, _
          ByVal bRedraw As Boolean) As Long
 

  Private Sub Form_Load()
    Show 'The form!
    SetWindowRgn hWnd, _
                  CreateEllipticRgn(0, 0, 300, 200), _
                  True
  End Sub

February 23, 1998
By AlMoataz B. Ahmed,
AlMoataz_m@hotmail.com

Tricks and Tips Index                  Top of Page

 

Showing long ListBox entries as a ToolTip

Sometimes the data you want to display in a list is too long for the size of ListBox you can use. When this happens, you can use some simple code to display the ListBox entries as ToolTips when the mouse passes over the ListBox.

First, start a new VB project and add a ListBox to the default form. Then declare the SendMessage API call and the constant (LB_ITEMFROMPOINT) needed for the operation:

  Option Explicit

  'Declare the API function call.
  Private Declare Function SendMessage _
          Lib "user32" Alias "SendMessageA" _
          (ByVal hwnd As Long, _
          ByVal wMsg As Long, _
          ByVal wParam As Long, _
          lParam As Any) As Long

  'Add API constant
  Private Const LB_ITEMFROMPOINT = &H1A9

Next, add some code to the form load event to fill the ListBox with data:

  Private Sub Form_Load()

    'load some items in the list box
    With List1
      .AddItem "Michael Clifford Amundsen"
      .AddItem "Walter P.K. Smithworthy, III"
      .AddItem "Alicia May Sue McPherson-Pennington"
    End With

  End Sub

Finally, in the MouseMove event of the ListBox, put the following code:

  Private Sub List1_MouseMove(Button As Integer, Shift As Integer, _
                                            X As Single, Y As Single)

  'present related tip message
  Dim lXPoint As Long
  Dim lYPoint As Long
  Dim lIndex As Long

    If Button = 0 Then ' if no button was pressed
      lXPoint = CLng(X / Screen.TwipsPerPixelX)
      lYPoint = CLng(Y / Screen.TwipsPerPixelY)
  
      With List1
        'get selected item from list
        lIndex = SendMessage(.hwnd, _
                  LB_ITEMFROMPOINT, _
                  0, _
                  ByVal ((lYPoint * 65536) + lXPoint))
        'show tip or clear last one
        If (lIndex >= 0) And (lIndex <= .ListCount) Then
          .ToolTipText = .List(lIndex)
        Else
          .ToolTipText = ""
        End If
      End With '(List1)
    End If '(button=0)

  End Sub

February 2, 1998
By Matt Vandenbush, matt_vandenbush@whbrady.com

Tricks and Tips Index                  Top of Page

 

Creating Short Arrays Using the Variant Data Type

If you need to create a short list of items in an array, you can save a lot of coding by using the Variant data type instead of a dimensioned standard data type. This is especially handy when you need to create a list of short phrases to support numeric output.

For example, add a button to a standard VB form and paste the following code into the Click event of the button:

  Private Sub Command1_Click()

    'create a quick array using variants
    Dim aryList As Variant

    aryList = Array("No Access", "Read-Only", "Update", "Delete")
    MsgBox aryList(2)

  End Sub

26 January 1998
By Michael C. Amundsen, mike@amundsen.com

Tricks and Tips Index                  Top of Page

 

If you have any queries email:
developer@may-computing.com