Professional Documents
Culture Documents
Bind Sample
Bind Sample
Bind Sample
In this sample program you will:
Figure 1.1.
What you can
create in this
sample
Figure 1.2.
Opening a new
project
3
Adding Controls
Once the VB Application Wizard is finished, display the toolbox (View->Toolbox).
Right-Click on an open area not occupied by a control and select
Components… from the pop-up menu (Bottom Left). Project->Components
will work as well.
Select the Controls tab if it is not already selected and scroll down to the
bottom. You should see Xtreme PropertyGrid ActiveX Control module
Make sure that the check box is selected and click OK
Figure 1.3.
Adding a new
control
You should now have a new control added to your Toolbox which looks like this:
Add the Microsoft Windows Common Controls 5.0 (SP2) control as in Figure 1.4.
4
Figure 1.4.
Adding a new
control
Several new controls will be added to the toolbox. You will use the TreeView
control in this sample project
Place a PropertyGrid control (blue box in Figure 1.5.) on any blank area on the
main form.
Click on the PropertyGrid control
Go to the properties window
Name it wndPropertyGrid
5
Figure 1.5.
PropertyGrid
controls
Figure 1.6.
Create a
control array
Figure 1.7.
wndTreeView
properties
Each control in the TreeView will have an icon indicating a folder is open or closed. An
arrow icon will point to the current node as in Figure 1.8.
Figure 1.8.
Arrow Icon
Figure 1.9.
Opening
ImageList
properties
There will not be any icons in the Images: List box (red box in Figure1.10.) To add
images:
Click on the Insert Picture… button
Navigate to the C:\Program Files\Codejock
Software\ActiveX\Suite\samples\PropertyGrid\BindSample\Icons
directory.
Insert arrow.bmp first, then folder.bmp, and last openfolder.bmp as in
Figure 1.10.
8
Figure 1.10.
Images Property
Pages
The key and tag properties are not important in this project. Leave them as the
default values.
The index of the image will be used to associate an image to different
TreeView controls.
A MaskColor is needed to mask transparent content of an image.
Each image has a background color of green that needs to be set as the
transparent color
Click on the Color tab
Click on MaskColor (red box in Figure 1.11) in the Properties box
Select Green from the Color Palette (blue box in Figure 1.1.)
Click the Apply button
Click OK to finish
9
Figure 1.11.
Color Property
Pages
Figure 1.12.
General Property
Pages
Available Events
Look at all of the available events the PropertyGrid controls provide. There are two
ways to do this:
#1
Open up the object browser by hitting F2
Select XtremePropertyGrid from the library list drop down menu
Figure 1.13.
Displaying
available events
Select the PropertyGrid 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
Figure 1.14.
Displaying
available events
11
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 wndPropertyGridr option from the left drop down list and browse the right drop
down list for a list of event procedures available to the PropertyGrid control. You might
also take a look at the PropertyGridItems and ItemConstraints objects as you will be using
these as well. It is this second method that we will be using to add code to the different
PropertyGrid event Procedures.
The property grids will be bound to variables, which allow the property grids to store data in
variables.
The integer variable will correspond with the index number of a property
grid in the PropertyGrid control array. Declare the integer variable:
Dim nCurrentGrid As Integer
Each variable needs to be initialized so the property grid has some data to display
when the application first opens.
Before the property grids are created, initialize the variables in the Form_Load event
procedure.
Assign the values as in the following:
nCurrentGrid = 0
13
AssembleName = "WindowsApplication1"
OutputType = "Windows Application"
Namespace = "WindowsApplication1"
ApplicationIcon = "App.ico"
ProjectFile = "WindowsApplication1.csproj"
ProjectFolder = "C:\Documents and Settings\User\My Documents\Visual Studio Projects\" _
+ "WindowsApplication1\"
OutputFile = "WindowsApplication1.exe"
ConditionalCompilationConstants = "DEBUG;TRACE"
OptimizeCode = False
CheckforArithmeticOverflow = False
Alowunsafecodeblocks = False
WarningLevel = "Warning level 4"
TreatWarningsAsErrors = False
InitTreeView
InitGeneralProperties
InitDesignerProperties
InitBuildProperties
End Sub
The InitTreeView subroutine will create all of the nodes in the TreeView control. Create the
InitTreeView subroutine
Click somewhere on your code view window
Click Tools>Add Procedure... from the popup menu.
14
Figure 1.15.
Add Procedure
NodeCommon.Expanded = True
NodeConfiguration.Expanded = True
End Sub
Create a node
Add it to the TreeView control’s node collection
Assign a pointer to it called NodeCommon
15
NodeCommon.ExpandedImage = 3
Now the image next to the text will change indicating whether the node is
expanded.
There will be three grids to display
There must be a way to identify which grid needs to be displayed when a
node is clicked. This is done with the Tag property.
NodeCommon.Tag = 0
The Tag returns or sets an expression that stores any extra data needed for
your program.
The index of the appropriate grid will be assigned to each node.
Code will be added to retrieve the node Tag each time a node is clicked and
that value will be used to determine which grid is displayed.
16
This will create two child nodes to the “Common Properties” node.
The “General” node is assigned a Tag value of zero and the “Designer”
node is assigned a Tag value of one.
By default, the top level nodes are not expanded, leaving the child node
hidden from view.
To expand the top level node “Common Properties,” modify the Expanded
property, setting it to True.
NodeCommon.Expanded = True
So far a tree with a top level node called “Common Properties” was created and has
two child nodes called “General” and “Designer” as in Figure 1.16.
Figure 1.16.
Common
Properties node
NodeConfiguration.Expanded = True
Figure 1.17.
Configuration
Properties node
Category.Expanded = True
ItemAssemble.Selected = True
Category.Expanded = True
End Sub
Add a category named “Application” to the first property grid in the control
array.
1. Use the wndPropertyGrid(0).AddCategory(“Application”) method,
using zero to indicate the first property grid and passing in a string
value of “Application”
2. Set Category as a pointer to this property grid category.
Set Category = wndPropertyGrid(0).AddCategory("Application")
It was previously explained what String Notation is and how to use it.
The variable name in String Notation is automatically set as the Caption of the
PropertyGridItem.
If the Caption property of the PropertyGridItem is not set, the default String
Notation is used.
When adding the child property item bound to the variable “AssembleName”, set a
pointer called ItemAssemble to the property item.
This pointer will be used to change the Caption and make this property item
“Selected” at startup.
To override the default caption, set the Caption property of the
PropertyGridItem to the desired String value.
Now change the Caption of the “AssembleName” property item as below
ItemAssemble.Caption = "Name of Application"
19
Changing the Caption will not change the value of the variable.
The next property item will have several preset values the user can choose from.
The values will be displayed in a drop-down.
Use the AddChildBinded method to add an item with property type
PropertyItemString
Set the handler to Me
Bind to the variable “OutputType”
Set a pointer to the property item called Item as in the following:
Set Item = Category.AddChildItemBinded(PropertyItemString, Me, "OutputType")
To create a list of items, use the Item pointer to add Constraints to the
property.
The “OutputType” property item will have three items to choose from in the
drop-down list.
Use the Item.Constraints.Add method to add a constraint.
The Constraints.Add method is only one parameter of type string.
Add three constraints to the “OutputType” property as below.
Item.Constraints.Add "Windows Application"
Item.Constraints.Add "Console Application"
Item.Constraints.Add "Class Library"
By default, the property item value can be edited by the user. To keep the user from
changing the value of the items in the constraint list, set the Item.ConstraintEdit
property to True
Item.ConstraintEdit = True
This will keep users from changing the text of the items in the constraint list.
It also keeps them from highlighting the text or placing the cursor anywhere
in the field.
To display the list of values in a drop-down list, set the Item.Flags property
to ItemHasComboBox.
To allow users to highlight the text and place the cursor on the text, use the
ItemHasEdit flag. The ItemHasEdit flag has the opposite effect as the
ConstraintEdit property.
Item.Flags = ItemHasEdit + ItemHasComboButton
20
The ItemHasEdit flag allows the user to enter a value not in the list as well as
change the values. However, the ConstraintEdit property will not allow the
text of the items in the constraint list to be changed when set to True, you
will only be able to select from one of the items in the constraint list.
Then the Caption of the “OutputType” property item is changed to “Type of
Output”
Item.Caption = "Type of Output"
After adding the “Namespace” property item, the Caption is set using the
PropertyGrid methods as below
Add the two remaining property items to the “Application” category as below:
To have the “Application” category expanded, displaying all of its properties, set the
Category.Expanded property equal to True
Category.Expanded = True
When the application starts, the “Assemble Name” property should be selected
To do this, use the ItemAssemble pointer to set the Selected property equal
to True
ItemAssemble.Selected = True
Each property will be read-only, so the user can only see the value and it will
appear grayed out.
To make an item read only: Use the Item pointer setting the ReadOnly
property to True.
Add the code below to add the “Project” category following the same steps
used to create the “Application” category.
Set Category = wndPropertyGrid(0).AddCategory("Project")
Category.Expanded = True
Category.Expanded = True
Item.Selected = True
Category.Expanded = True
22
End Sub
Make sure that a 1 is used as the property grid index since these categories and items
will be added to the second grid (index started at zero)
Set Category = wndPropertyGrid(1).AddCategory("Web Designers")
The only other difference is that the “Page Layout” values can be edited by setting
the ConstraintEdit property equal to False
Adding categories and items to the second grid is exactly the same as before.
Code the subroutine as in the following:
Category.Expanded = True
Item.Selected = True
Category.Expanded = True
End Sub
Make sure that a 2 is used as the property grid index since these categories and items
will be added to the second grid (index started at zero)
In this property grid there are some Boolean items which will display True or False
as its value
Category.AddChildItemBinded PropertyItemBool, Me, "Optimize Code"
Only one of the three grids can be visible at the same time. A subroutine will be added to
determine and set which grid is visible.
23
nCurrentGrid = nGrid
End If
End Sub
An integer called nGrid is passed, which is the index of the property grid that
needs to be displayed.
The integer nGrid is compared to the current grid index called nCurrentGrid
If the two values are not equal, then a new grid with index nGrid is to be
displayed.
When a new grid is to be displayed, the current grid with an index of
nCurrentGrid is hidden and the grid with and index of nGrid is set to visible
nCurrentGrid is then updated with the value in nGrid.
When a node is clicked in the wndTreeView control, the corresponding property grid is
displayed. To determine when a node has been clicked:
Edit the wndTreeView_NodeClick event procedure
This event procedure is passed the node that was clicked as the only
parameter.
Code the wndTreeView_NodeClick event procedure as below:
SwithPropertyGrid (Node.Tag)
If (Not Node.Parent Is Nothing) Then Node.Parent.Tag = nCurrentGrid
Each node was previously assigned a Tag value in step that corresponded
with one of the three property grids.
Whenever a child node is clicked the Tag value of the parent is updated to
the child’s Tag value.
If (Not Node.Parent Is Nothing) Then Node.Parent.Tag = nCurrentGrid
This is done to keep the current grid displayed even when the user clicks on
the parent node (nodes with folder icons).
The arrow icon must point to the current node.
1. Look at all of the child nodes, checking the Tag value to determine
which one is active.
2. The nodes image is updated to the index of the arrow image in the
imlTreeIcons ImageList if the node is active (corresponding grid is
displayed).
3. If not, then no image is displayed.
For Each Node In wndTreeView.Nodes
If Node.Child Is Nothing Then Node.Image = IIf(Node.Tag = nCurrentGrid, 1, 0)
Next
For debugging purposes, the values of all the variables are printed in the debug window
when the application calls its Unload event procedure.
Add the following code to the Form_Unload event procedure
Me.SetFocus
End Sub
25
Both the OK and Cancel command buttons will call the forms Unload event procedure.
Add the following code to the OK and Cancel Click event procedures
This will allow you to see how each variable was affected.
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.