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 3 - Click on 'Tricks and Tips' to return to the index page.

Dragging items from a list to another one

Here's a way that you can let users drag items from one list and drop them in another one. Create two lists (lstDraggedItems, lstDroppedItems) and a text box (txtItem) in a form (frmTip).

Put the following code in the load event of your form.

Private Sub Form_Load()
  ' Set the visible property of txtItem to false
  txtItem.Visible = False
  'Add items to list1 (lstDraggedItems)
  lstDraggedItems.AddItem "Apple"
  lstDraggedItems.AddItem "Orange"
  lstDraggedItems.AddItem "Grape"
  lstDraggedItems.AddItem "Banana"
  lstDraggedItems.AddItem "Lemon"
  '
End Sub

In the mouseDown event of the list lstDraggedItems put the following code:

Private Sub lstDraggedItems_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  '
  txtItem.Text = lstDraggedItems.Text
  txtItem.Top = Y + lstDraggedItems.Top
  txtItem.Left = X + lstDraggedItems.Left
  txtItem.Drag
  '
End Sub

In the dragDrop event of the list lstDroppedItems put the following code:

Private Sub lstDroppedItems_DragDrop(Source As    Control, X As Single, Y As Single)
  '
  If lstDraggedItems.ItemData(lstDraggedItems.ListIndex)    = 9 Then
    Exit Sub
  End If
  ' To make sure that this item will not be selected again
  lstDraggedItems.ItemData(lstDraggedItems.ListIndex)    = 9
  lstDroppedItems.AddItem txtItem.Text
  '
End Sub

Now you can drag items from lstDraggedItems and drop them in LstDroppedItems.

Note that you cannot drag from the second list to the first. Also, the dragged item remains in the first list. You'll have to address those limitations yourself.

March 30, 1998
By Bassam Alkharashi.

Tricks and Tips Index                  Top of Page

 

Getting sensible Win32 API call errors

Most of the Win32 API calls return extended error information when they fail. To get this information in a sensible format, you can use the GetLastError and FormatMessage APIs.

Add the following declarations and function to a BAS module in a VB project:

Option Explicit

Public Declare Function GetLastError _
     Lib "kernel32" () As Long
Public Declare Function FormatMessage _
     Lib "kernel32" Alias "FormatMessageA" _
     (ByVal dwFlags As Long, _
     lpSource As Any, _
     ByVal dwMessageId As Long, _
     ByVal dwLanguageId As Long, _
     ByVal lpBuffer As String, _
     ByVal nSize As Long, _
     Arguments As Long) As Long

Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000

Public Function LastSystemError() As String
     '
     ' better system error
     '

     Dim sError As String * 500
     Dim lErrNum As Long
     Dim lErrMsg As Long
     '

     lErrNum = GetLastError
     lErrMsg = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, _
          ByVal 0&, lErrNum, 0, sError, Len(sError), 0)
     LastSystemError = Trim(sError)
     '

End Function

Now place a command button on a standard VB form and call the LastSystemError function:

Private Sub Command1_Click()
     '
     MsgBox LastSystemError
     '

End Sub

If there was no error registered, you'll see a message saying "The operation completed Successfully."

When using this function, keep these points in mind:

1. Many API calls reset the value of GetLastError when successful, so the function must be called immediately after the API call that failed.

2. The last error value is kept on a per-thread basis, therefore the function must be called from the same thread as the API call that failed.

January 12, 1998
By Duncan Jones.

Tricks and Tips Index                  Top of Page

 

Specifying maximum lengths in a ComboBox

The ComboBox control doesn't have a MaxLength property like a TextBox does. You can add some code to emulate this property, however. Just add the following code to the KeyPress event of your ComboBox:

Private Sub Combo1_KeyPress(KeyAscii As Integer)
  'If the user is trying to type the eleventh
   key and...
  ' ...this key is not the Backspace Key,
   cancel the event!
  Const MAXLENGTH = 10
  If Len(Combo1.Text) >= MAXLENGTH And KeyAscii
     = vbKeyBack Then KeyAscii = 0
  '
End Sub

You can change the MaxLength value to any number you want. As you can see, the code allows the user to use the [Backspace] key; you could enable other keys by simply adding their KeyAscii values the way we did with [Backspace].

December 22, 1997
by Roberto Giacometti Machado.

Tricks and Tips Index                  Top of Page

 

Clearing all fields and combo boxes on a form

Sometimes you want to clear all the fields and combo boxes on a data-entry form. If your form contains many controls, this could become tedious and error prone. The following subroutine clears the contents of such fields on your form automatically:

Public Sub ClearAllControls(frmForm As Form) Dim ctlControl As Object

  ' Initialize all controls that can be
    initialized
  ' Any control with a text property or a
    list-index property
  On Error Resume Next
  For Each ctlControl In frmForm.Controls
    ctlControl.Text = ""
    ctlControl.ListIndex = -1
    DoEvents
  Next ctlControl
End Sub

Just call this procedure from your code like this:

Call ClearAllControls(Me)

December 15, 1997
By John Baumbach.

Tricks and Tips Index                  Top of Page

 

Preventing multiple instances of VB apps

You can easily prevent users from running multiple instances of your programs by taking advantage of the PrevInstance property of the App object.

To do so, enter the following code in your application's opening form:

If App.PrevInstance Then
    MsgBox ("Cannot load program again."),
      vbExclamation, "The requested " _
    & "application is already open"
    Unload me
End If

This technique will also prevent multiple users from accessing single-user applications.

October 27, 1997
by Saptadeep Dutta

Tricks and Tips Index                  Top of Page

 

Case-conversion on the fly

If you want to convert text to uppercase as it's entered in a text box, just create an Upper function and call it from the text box's keypress event, as shown here:

     Private Sub Text1_KeyPress(KeyAscii As Integer)
        KeyAscii = Upper(KeyAscii)
    End Sub

    Function Upper(KeyAscii As Integer)
        If KeyAscii > 96 And KeyAscii < 123 Then
            KeyAscii = KeyAscii - 32
        End If
        Upper = KeyAscii
    End Function

This technique eliminates the need to "UCase" entered data. It also makes "hotseek" data searches much easier.

October 20, 1997
by Chris Warnke, Dell Computer Corp.

Tricks and Tips Index                  Top of Page

 

Retrieving the network logon name

You can easily retrieve a user's network logon name by using the following API call:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

To retrieve a "clean" version of the name, use this function:

Public Function NTDomainUserName() As String
Dim strBuffer As String * 255
Dim lngBufferLength As Long
Dim lngRet As Long
Dim strTemp As String

    lngBufferLength = 255
    lngRet = GetUserName(strBuffer, lngBufferLength)
    strTemp = UCase(Trim$(strBuffer))
    NTDomainUserName = Left$(strTemp, Len(strTemp) - 1)

End Function

November 3, 1997
by Barron Anderson, Micron Electronics Inc.

Tricks and Tips Index                  Top of Page

 

 

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