Copyright: VB Extra ® by Jeremy Deveraturda


Contents
VISUAL "EXTRA" HELP FILE

WELCOME! This help file is filled with Visual Basic stuff: code, short program examples, and "How to". Anyone looking for a little extra code should use this help file. Anyone can copy any code material found here and use it in their own file (VB help files, etc.)

Something SIMPLE

Making a Startup form

Using CenterMe to center forms

Creating a Progress bar

Moving a menu item to the right side of the menu bar

Showing GDI, USER, and FSR usage resources

How to check the answer, YES or NO, in a message box

How to Restart Windows

How to create a Top Most window

Something for CONTROLS

How to Draw Shadows around Controls

Discarding Letters in a Textbox

Discarding Some Characters in a Textbox

CheckPeriod procedure

Overtype Mode in a Textbox

Password Textboxes

The Basic Edit Menu

Setting Defualt and Cancel properties for 3-D Buttons

Something for Fonts

Asking for a Font by TextHeight

Something for Drawing

How to draw a Pentagon

About stuff

About the Developer

This Version of VB Extra

Trademarks


Making a Startup form

A startup form is usually for long projects, or complicated forms. And without a startup form, your project will seem slow and unorganized. A startup form is a trick that will make the user feel like as if it's loading faster- and you can display all types of information in a startup form.

Creating a Startup form

Create a form. Make sure you delete all text in the caption, MaxButton, MinButton and the ControlBox are all set to False, and the BorderStyle = "1-  Fixed Single" Name the form StartupForm.

Create a Sub Procedure called Main. If you have a module already, add this procedure to it so you can access it all times during run-time. Type:

Sub Main ()
Screen.MousePointer = 11 'Set the hourglass cursor
CenterMe StartupForm	'Center Startupform
StartupForm.Show	'Show StartupForm
Startupform.Refresh	'Draw All
MainForm.Show	'Show the program
Unload StartupForm	'Remove StartupForm
Screen.MousePointer = 0    'Restore the mouse pointer
End Sub

You'll find the code for CenterMe in the CenterMe topic. Use Search... or go to the Contents.

Replace the name for MainForm  if you don't use this name.

After typing this procedure, go to the Options menu and select Project.... You will see a dialog box. Click on Start Up Form and then the down arrow next to the x and checkmark buttons. Scroll down to Sub Main. Click OK


CenterMe

Use this code with the Startup form procedure.

Use CenterMe for non-MDI windows such as a dialog box. You should use CenterMe BEFORE you use Show to display the form. For two reasons:

If you use CenterMe after Show, you will see the form move at run-time. This looks very unprofessional.

If you display the form as modal (form.Show 1) and then use CenterMe, Visual Basic won't listen to the next command following Show until your new form is removed from the screen.

The Code for CenterMe

Sub CenterMe (frm as Form)
Dim x, y	'New directions for the form

x = (Screen.Width - frm.Width) / 2
y = (Screen.Height - frm.Height) / 2
frm.Move x, y	'Change the location of the form
End Sub

The syntax for CenterMe is:
CenterMe form
Replace form with a name of a form (E.G Form1)

You can use CenterMe in the Form_Load event, causing the form to be centered each time it's loaded, or before the Show method.

You can use CenterMe in the Form_Resize event. This will make the window always centered, even if the user changes the size of your form.


Making a Progress bar
Creating a Progress bar

Progress bars are useful for lengthy operations such as installation routines or downloading routines. You can use a Progress bar for your own business, and here's how to make one:

The Progress bar code

Create a picture box on your form and give it a control name, such as ProgBar.

Create a Sub Procedure, Progress. You'll find the code for it below.

Make a call to Progress in your program. You need to supply two parameters to this subroutine: The name of the picture box you created, and a number between 0 and 100. Example: Progress ProgBar, 50

Or use the example below the code.

Sub Progress (pb As Control, ByVal percent)
    Dim num$                    ' Used to hold percent string

    If Not pb.AutoRedraw Then   ' Do we have off-screen image?
        pb.AutoRedraw = -1      ' No, set one up.
    End If

   	 pb.Cls                      ' Clear off-screen image
    pb.ScaleWidth = 100         ' Set for percentage
    pb.DrawMode = 10            ' Set for erasable drawing

    ' Display the percentage, centered in the picture
    ' box.
   '
    num$ = Format$(percent, "##0") + "%"
    pb.CurrentX = 50 - pb.TextWidth(num$) / 2
    pb.CurrentY = (pb.ScaleHeight - pb.TextHeight(num$)) / 2
    pb.Print num$               ' Display percentage

    ' Draw box over the percentage.

   	 pb.Line (0, 0)-(percent, pb.ScaleHeight), , BF
    pb.Refresh                  ' Show changes on screen
