Tutorial - User Interface - Dialog Box/Forms

#1
One of the greatest benefits of VBA is its ability to easily interface with the user.

Dialog boxes or Forms as they are called in VBA provide a simple user interface that can handle many tasks.

Some of these tasks go beyond just user selections (such as radio buttons and check boxes) and can provide error trapping, visual indicators/information that enhance the users understanding of the program.

Behind all controls placed on a form there are a series of actions that can be called when the user selects the item (called Events in VBA). An event is some action that has occurred and can take many forms such as a mouse click or double click or even a selection of another control.

Try this out:

With a new drawing open type VBA at the command prompt to enter the VBA IDE.

Insert a UserForm into the drawing by selecting the InsertUserform from the pull-down menu.

Drag two Command Buttons onto the userform and VBA will name these CommandButton1 and CommandButton2 .

Double click on the CommandButton1 to open the code window of the form and you should see:

Private Sub CommandButton1_Click()

End Sub

There are two pulldown boxes at the top of the code window – one reads CommandButton1 and the other Click. Yes you guessed it - it is the Click Event.

Select the Click pulldown box arrow to display a list of other Events that you can select. Select DblClick and the following code will appear:

Private Sub CommandButton1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

End Sub

Delete the Sub CommandButton1_Click() Event because we will be using the Double Click Event.

In the Sub CommandButton1_DblClick Event type in:

Code Starts Here 

If CommandButton2.Visible = True Then
CommandButton2.Visible = False
Else
CommandButton2.Visible = True
End If

Code Ends Here

Now select the Userform and Run it.

Click on CommandButton1 – Nothing Happens

Double Click on CommandButton1 – CommandButton2 disappears.

Double Click on CommandButton1 again – CommandButton2 appears.

The code we added just makes CommandButton2.Visible either True or False.

Armed with these Events we can display all the controls we want based on user selections – say we have two options, One Circular and the other Rectangular. When the user selects Circular we make visible a TextBox and Label Control on the form and if the user selects Rectangular we can make visible two TextBox and Label Controls on our Form. This saves us room around the drawing environment because we only need one toolbar button to drive both Circular and Rectangular programs.

Next I will create a project for editing text efficiently.


------------------
Regards
John Finlay
Don't want to post a question - email me direct on john@acecad.com.au

#2
To create an editor for MultiText in VBA is very easy.

Open the VBA IDE by typing VBA at the command prompt in a drawing and select CommonProjects in the Projects Window (Upper Left Window)

Select Insert-->Userform from the pulldown menu and name it frmEditMText then set the Caption Property to Edit Multi-Text V1-- CMS Tutorial.

Drag a TextBox onto the form and enlarge it to suit the size of your Multi Text Edit Window. Change the name of the TextBox to txtEditM then change the Property Scrollbars to 2 – frmScrollBarsVertical to allow us to have scrollbars when the Multi-Text is large. To allow us to type multilines in our textbox select the property Multilines and change this setting to true.

Now add two CommandButtons and name them cmdUpdate and cmdCancel.

Add a label to the top of the form and set the Caption Property = To start multi-text on a new line type \P after a paragraph.

The form should look like this:
Image


Double click on the form to open the form code window and copy/paste the code below

‘ Code Starts Here 

Option Explicit

Private Sub cmdCancel_Click()
' When the user selects the Cancel Button the program ends
End
End Sub

Private Sub cmdUpdate_Click()
' When user selects Update Button the Multi-Text is equal to the text in our
' textbox named txtEditM
myMText.TextString = txtEditM
myMText.Update

' Unload the user form and end the editing session
Unload frmEditMText

End Sub

Private Sub UserForm_Initialize()
' When the userform is initialized the selected Multi-Text is displayed in the textbox
' named txtEditM
txtEditM = myMText.TextString

End Sub

‘ Code Ends Here 

With CommonProjects still selected add a module to drive our form and add the code below:

‘ Code Starts Here 

Option Explicit

Public myMText As mText

Sub TextMEdit()

Dim Pnt As Point
On Error Resume Next
IntelliCAD.ActiveDocument.Utility.GetEntity myMText, Pnt, "Select Text"

If myMText Is Nothing Then End

frmEditMText.Show


End Sub

‘ Code Ends Here 

To run our Multi-Text Editor create a Toolbar button and insert the following command:

^C^C^C(Command "-VBARUN" "modYourModuleName.TextMEdit")

Where modYourModuleName is the Name of the Module that holds the Sub TextMEdit.

Well this is still not quite a complete Multi-Text Editor and we will need to add further features.

What features would you like to have added?

Maybe change the text height or a find facuility to find words in our Multi-Text.



------------------
Regards
John Finlay
Don't want to post a question - email me direct on john@acecad.com.au

#3
John,

This works good. Thank you for your time and help. Could the following be added?

1. Be able to change text height and fonts (within the same line) as you mentioned.

2. Spell check (could and external spell checking program be called such as Word?).

This is great, I need to learn VBA--in my spare time when I get some.

Thank you again,

Scott

#4
John,

Found this routine to use Word's spell checker. It is modified slightly to work within this routine:

<B>Private Sub cmdSpellCheck_Click()
Dim oWord As Object
Dim oTmpDoc As Object
Dim lOrigTop As Long

' Create a Word document object...
Set oWord = CreateObject("Word.Application")
Set oTmpDoc = oWord.Documents.Add

' Position Word off screen to avoid having document visible...
lOrigTop = oWord.Top
oWord.WindowState = 0
oWord.Top = -3000

' Assign the text to the document and check spelling...
With oTmpDoc
.Content.Text = txtEditM.Text
.Activate
.CheckSpelling

' After user has made changes, get text back...
txtEditM.Text = .Content.Text

' Close the document and exit Word...
.Saved = True
.Close
End With
Set oTmpDoc = Nothing

oWord.Top = lOrigTop
oWord.Quit
Set oWord = Nothing

End Sub</B>

This works great inside the text editing box. How can we make the editor automatically add the /p for carriage returns (enter key) and also filter out (not show) the /p when the editor is open (show a new line instead)?

What are the steps to use the spell check routine from Word to check all text in the drawing?

Thank you for your time it has been a great help,

Scott

#5
Scott,

To make the TextBox start a new line when the Enter key is selected just select the TextBox in the IDE and set the property EnterKeyBehavior to True.

What you will find is the TextBox will start a new line when Enter is pressed however, IntelliCAD will not recognize the hidden symbol that VBA places at the end of the previous line and insert a ? in the Mtext.

I am working on this to parse the text string from the TextBox and change the hidden symbol (by the way it is a Line Feed Carriage Return symbol used by all printers to start a new line) to a \P that is required by ICAD’s Mtext when the Update button is activated.

I will add your spell checker to the MultiText Editor when I sort out the new line problem.



------------------
Regards
John Finlay
Don't want to post a question - email me direct on john@acecad.com.au

#6
I have changed our Multi-Text Editor - CMS Tutorial to Version 2 and altered the code behind our UserForm using V2 in front of the comments.

We can add additional controls to our form to allow the user to alter the MultiText Height and the Text Style used to create the overall MultiText which in turn sets the Font Style.

All that this entails is to add the two frames and three text boxes, one combo box and four labels. Set the back color property of txtSTH and txtFont “Button Light Shadow” and their locked property to True.

The form should now look like this:
Image


When we select a new style we need to inform the user of the font style that is attached and I have added a change event to the combobox that updates the txtFont TextBox.

As previously requested by Scott, we need to adjust the Linefeed and Carriage Return hidden symbols in our edit text box to allow our user to just press Enter.
This became a two way process:

First Way – From selection into our Edit TextBox –

When we select MultiText the string has “\P” in it to represent a new paragraph and we don’t want to see this in our Edit TextBox. I used the VBA Replace method to find all the “\P”’s and replace them with the Linefeed character (ASCII number 10). Our Edit TextBox add the Carriage Return automatically after a Linefeed.

Replace Method Used when the UserForm is initialized:
txtEditM = VBA.Replace(myMText.TextString, "\P", VBA.Chr(10))

Second Way – From our Edit TextBox to the MultiText in IntelliCAD -

We needed to remove both the Linefeed and the Carriage Return hidden symbols and replace these with “\P”. What better way than to use the Replace Method again.

Replace Method Used when the Update button is selected:
myMText.TextString = VBA.Replace(txtEditM, VBA.Chr(13), "\")
myMText.TextString = VBA.Replace(myMText.TextString, VBA.Chr(10), "P")

The first Replace Method replaces the Carriage Return (ASCII number 13) into a “\” and the second replaces the Linefeed.into a “P”.

The user is totally unaware that these events are happening and here is the complete new code that you can copy and paste in the UserForm’s code window:

Code Start Here 

Option Explicit

Private Sub cboStyle_Change()
' V2 Display the Font in the TextBox named txtFont when the cboStyle is changed
txtFont = IntelliCAD.ActiveDocument.TextStyles(cboStyle.Value).FontFile

End Sub

Private Sub cmdCancel_Click()
' When the user selects the Cancel Button the program ends
End
End Sub

Private Sub cmdUpdate_Click()
' When user selects Update Button the Multi-Text is equal to the text in our
' textbox named txtEditM.

' V2 remove the Linefeed and Carrage Return and insert "\P"
myMText.TextString = VBA.Replace(txtEditM, VBA.Chr(13), "\")
myMText.TextString = VBA.Replace(myMText.TextString, VBA.Chr(10), "P")

' V2 Alter the Text Height if a new text height is input into textbox txtNTH
If txtNTH > "" Then myMText.Height = txtNTH


'V2 Alter the Text Style to the selected in cboStyle
myMText.StyleName = cboStyle.Value

myMText.Update
' Unload the user form and end the editing session
Unload frmEditMText


End Sub

Private Sub UserForm_Initialize()
' When the userform is initialized the selected Multi-Text is displayed in the textbox
' named txtEditM

' V2 Replace the "/P" With a Linefeed (NOTE the Text Box automatically adds a Carrage Return)
txtEditM = VBA.Replace(myMText.TextString, "\P", VBA.Chr(10))


' V2 Add Selected Text Length
txtSTH = myMText.Height

' V2 Add Text Styles into Combo Box
Dim myStyle As TextStyle
Dim myStyles As TextStyles
Dim I As Integer

' V2 Loop from 1 to the total number of TextStyles
For I = 1 To IntelliCAD.ActiveDocument.TextStyles.Count
' V2 Add each TextStyle name to the combo box
cboStyle.AddItem IntelliCAD.ActiveDocument.TextStyles.Item(I).Name
' V2 Set the value in the ComboBox to the MultiText TextStyle
cboStyle.Value = myMText.StyleName
' V2 IntelliCAD.ActiveDocument.GetVariable ("TextStyle")
Next I

' V2 Display the active Font in the TextBox named txtFont
txtFont = IntelliCAD.ActiveDocument.TextStyles(cboStyle.Value).FontFile


End Sub

Code Ends Here 

Scott also wants a spell checker added and I will look into a generic one rather than a the Office spell checker because we want to have control over the dictionary.


------------------
Regards
John Finlay
Don't want to post a question - email me direct on john@acecad.com.au
cron