March 7, 2021
Hot Topics:

Creating a Visual Basic 6 Add-In

  • By John Cunningham
  • Send Email »
  • More Articles »

The next step will be to add a Text Box, four Labels and another Command Button to frmConvert. First, change the captions of the two existing Command Buttons from "OK" and "Cancel" to "Convert" and "Exit", then add the third Command Button, set it's Caption to "Clear" and name it cmdClear. You can put the Labels in a Frame as shown below if you like, but it is not necessary.

Next, we will add the number input buttons as an Array. Start by placing another Command Button on the Form, set it's Caption to 0 and it's Name to Btn. Using the Right Mouse Button, Click on Btn to highlight it and then select "Copy", next, Right Click the Mouse on the Form and select "Paste". Click "yes" to VB's question box. VB responds by placing a copy of the original Button in the upper left hand corner of the Form. Change this new Button's Caption to 1, a check of the Properties Window reveals that this Button's Name is Btn(1) and the original Button's Name is Btn(0).

This is exactly what we wanted, the start of our Control Array for all of the number Buttons. Proceed with this method to establish the balance of the Array for number buttons 2 through 9. In the end you should have ten number buttons labeled zero through nine and their names will be Btn(0) through Btn(9). Your Form should now look like the following;

Form Design
Form Design

You will notice that I have changed the Form's Caption also. Now we are ready to Code the Form.

If you look at the Form's General Declarations Section of the Code you will see the following:

Option Explicit
Public VBInstance As VBIDE.VBE
Public Connect As Connect

These three lines of code are part of the Add-In Template. We are going to change the Second Line to suit our Program's purpose. Change Line 2 as follows:

'Change the name of the Designer here - on the second Line
Public Connect As NumConverter

Now add the following below "Option Explicit" in the General Declarations Section.

Dim Adecimal, digit, h, i, j, k As Integer
Dim jbin As String

Next, we will add the Subroutine DecBin to the Project, after the above Dimension Statements. We will not delve into the workings of this routine, suffice it to say that it converts an Integer or Decimal Number to a Binary Number.

Public Sub DecBin(Adecimal, jbin As String)
' Convert integer value to an equivalent string of binary digits
jbin = ""
h = Hex(Adecimal)  ' convert from integer to hexadecimal
For i = 1 To Len(h)
digit = InStr("0123456789ABCDEF", Mid(h, i, 1)) - 1
j = 8
k = 4
Do 'convert from hexadecimal to binary
jbin = jbin + Right(Str((digit  j) Mod 2), 1)
j = j - (j  2)
k = k - 1
If k = 0 Then Exit Do
Loop While j
Next i
End Sub

The following is the Code to complete the Form's Events:

Private Sub Btn_Click(Index As Integer)
Text1 = Text1 & Index
End Sub
Private Sub CancelButton_Click()
'Place the next 3 lines here as well as in the cmdClear Sub
Text1.Text = ""
'to clear out memory when we disconnect the Add-In
Frame1.Visible = False
End Sub
Private Sub cmdClear_Click()
Text1.Text = ""  'Clears out TextBox and memory
Text1.SetFocus   'to allow a new conversion
Frame1.Visible = False
End Sub
Private Sub Form_Load()
'Center Form on the Screen
Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2
End Sub
VB-6 Add-In Tutorial
Page 6
Private Sub OKButton_Click()
'Comment out original Code placed here by the Add-In Template
 'MsgBox "AddIn operation on: " & VBInstance.FullName
Adecimal = Val(Text1) 'get the number from the TextBox
Frame1.Visible = True
'Make the Call to convert from decimal to binary
Call DecBin(Adecimal, jbin)
Label1.Caption = "Decimal = " & Text1
Label2.Caption = "Binary = " & jbin  ' display the result
Label3.Caption = "Hex   = " & Hex(Adecimal)
Label4.Caption = "Octal  = " & Oct (Adecimal)
End Sub

That's it for the Coding of the Projects Events, now on with the next part.

Page 3 of 5

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date