End Sub

Place the following code in a command button click event, or a form_load event.

Picture1.ForeColor = RGB(0, 0, 255)	'Use a blue color
For i = 0 to 100 Step 5
Progress Picture1, i
Next i
Picture1.Cls

Note: The control picturebox name is Picture1 in this example. Change Picture1 to your control name if you named it something else.


Moving a menu item to the Right

This is usually for a Help menu item

You may of seen "big" programs do this. They have a menu item all the way to the right, usually a Help menu. Have you ever wondered how they did this? I sure did, and I finally found out how.

The Code for moving it

You must use this code during run-time in the Form_Load event.

Menu.Caption= Chr$(8) & Menu.Caption

Replace Menu with a real control menu name such as menuHelp.


Showing GDI, USER, System resources
Showing GDI, USER and FSR usage

Great code for an about box form.

To write a program that displays GDI, USER and FSR usage, do the following:

Create a form

Add a Timer control to the form. Set the Interval to 1000

Add 3 labels.
Name  	Caption

Label1	 FSR USAGE
Label2	USER USAGE
Label3	GDI USAGE

Add 3 more labels and line them up with the first 3 labels you created.
For example, Label1 should be next to label 4, Label2 should be next to label 5 and so on.

Name	Caption

Label4	Label4
Label5	Label5
Label6	Label6

Add the following code in the Declarations section of  the form or a module. Make sure it is all one line.

Declare Function GetFreeSystemResources Lib "User" (ByVal ResType As Integer) As Integer

Add the following code in the Timer1_Timer event:

LABEL4.Caption = GetFreeSystemResources(0)
LABEL5.Caption = GetFreeSystemResources(1)
LABEL6.Caption = GetFreeSystemResources(2)


Checking the answer YES or NO
Checking YES or NO in a msgbox

Here's a short code for a msgbox.

How do you write a code that checks if the user chose Yes instead of No in a msgbox? Or Yes instead of Cancel? Here's the code below:

The Chose Yes? code

Dim Msg
Msg = "Pick Yes or No"	'Here's a message
If MsgBox(Msg$, 4 + 32 + 256) <> 6 Then  	'Msgbox with a question mark	'andYES/ NO buttons.
Msgbox "You chose No"
Else
Msgbox "You chose Yes"
End If

This is what it means:
IF MsgBox(Msg.....) <> 6 Then
If the user chose anything besides Yes (Yes means 6) then
.... do whatever needs to be done.
End If


How to Restart Windows

You may need a code that restarts Windows to save all changes made in the Program Manager. Here's the code:

Restart Windows Code

Add the following in the declarations section or in a module.
Make sure it is all in one line.

Declare Function ExitWindows Lib "User" (ByVal RestartCode As Long, ByVal DosReturnCode As Integer) As Integer

Then, add a menu item called RestartWindows. Then add this code in it.

i = ExitWindows(66, 0)


Creating a Top Most window
How to Create a Top Most window

To create a Top Most window, or a floating window like Program Manager's Clock, here's the code:

The Code for Top Most

Add this code in the declarations of the form or in a module.
Make sure it is one line.

Declare Sub SetWindowPos Lib "User" (ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer)

Add this in the Form_Load event:

 Call SetWindowPos(Me.hWnd, -1, 0, 0, 0, 0, 27)

The code above will make the window float above every window in Program Manager at Run-Time.


Drawing Shadows around Controls
Drawing Shadow around Controls

Draws a drop shadow around any control on your form.

If you ever wanted a shadow around a control, here's a subroutine. Place a textbox on the Form1 window. Place a subroutine called Shadow in a Form_Paint event.

Sub Form_Paint ()
Shadow Form1, Text1
Shadow Form1, Label1
End Sub

Create the Shadow subroutine in a module so you can access it all times. You can also change the color and width by changing the color and shWidth constants at the start of Shadow.

The Shadow Code

