It is possible using the Visual Basic programming language to create your own dialog boxes.
This is the term used for dialog boxes.
Most user interfaces use dialog boxes and most dialog boxes are modal (vbmodal) which means they must be dismissed from the screen before you can continue with the underlying application.
It is relatively simple to create custom dialog boxes for your applications.
Custom dialog boxes can be used to request specific information, get a user's options or preferences.
Custom dialog boxes are created using the UserForms
A Project can contain any number of userforms and each userform represents an individual dialog box.
Properties and Methods
Show method - Display a userform, create a procedure that uses the show method of the userform object (ie frmCustomDialogBox.Show). This procedure must be in a standard code module.
Hide method - Hides the userform although it is still loaded in memory so you can still access all the control values.
Repaint method - Refreshes your userform by painting it again. Useful for updating status. If you display a userform on top of another userform, then it is worth re-painting the userform underneath when the top one is dismissed.
Load statement - Loads your userform into memory however it will not be displayed until you use the Show method. For really complicated userforms you may want to load the userform prior to displaying it so it appears to load quicker.
Unload statement - Unloads your userform and resets all the controls back to their original / default values. A userform is automatically unloaded when the user clicks the close button (top right corner). There is no event-handler for this close button so if pressed any clean-up instructions that you may have included when the Cancel button is pressed are ignored.
It is much better to put any clean up instructions into the Userform_Terminate event. This way they will be executed even when the close button in the top left corner is pressed
All users of an application will appreciate an intuitive and well presented user interface.
When creating your customised userforms make sure you are consistent with fonts, sizes, formatting and colours.
Try not to over-complicate userforms or attempt to display too much information.
You can use the Properties window to change the properties ??
For simple user interaction you should use the MsgBox() function, InputBox() function
refer to specific Excel and Word dialog boxes
Use the Object Browser to obtain a list of all the constants needed to each dialog box
Be aware that the Dialogs method will not work if the context is not appropriate. You cannot display a Pivot Table dialog box when you are not in a Pivot Table ??
Get a full list and add to excel and word pages !!
While you are designing a form:
Each form window has a Maximize, Minimize, and Close button.
You can view the form grid and determine the size of the gridlines from the General tab of the Options dialog box.
Use the buttons in the Toolbox to draw controls on the form. You can set your controls to align with the grid of your form from the General tab of the Options dialog box.
Hide the form when you actually press Cancel
Run any cleanup code
Unload the form after the cleanup code has run
Display examples of the different appearances - flat, raised, sunken, carried border, ridge ??
A clear intuitive interface removes the need for special training
A well organised dialog box with controls in a logical order
Add tooltips to help explain the functionality
It is even possible to create custom dialog boxes that resemble the built-in dialog boxes such as (File > Open).
Include a status bar to keep the user informed
Add a list of all the default methods for the controls ??
Ensure that when users enter incorrect information into a dialog box an error message appears and the focus is set to the control with incorrect information.
It is good practice to always have your "Userform_Initialise" procedure at the very top of your userform code module. This always runs before the userform is displayed and makes sense for it to be at the top.
It is good practice to refer to the userform when you are in the userform code module using the keyword "Me". This can be used to refer to the userform rather than explicitly using the userform name.
frmUserInput.lstName.List = vaNames
Me.lstNameList = vaNames
It is possible to display a userform in an exact position of the screen. You can use the Userforms Left and Top properties. In order for this to be effective you must set the StartUpPosition property to 0.
Adding controls to a userform is no good unless you add code to the event handlers
.frm - when exported
There is a big difference between Hiding a userform and Unloading it.
If you Hide a userform then it is invisible but still remains in memory. Hiding a userform can be useful if it takes while to initialise all the controls and it is likely that the userform will be displayed again, then it will be quicker to hide and then show the userform, rather than unloading and loading again.
If you want to close your dialog box when the user clicks Esc but do not want to display a small command button then add a command button but just change the visible property to false.
To add shortcut keys to activate and select individual controls use the Accelerator property to assign your hot keys.
It is possible to assign a single macro to be executed when any one of a large number of command buttons are pressed. This can be done using a class module.
If you have several text boxes or controls that will not be used by most but you want to keep the controls in a logical order, you can just change the tab order so these are last in the list.
It is possible to display a shortcut menu when you right click on a userform using the MouseMove event.
When you are using MsgBox to ask a question or confirm actions, use an integer variable and compare the value to the built in constants vbYes and vbNo.
Office 2000 supports modeless userforms.
You can use the PaintPicture method to tile a bitmap across a userform.
If you get an "Invalid Forward Reference" error then it may be because you are trying to combine standard form methods with VBE methods. Example is trying to display a userform.
The widest a userform can be is 12287.25 points.
You can create a Userform containing the controls you use most often. Select (File > Export) to save the userform.
When you want to add a new form to another project select (File > Import Files)
You cannot remove the title from the dialog box although you can have a blank caption.
Understanding the Controls collection
Dim aTextBoxes() As TextBoxes
Dim ctlControl As Control
Dim icount As Integer
For Each ctlControl In Userform1.Controls
If TypeName(ctlControl) = "TextBox" Then
icount = icount + 1
ReDim Preserve aTextBoxes(1 To icount)
Set aTextBoxes(icount) = ctlControl
Displaying a modeless dialog box
Manipulating the Userform with a variable - WORD ??
Set vbcFormName = ThisDocument.VBProject.VBComponents(sFormName)
Prefix any reference to controls with Me to make it explicit.
All userforms and messages should have a distinctive title so they can be easily identified by the user and when debugging.
Always put dialog boxes in the middle of the screen (even when multiple screens) save in registry - problems when monitors are moved
All userforms should have their position saved in the registry to allow for multiple screens.
Userforms should load quickly
Prompt the user (to save) if there have been any changes to any information
Cancel, Escape and X should all call the btnCancel_Click subroutine.
Have consistent Cancel (or Close) buttons throughout your application.
Userform - Controls
All userform controls should be prefixed with a consistent three character prefix (eg cmb for command button).
All controls should be renamed even for the most simple userforms.
Labels that are used as static text do not have to be renamed assuming they are not referenced anywhere in the code.
You can always create a control array containing your controls LINK ????
Rename all the controls before writing any code. Code is linked to a control by its name so if its name changes you will have an orphaned event handler.
Control toolbox - buttons - if the project doesn't compile then the Edit button does not appear on the Assign Macro dialog box - it is displayed as New
DateTime Control - always use custom date format "dd mmmm yyyy dd"
If you are using a checkbox to show/hide additional information, ticked should mean visible and unticked should mean hidden.
Any data that a user enters in a userform is lost when the userform is closed
A custom dialog box is created on a Userform
There is a big difference between hiding a form and unloading it. A hidden one is kept in memory.
You should always let your Dialog Boxes be dismissed with the Esc key
© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopNext