Designer Sample
Designer Sample
The Designer
In this sample program you will:
Learn how to create an application that loads the default designer command bar
Learn how to add functionality to the command bar via event procedures
Learn how to add the CommandBars ActiveX control module to your application
You can add more toolbars and controls to this default command bar via code. You will learn how
to create command bars and controls via code in another tutorial.
2
Figure 2.1.
What you can
create with the
Designer
Start Visual Basic and select new project from the file menu. Select Standard EXE
from the new project dialog box and click OK.
Figure 2.2.
Opening a new
project
3
this:
Figure 2.3.
Adding a new
control
Now you must add the Rich Textbox Control IF it is not already in your toolbox.
Follow the steps outlined previously to add a new control made in the previous
directions (page 3), this time adding the Microsoft Rich Textbox Control 6.0. You
should have a new control in your toolbox:
4
Figure 2.4.
Adding the
Rich Text Box
Control
Add the Microsoft Windows Common Controls 5.0 (SP4) and Microsoft Windows
Common Controls-3 6.0 (SP5) components.
Figure 2.5.
Adding common
controls
You should now have several new controls added to your toolbox; you will
be using the Status Bar control in this project.
5
Figure 2.6.
Adding
controls to the
form
You are now ready to start adding code for the CommandBars component. To load
the default designer command bar, add the code CommandBars.LoadDesignerBars
to the Form_Load event procedure:
CommandBars.LoadDesignerBars
CommandBars.LoadCommandBars "Codejock Software ActiveX Demos", App.Title, "Layout"
End Sub
6
This method takes three string parameters, which are the Registry key, Application Name,
and the Section (folder) in which the settings will be saved. This completes the Form_Load
event procedure.
You should now be able to compile your application and see your new menu and
command bar. However, it will not have any functionality.
Run your application to make sure everything is in order. If not, go back through the
previous steps until you can run your application and see your command bar and
menu.
Make sure you save the users command bar settings. Because users will be able to
customize the command bar (i.e. Add and remove buttons) you need a way to save
their settings for the next time they open the application. To do this, your settings
are saved to the systems registry. You do this with the
CommandBars.SaveCommandBars method.
End Sub
This method takes three string parameters, which are the Registry key, Application
Name, and the Section (folder) in which the settings will be saved. The following line
of code will save your application’s command bar settings in the system registry
under HKEY_CURRENT_USER\Software\Codejock Software ActiveX
Demos\DesignerSample. This should be the first line of code in your main
application’s Form_Unload event procedure:
7
You should now be able to save and load your command bar settings. Try this now
to make sure everything is in order. If not, repeat all previous steps to figure out
what went wrong.
Available Events
Look at all of the available events the Commandbars control provides. There are
two ways to do this:
#1
Open up the object browser by hitting F2
Select XtremeCommandBars from the library list drop down menu
Figure 2.7.
Displaying
available
events
Select the CommandBars entry in the list box. Now you can see all of the
available members, methods, events, properties, functions, and subs. If you
click on a member, you will get detailed information about that member.
Here we are interested in the events available that have the lightning bolt
icon next to them.
8
#2
Go to your code view by hitting F7
Figure 2.8.
Displaying
available
events
The left drop-down is a list of your controls; the right drop-down is a list of
event procedures available for those controls.
Select the CommandBars option from the left drop down list and browse
the right drop down list for a list of event procedures available to the
CommandBars control. It is this second method that we will be using to add
code to the CommandBars different event Procedures.
These values are all predefined and cannot be changed. You will learn how to change
IDs when you create your own custom command bar.
9
Figure 2.9.
Display project
explorer
From the open dialog, click the Open button to open the new module in code view
Type in the control IDs and index numbers listed in the first diagram in this step or
cut and paste from the module included with this sample.
You have now added your resource file and can begin associating your command bar
controls with their IDs and Index number.
Now you are ready to start adding our CommandBars event procedures.
Start by adding the Execute event procedure:
Go into your code view by hitting F7
Select CommandBars from the first drop-down list
Select Execute from the second drop-down list
Case ID_VIEW_STATUS_BAR:
sbStatusBar.Visible = Not sbStatusBar.Visible
CommandBars.RecalcLayout
Case ID_EDIT_CUT:
Clipboard.SetText rtfText.SelRTF
rtfText.SelText = vbNullString
Case ID_EDIT_COPY: Clipboard.SetText rtfText.SelRTF
Case ID_EDIT_PASTE: rtfText.SelRTF = Clipboard.GetText
End Select
End Sub
In this event procedure, you will set up a Select statement that will check to see which
command bar/ menu item was selected. The switch statement will accept and compare the
Command Bar IDs. You will be using the IDs that were defined in the .bas module.
Create an entry in your switch statement for each ID you defined in the .bas resource
module.
For each Case statement, you can do anything you want: like call a function or
display a message box. For example, you might want to open a save file dialog when
the save button is clicked:
Display a message box stating that the toolbar button was clicked when the
New File, Open, Close, and Save controls are clicked.
The CommandBars_Execute event procedure is passed the CommandBarControl
as a parameter. As you can see in the event procedure header, the variable is called
Control and is of type XtremeCommandBars.ICommandBarControl.
There are several properties you can get from Control, but you are only interested in
the command ID for this example. If you would like to know all the available
11
We need to use some code to take into account the size of the status bar when drawing the
window. The CommandBars_GetClientBordersWidth event procedure is used when the
area occupied by controls docked along the edges of the form (like a ToolBar or StatusBar)
has to be retrieved. This event procedure will be called automatically so you will not directly
call it in your code. Without this procedure, you would not see your status bar because the
Rich TextBox would display over it.
Add the code in the diagram below to the GetClientBordersWidth event procedure.
Make sure that your rich text box is the same size as the open document window not
occupied by controls. This means the rich text box will fill the entire form area not occupied
by the status bar, toolbars, or menubar.
Add the following code to the CommandBars_Resize event procedure
End Sub
If you need help with the rich text box and its methods, it is well
documented in the Microsoft help documentation.
To determine the right margin of the rich text box, you need to make a
decision based on whether or not the width is greater than 400.
The Immediate-If function (IIF) is used to evaluate the width of the rich
textbox. The IIF function takes three parameters: the first is the expression
to evaluate, the next is what should be done if the expression is true, and the
last happens if the expression is false
Check the Microsoft help files for more help using the IIf function.
13
Now we must take care of what happens when we have command buttons or menu
selections that are displayed differently based on a value, or are enabled/disabled based on
some condition. For example, you might want to enable/disable certain command buttons/
menu selections based on events that take place in your application:
You will have a Select structure that will do a switch on the ID of the command
button/menu control selected. The big difference here is that we only include the
control IDs that need to be updated. In our example, we are enabling/disabling our
Cut and Copy command button/menu controls based on whether the user has some
text in the rich textbox selected. When we update the checkmarks next to the
controls that can have checkmarks next to them, we will check if the user has hidden
the status bar, standard toolbar, or themes toolbar. Finally, we set the properties if
the rich text box based on which of the rich textbox formatting and editing controls
were clicked. This is the place to add any other code that will need to be executed
when you need to check if a menu item is check/not checked
This code only places/removes the check mark next to the item in the
menu; the code to actually hide/unhide the status bar should go in the
Execute event procedure
or any other action that should be performed when a command button/menu item
needs to look different based on specific conditions but has not been clicked (i.e.
hiding check marks next to menu items.).
You have now completed this tutorial! If you are unclear on anything covered, you might
want to go back and complete this tutorial again. It might be a good idea to add some
additional functionality to this sample application to make sure you fully understand what is
going on.