Sub Shadow (f As Form, c As Control)

    ' This subroutine draws a gray shadow below and to the
    ' right of a control.  This subroutine must be in one
    ' your form.

    Const color = &HC0C0C0      ' Color of the shadow
    Const shWidth = 3           ' Width of the shadow
    Dim oldWidth As Integer     ' Saves old DrawWidth
    Dim oldScale As Integer     ' Saves old ScaleMode

    oldWidth = f.DrawWidth      ' Remember current DrawWidth
    oldScale = f.ScaleMode      ' Remember current ScaleMode

    f.ScaleMode = 3             ' Set to Pixel scaling
    f.DrawWidth = 1             ' 1-pixel wide lines

    ' Draws the shadow around the control by drawing a gray
    ' box behind the control that's offset right and down.

    f.Line (c.Left + shWidth, c.Top + shWidth)-Step(c.Width - 1, c.Height - 1), color, BF

    f.DrawWidth = oldWidth      ' Restore old DrawWidth
    f.ScaleMode = oldScale      ' Restore old ScaleMode

End Sub


Discarding Letters

The following code only accepts the digits zero through nine: (Add a text box and put the code in the keypress event)

The code:

Sub Text1_KeyPress (KeyAscii As Integer)
If KeyAscii < Asc (" ") Then	'Is this Control Char
Exit Sub
End If

If KeyAscii < Asc ("0") Or KeyAscii > Asc ("9") Then
KeyAscii = 0
End If
End Sub

Now the textbox will accept any number of digits, but it won't accept any other characters.


Discarding Some Characters

This subroutine (DoKeyPress) discards any characters that can't be in a number format. The only characters allowed are:
0 - 9 All digits
-        A minus, only if it is the first character
.       Periods are allowed

Type this code in a module or the declarations of a form. There is also another subroutine DoKeyPress uses, CheckPeriod. Use Search or the Contents to find this topic.

The DoKeyPress code

Sub DoKeyPress (t As Control, KeyAscii As Integer)
    If KeyAscii < Asc(" ") Then     ' Is this Control char?
        Exit Sub                    ' Yes, let it pass
    End If

    CheckPeriod t                   ' Remove excess periods

    If KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Then
        ' keep digit
    ElseIf KeyAscii = Asc(".") Then
        ' keep .
    ElseIf KeyAscii = Asc("-") And t.SelStart = 0 Then
        ' Keep - only if first char
    Else
        KeyAscii = 0                ' Discard all other chars
    End If

    ' This code keeps you from typing any characters in front of
    ' a minus sign.

    If Mid$(t.Text, t.SelStart + t.SelLength + 1, 1) = "-" Then
        KeyAscii = 0                ' Discard chars before -
    End If
End Sub


Check Period
CheckPeriod procedure

Use this with the DoKeyPress subroutine.

The subroutine, DoKeyPress needs the procedure. This subroutine makes sure your text box never has more than one period in it. You can also use this subroutine separate with your project.

The CheckPeriod procedure

Sub CheckPeriod (t As Control)

    Dim i As Integer

    i = InStr(1, t.Text, ".")   ' Look for a period
    If i > 0 And InStr(i + 1, t.Text, ".") > 0 Then
        t.SelStart = t.SelStart - 1
        t.SelLength = 1         ' Select new period
        t.SelText = ""          ' Remove new period
    End If
End Sub


Overtype Mode

The Visual Basic textbox control does not support Overtype mode. Add this code to a textbox so you can use the Insert key.

Add this code to your Text1_KeyPress event.

Overtype Code

Sub Text1_KeyPress (KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii <> 13 And Text1.SelLength = 0 Then
Text1.SelLength = 1
End If
End Sub


Password Textboxes

Here's a way to have a password character in your textbox. Set the following properties for a textbox.

Property	Setting

PasswordChar	Alt+0183
FontName	Symbol
FontBold	False

The code uses the character, · as the password character.
To create the ·, hold down the Alt key and type 0183 on your numeric keypad with the Alt key still down and Num Lock on.


The Basic Edit Menu

This topic shows how to create an Undo, Cut, Copy, and Paste menu items for a textbox.

All Window programs should take advantage of the Window Clipboard, such as an Edit menu would. Below shows how to create a code for Undo, Cut, Copy, Paste, and Select All. Create a menu with the following menu items and properties.:

Caption	Control Name  	Accelerator

&Edit	menEdit
   &Undo	miUndo	Ctrl+Z
    -	mEditiSepBar1
   Cu&t 	miCut	Ctrl+X
   &Copy	miCopy	Ctrl+C
   &Paste	miPaste	Ctrl+V
   -	miEditSepBar2
   Select &All  miSelectAll

The Edit Menu code:

Now after that, type in the code for each menu item.

Sub miUndo_Click ()
Sendkeys "%{BACKSPACE}"	'Send Alt+Backspace
End Sub

Sub miCut_Click ()
Sendkeys "+{DELETE}"	'Send Shift+Del
End Sub

Sub miCopy_Click ()
Sendkeys "^{INSERT}"	'Send Ctrl+Ins
End Sub

Sub miPaste_Click ()
Sendkeys "+{INSERT}"	'Send Shift+Ins
End Sub

Sub miSelectAll
    text1.SelStart = 0            	            'Set text selstart
    text1.SelLength = Len(text1.Text)            'Select all text
End Sub

The shortcuts listed here are standard shortcuts Microsoft suggests all programs should use.


Setting Defualt and Cancel
Setting Defualt and Cancel for 3D Buttons

You can only use the code in this topic code if you have THREED.VBX

3D Buttons which are part of THREED.VBX (found in VB Prof. Edition) don't support the Defualt or Cancel property. What happens if you need them? The following helps to solve the problem:

Create your form with all the controls you need. Include the 2 3D Buttons with the contol name cmdOpen and cmdCancel

Size the form for all the controls

Create two standard Command buttons, called cmdoOpen and cmdoClose (for this example)

Set the Defualt property of cmdoOpen to True and set the TabStop property to False. Set the Cancel property of cmdoCancel to True and set the TabStop to False.

Then add the following code:

Sub cmdoOpen_Click()
cmdOpen_Click
End Sub

Sub cmdoCancel_Click ()
cmdCancel_Click
End Sub

Now cmdOpen and cmdCancel will appear to have Defualt and Cancel properties.

Resize the form so the two standard command buttons won't be visible. Then add the following code if your form is resizable:

Sub Form_Load ()
cmdoOpen.Left = -1000
cmdoCancel.Left = -1000
End Sub

With this code, you can place the standard command buttons anywhere at design time.


Asking for a Font by TextHeight

Please read the "This Version" topic about this topic.

When you set the TextHeight property of a control or a form, you're setting the height of the font in points. But, what happens when you want to set the height
of the font to a certain size  in twips?

The FindFontSize function is just what you need. You use FindFontSize  by the following:

ScaleMode = 3
Label1.FontSize = FindFontSize(Form1, 18)

This code shows how to have a label (called Label1) that is no more than 18 pixels high.

Here's the code you can put into a module or a form.

FindFontSize code:

Function FindFontSize (f As Form, ByVal high)

    ' This function returns the first font with a
    ' TextHeight <= high.

    ' Note: If the smallest font has a TextHeight > high, this
    '       function will return a font larger than high.

    Dim i, oldSize, size
    Dim oldScale

    oldSize = f.FontSize            ' Remember old font size

    ' Convert starting height to points.

    oldScale = f.ScaleMode          ' Remember scale mode
    CurrentY = high                 ' Set Y to height we want
    ScaleMode = 2                   ' Switch to points
    f.FontSize = CurrentY           ' Set to size, in points
    f.ScaleMode = oldScale          ' Restore the scale mode

    i = f.FontSize                  ' Size we'll start at
    Do
        If f.FontSize < i Then      ' Is size smaller than i?
            i = f.FontSize          ' Yes, adjust i to real size
        End If
        i = i - .5                  ' Try next smaller size
        f.FontSize = i              ' Set font to this size
    Loop While f.TextHeight("A") > high And i > 1
    FindFontSize = f.FontSize       ' Return size of found font

    f.FontSize = oldSize            ' Restore the old font size
End Function


How to draw Rulers on your Form

The ruler-drawing code is in the Ruler.bas I also uploaded. Ruler.bas consist of many subroutines, so I only gave you the code on how to call it. To use Ruler.bas use Add File and select Ruler.bas. Ruler.bas  will add to your project as a module.

Code for calling Ruler.bas

Type this in the Form_Paint event.

Sub Form_Paint ()

    Dim iWidth As Integer, iHeight As Integer
    Dim Wid As Integer
    Dim i As Integer
    Const offset = 250

    iWidth = ScaleWidth / 1440
    iHeight = ScaleHeight / 1440
    DrawXRuler Form1, offset, 0, iWidth
    DrawYRuler Form1, 0, offset, iHeight

    FontBold = 0
    Wid = iWidth
    If iHeight > Wid Then Wid = height
    For i = 0 To Wid
        CurrentX = offset + i * 1440: CurrentY = 0
        Print i
        CurrentX = 0: CurrentY = offset + i * 1440
        Print i
    Next i

End Sub

Again, it is important you add Ruler.bas to your project for this code to work!


Pentagon drawing
How to draw a Pentagon

Here's a program that draws a pentagon with a 2-pixel wide black border, and filled with light gray.

The following lines should be in the declarations of your form:

Dim points(10) As POINTAPI
Dim numPoints As Integer

The next code should be in the Form_Load event and the next one should be in the Form_Paint event:

Sub Form_Load ()
numPoints = 5
dTheta = 4 * 3.1415926 / 5
For i = 1 to numPoints
points(i).X = 100 * Sin(dTheta * i) + 110
points(i).Y = -100 * Cos(dTheta *i) + 110
Next i
End Sub

Sub Form_Paint ()
ForeColor = RGB(0, 0, 0)	'Draw lines in black
FillColor = RGB(192, 192, 192, 192)	'Fill with light gray
FillStyle = 0	'Set a solid fill
DrawWidth = 2	'Set line width to 2
j% = Polygon(hDC, points(0), numPoints)
End Sub

The rest should be in a module:

Type PointApi
    X As Integer
    Y As Integer
End Type
'
'Make sure this is one line:
'
Declare Function Polygon Lib "gdi" (ByVal hDC As Integer, pPoints As PointApi, ByVal nCount As Integer) As Integer


About the Developer

Hey there! This is my first help program I developed and uploaded, and I'm glad to share it with you. I developed this help file for Visual Basic programmers who need a little extra kick in their programs, such as a startup form, how to make a Top Most window, that kind of stuff. I'm only 13 (well right now 1994) and I developed all other Visual Basic programs. Here are some I uploaded: (From new to old)

VB "Extra"

Neat Clock

CPU Mail

MANY, MANY THANKS FOR DOWNLOADING VB EXTRA

Sincerely,
Jeremy 	                                                      D.

How to reach me?

Mail letters to this address if you have any question or comments about VB EXTRA or any other programs from me. I'll try to answer your questions as best I can. Thank you.

Jeremy Deveraturda
76 Morgan Place
East Brunswick, New Jersey 08816

OH BY THE WAY: If you download any of the programs listed above (VB Extra, Neat Clock and/or Mail) and it says the program was done by JD Interactive Inc., please note that JD Interactive Inc IS NOT A REAL COMPANY. All software that I produce are titled as JD Interactive software stuff.


This Version of VB Extra

Please note all code written here are raw. I didn't check if they work as documented, but I'm pretty sure they work. I apologize for lack of information for the "Something for Fonts" and "Something for Drawing" group. If I plan to create future upgrades, I will try to expand this group.

PLEASE NOTE I AM NOT RESPONSIBLE FOR ANY DAMAGE CAUSED BY ANY CODE SHOWN IN VB EXTRA FILES

When using VB Extra (help file), please maximize the window . Click on the up arrow button located at the top-right corner of the window.

Please note that if you need to use VB Extra, you should know the Visual Basic for Windows environment. You should know what a module is, how to add a procedure etc.

If you have any questions about VB Extra, or comments please write to this address. Also if you find any code incomplete, missing please write to this address as well. Please explain the error.  I'll try to respond quickly as possible.

Jeremy Deveraturda
76 Morgan Place
East Brunswick, New Jersey 08816
USA


TRADEMARKS

All brand names and product names used in VB Extra are trademarks, registered trademarks or trade names of their respective holders.

All codes listed in VB Extra are codes from their respective programmers.


Caption	Control Name  	Accelerator

&Edit	menEdit
   &Undo	miUndo	Ctrl+Z
    -	mEditiSepBar1
   Cu&t 	miCut	Ctrl+X
   &Copy	miCopy	Ctrl+C
   &Paste	miPaste	Ctrl+V
   -	miEditSepBar2
   Select &All  miSelectAll