MDISample
MDISample
MDI Sample
In this sample program you will:
Learn how to create a menu and two command bars, a standard command bar and a
command bar, that will control the visual theme, via code
Learn how to create a pop-up menu when you right-click on a child window
Learn how to create tabbed workspace that works just like the well known Mozilla tabbed
browsing. Each individual tab will respond to a right mouse click event by displaying a
popup menu
Learn how to create key bindings/accelerators via code
It is recommended that you complete either the Designer or Customizable tutorials before
completing this tutorial, as they contain many of the same techniques used here.
The Command Bar used in this application can be done using the Xtreme
Commandbar designer for those who do not want to code; refer to the
Designer tutorial.
2
Creating MDI
Figure 4.1.
What you can
create in this
sample
In the previous step, Visual Basic created an Image List that contains all of the toolbar icons.
It also created fully functional event procedures for most of the menu commands. To create
your own menu and toolbar via code, as done in this tutorial, you will need to get rid of the
ones Visual Basic created. You will learn how to do this later in the tutorial.
this:
Figure 4.2.
Adding a new
control
Once you have the CommandBars component (circled in yellow in Figure 4.3.)
added to your Toolbox, click on the component and place it on any blank area on
your application’s main form.
4
Figure 4.3.
CommandBars
component
added
- ImageList control
- CommonDialog control
You are doing this because you will be adding your own custom menu and toolbar via code
and you don’t want the menu Visual Basic created. By allowing Visual Basic to create the
5
menu, you can now use all of the menu event procedures that Visual basic created for your
custom menu and command bar.
In the Designer tutorial, we created a resource file. We still need to have a .bas resource
file with all of our menu item IDs, but this time we have to create it ourselves. We must
also assign an ID and Index number for each of the buttons on the command bar and
menu.
To create a new module:
Make sure you are displaying the project explorer
Right-click on the Modules folder
Select Add from the pop-up menu
Select Module from the second pop-up menu.
Figure 4.4.
Display project
explorer
The name can be anything you want, but it is recommended that you follow a
naming scheme. For example, for the New File menu selection, declare it like this:
Public Const ID_NEW_FILE
Start with the prefix ID, followed by the name of the menu item - all
separated by underscores.
Assign the menu item ID an index number. The number can be any number
you want in the range of 100-5700, as long as the menu item does not also
have a corresponding toolbar icon. Add the control item declarations in the
diagram below to the new module.
You must careful here: If the control/menu item will have a toolbar button
and a corresponding icon, you have to make sure that Visual Basic did not
assign a different index to the corresponding picture in the ImageList.
Visual Basic associates a control/menu item to an icon in the ImageList
control via a tag (the index we assigned to the menu IDs). To find out what
tag number was assigned to the icons:
Figure 4.5.
Properties
4. From the properties page dialog, select the Images tab. You will see
all of your toolbar icons.
8
Figure 4.6.
Property
Pages
Notice the Tag field. This is the number that is associated with your menu
IDs. You can change this to whatever you like or keep it as it is (Visual Basic
might not have assigned a value). These are the values that you will use in the
new module we just created. In our example, the print icon has a Tag value
of 113, so in General section of your code you would enter the definition:
Const ID_FILE_PRINT = 113.
You can change the 113 to whatever you want; just make sure that all the
tags are unique and that the Tag value corresponds with the definition you
created in the General code section.
You could design the menu and toolbar exactly how you want it in the
Xtreme Command Bar Designer and generate the resource file like we
did in the Designer tutorial. However, you will have to open the
ImageList properties and assign the appropriate Tag values that the
Xtreme Designer generated. You might do this because you don’t want
to have the external .xcb file for your command bar. The advantage
here would be that you get to see what your menu/toolbar will look
like, and you can generate the resource file that already has appropriate
IDs and Indexes. Just remember to change the Tag values in the
ImageList control.
9
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 4.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.
#2
Go to your code view by hitting F7
10
Figure 4.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.
In our example, you will notice that the child window starts out maximized. To do this:
Select the frmDocument (Child window)
Click anywhere on the form
Go to the properties box
Change the Window State to maximized.
Figure 4.9.
Maximizing the
child window
Figure 4.10.
One tabbed
workspace
Now that the Workspace object is created, you can start to edit the
Workspace event procedures.
In the RClick event procedure, a context popup menu with two controls will
be diaplayed.
Add the code below to the Workspace_RClick event procedure:
Private Sub Workspace_RClick(ByVal Item As XtremeCommandBars.ITabControlItem)
If Not Item Is Nothing Then
Debug.Print Item.Caption
Item.Selected = True
Workspace.Refresh
With Popup.Controls
.Add xtpControlButton, ID_FILE_NEW, "&New", -1, False
.Add xtpControlButton, ID_FILE_CLOSE, "Close", -1, False
End With
Popup.ShowPopup
Workspace.Refresh
End If
End Sub
Figure 4.11.
TabWorkspace
Figure 4.12.
Documents
With Popup.Controls
.Add xtpControlButton, ID_FILE_NEW, "&New", -1, False
.Add xtpControlButton, ID_FILE_CLOSE, "Close", -1, False
End With
Once the Popup command bar is created, it has to be displayed. This is done
using the ShowPopup method.
Popup.ShowPopup
Before you begin creating the command bar, it will be helpful to examine the process of
creating the menu:
Begin by declaring an object of type CommandBarControl (This represents the base
class of a command bar control). This object will be used for the entire menu
creation process. The object is used to modify the properties of command bar
controls with the keyword Set.
A typical ActiveMenuBar would contain popup controls such as File, Edit, View,
and Help.
This code creates a popup control named ControlFile with the caption File and
places it into the ActiveMenuBar.
New controls can now be added to the ControlFile popup control located in the
ActiveMenuBar’s collection of controls. To do this, use the keyword With to state
that all the below method calls will be used for ControlFile.CommandBar.Controls.
With ControlFile.CommandBar.Controls
The Add method adds controls to the File control located in the ActiveMenuBar.
Using the keyword With, all below controls will be added to Files control. In the
example, controls of XTPControlType xtpControlButton are added to the Files
16
control, these are the submenu controls in the popup File menu. The following code
adds the Save control button to the File popup button on the ActiveMenuBar.
In the process of creating the ActiveMenuBar, control popups are added to the
ActiveMenuBar, and then controls are added to the control popup in the ActiveMenuBar.
The process is repeated until the ActiveMenuBar is complete.
You will start with the File ActiveMenuBar control as in the following code. This code
should be the first thing after the Visual Basic generated window sizing code. Here, you will
need to declare an object as type CommandBarControl
In our example, we call it Control as below.
Next you must create the &File control popup in the ActiveMenuBar by using the
Set command with CommandBars.ActiveMenuBar.Controls.Add(Type As
XTPControlType, Id As Long, Caption As String, Before As Variant, Temporary
As Variant), which takes five parameters:
The first parameter specifies the type of control which is xtpControlPopup
Next is the control (menu item) ID that you specified in the General code
section. For the main menu headings (i.e. File, Edit, View, Help) this value
will always be zero.
Next is the caption that will be displayed.
Then we have two optional fields:
17
In the example the control popup is named ControlFile. Now that the &File
control button is created in the ActiveMenuBar, submenu controls can be added.
The previous Set statement created an xtpControlPopup with caption File in the
ActiveMenuBar.
The File control will be used with a With/End With Structure where submenu
controls will be added to the File control. The With/End With structure will be
primed with ControlFile.CommandBar.Controls.
Within the structure code an .add statement for each submenu control. Be sure to
put them in the correct order starting with the top menu item and moving down (or
change the index of the control).
Repeat the previous steps (starting on page 14) for the &Edit control popup in the
ActiveMenubar as below:
Under the menu bar selection &View, you will notice that there is a popup menu for toolbar
selection. To accomplish this:
Set the XTPControlType to xtpControlPopup for the submenu control that will
have the popup menu. The &Toolbars submenu control will have the popup.
Set a pointer to the submenu control when you create it with the command Set
Control = .add (xtpControlPopup, 0, “&Toolbars”, -1, False) as in the code
shown.
With the pointer set to the &Toolbars submenu control, you can add the popup
menu entries:
This is the same .Add method you used earlier so follow the same syntax as
before.
When you are done adding popup menu controls, use the .Add method as
you did when creating the &File and &Edit submenu controls.
Create the &Window and &Help control popups in the ActiveMenuBar as below:
Refer to the previous steps (beginning on page 14) if you need help.
The only thing to note here is the last .Add call in the “&Window” submenu
creation code. Here, we are creating the window list that will hold the names of all
the open documents in the application. The ID, 35000 passed is a special Id used for
Window Lists and cannot be changed.
With the toolbar/commandbar complete you must load the images from the
ImageList control. To do this, add the code below.
CommandBars.AddImageList imlToolbarIcons
This method takes the name of your ImageList control as its only parameter.
Now you need to create the Themes toolbar. This is done exactly the same way you created
the standard toolbar previously. Refer to the code below.
To illustrate how to change the properties of a control, we are going to change the
XTPControlType of the ThemesBar controls. Because the theme buttons do not have an
icon, their style should be xtpButtonCaption, which displays only text. We are looping
through all of the ThemesBar buttons and changing their styles. This technique can be used
on all of the menu and toolbar controls. You can change properties such as caption, type,
and style.
Next, add key bindings to the controls as below (Appears next to the menu caption on the
menubar).
You will need to add the constants to resource file using the predefined values in the
diagram below. This will complete your menu and commandbar/toolbar.
You can also disable the expand button feature at the end of your toolbar that allows you to
add or remove buttons
Case ID_FORMAT_BOLD:
ActiveForm.rtfText.SelBold = Not ActiveForm.rtfText.SelBold
Case ID_FORMAT_ITALIC:
ActiveForm.rtfText.SelItalic = Not ActiveForm.rtfText.SelItalic
Case ID_FORMAT_UNDERLINE:
ActiveForm.rtfText.SelUnderline = Not ActiveForm.rtfText.SelUnderline
Case ID_EDIT_CUT:
Clipboard.SetText ActiveForm.rtfText.SelRTF
ActiveForm.rtfText.SelText = vbNullString
Case ID_EDIT_COPY: Clipboard.SetText ActiveForm.rtfText.SelRTF
Case ID_EDIT_PASTE: ActiveForm.rtfText.SelRTF = Clipboard.GetText
In this event procedure we will set up a switch statement that will check to see which
command bar/ menu item was selected. The switch statement will accept and compare the
Command Bar IDs. We will be using the IDs that we defined in our resource file
You will want to create an entry in your switch statement for each Command
Button/menubar control ID in the resource file
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 up a save file dialog
when the save button is clicked.
You will want to call the methods that Visual Basic created for most of the standard
functions like new file, save, print, and close.
In this example:
The CommandBars_Execute event procedure is passed the
ICommandBarControl 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 we can get from Control, but we are only
interested in the command ID in our example.
23
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
child windows would display over it.
Add the code below to the GetClientBordersWidth event procedure.
End Sub
Now we must address 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.
Edit the CommandBars_Update event procedure. This event procedure is passed
the same object as the Execute event procedure.
24
It should be set up exactly the same as the Execute event procedure as in the
following:
Case ID_THEME_OFFICE:
Control.Checked = CommandBars.VisualTheme = xtpThemeOfficeXP
Case ID_THEME_DEFAULT:
Control.Checked = CommandBars.VisualTheme = xtpThemeOffice2000
Case ID_THEME_OFFICE2003:
Control.Checked = CommandBars.VisualTheme = xtpThemeOffice2003
Case ID_THEME_NATIVEXP:
Control.Checked = CommandBars.VisualTheme = xtpThemeNativeWinXP
Case ID_FORMAT_BOLD:
Control.Enabled = Not ActiveForm Is Nothing
If Not ActiveForm Is Nothing Then Control.Checked = ActiveForm.rtfText.SelBold
Case ID_FORMAT_ITALIC:
Control.Enabled = Not ActiveForm Is Nothing
If Not ActiveForm Is Nothing Then Control.Checked = ActiveForm.rtfText.SelItalic
Case ID_FORMAT_UNDERLINE:
Control.Enabled = Not ActiveForm Is Nothing
If Not ActiveForm Is Nothing Then Control.Checked = ActiveForm.rtfText.SelUnderline
Case ID_FORMAT_ALIGNLEFT:
Control.Enabled = Not ActiveForm Is Nothing
If Not ActiveForm Is Nothing Then Control.Checked = _
ActiveForm.rtfText.SelAlignment = rtfLeft
Case ID_FORMAT_CENTER:
Control.Enabled = Not ActiveForm Is Nothing
If Not ActiveForm Is Nothing Then Control.Checked = _
ActiveForm.rtfText.SelAlignment = rtfCenter
Case ID_FORMAT_ALIGNRIGHT:
Control.Enabled = Not ActiveForm Is Nothing
If Not ActiveForm Is Nothing Then Control.Checked = _
ActiveForm.rtfText.SelAlignment = rtfRight
End Select
End Sub
25
You will have a Select structure that will do a switch on the ID of the command
button/menu item 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 Paste command button/menu
items based on whether the user has some text in the combobox selected (Which is
really a rich text box inside a window, thus the rtf method calls).
Then we want to enable the Paste command button/menu item if there is something
on the clipboard to paste
Finally, we will check to see if the user has hidden the status bar, standard toolbar, or
extended toolbar.
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 checked/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).
Now you will want to ensure that your rich text box is the same size as your
document window. To do this, change your rich text box’s border size as below.
With that out of the way, you can move on to code the pop-up menu for the
documents window.
26
Here you want to show a pop-up menu when the user right-clicks anywhere on the open
document. To do this, you will need to edit the rtfText_MouseUp event procedure as in the
following code:
If (Button = 2) Then
ZOrder
Popup.ShowPopup
End If
End Sub
Make sure that it was the right mouse button (Button = 2) that was clicked.
The ZOrder command is used to bring the document that requested the popup to
the front of the GUI interface (I.e. You have several documents open within your
application and they are not maximized, you can right-click a document in the
background and it will be placed at the front of the GUI.).
Create a pop-up menu just like you did when creating the &Toolbars submenu
popup.
Start off by adding the popup menu specifying the XTPControlType as
xtpBarPopup and set a pointer to it called Popup.
When creating the CommandBar/menu, notice the CommandBars control on the
main frame is being used.
The other notable difference is that you are calling the CommandBars.Add method,
which takes only two parameters: the name of the command bar and the
XTPControlType, which needs to be xtpBarPopup.
Notice that this was done the same way when creating the popup in the
ActiveMenuBar, only you were using a different .add method.
Create the With/End With structure and prime it with Popup.Controls.
27
Add the menu items just as you did on pages starting on page 14.
Now all you have to do is call the ShowPopup method to show your popup menu.
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.