ToolBar - Usage tutorial
Last modified: March 6, 2014
ToolBar is a control which allows you to create a pane with buttons in a row or in a column. Each button is a tool in ToolBar’s context which you can click and select it. The tools are oriented either horizontally or vertically. The amount of tools per ToolBar’s client area is given by the developer. The space for each tool is then divided evenly throughout its client area.
For example, if ToolBar’s size is (width = 200, height = 30), ToolsPerPage = 5 and Orientation = Horizontal, then ToolBar will create space for 5 horizontally oriented tools, each of size (width = 40, height = 30).
If there are more tools in the ToolCollection that fit on ToolBar’s client area, the user can flick the finger across it to bring-in another page of tools.
Description of some properties:
Property Name | Description | |
---|---|---|
Tools | Gets the ToolCollection which stores the Tools for ToolBar. | |
Images | Gets the TImageCollection which stores TImage objects for ToolBar. TImage represents an image with alpha channel transparency. Therefore you can load a transparent png image into TImage object. | |
ActivePage | ToolBar only displays as many tools as set in the ToolsPerPage property. However, if there are more tools in the ToolCollection, it allows you to finger scroll its client area and bring-in a new page – a new set of tools. The index of the currently active page is determined by this property. | |
Orientation | Specifies whether the tools should be aligned horizontally or vertically. | |
SelectedToolIndex | Index of the tool which is currently selected. | |
ShowToolsSeparators | Determines whether the tools should be graphically separated by a line. | |
ToolsPerPage | How many tools there should be per one page? The size of a tools is determined by the size of ToolBar, its orientation and the amount of tools per page so:SizeOfToolBar / ToolsPerPage = SizeOfTool | |
ToolsSeparatorColor | If ShowToolsSeparators is set to true, this property indicates the color of separators. | |
BorderColor | Specifies the color of the border which drawn around ToolBar if ToolBar DOES NOT contain the input focus. | |
BorderColorFocused | Specifies the color of the border which drawn around ToolBar if ToolBar DOES contain the input focus. | |
DisplayCursor | Should a graphical cursor be displayed under selected tools? | |
Cursor | Contains settings for graphical cursor. Cursor is a round rectangle with a border and gradient-fill. | |
BorderColor | Border color for the cursor. | |
CursorRadius | The radius of round rectangle which is used to draw the cursor. | |
UseGradient | Should the cursor be gradient-filled? If not, it will be solid-filled. | |
CursorColor | Color used to solid-fill the cursor. | |
CursorBackground | Colors used to gradient-fill the cursor. | |
Margins | Specifies the distance between the edge of cursor and the edge of the tool in which the cursor is drawn. The distance is measured in pixels. | |
GradientBackgroundFocused | Specifies the colors for gradient-fill of ToolBar’s background if it DOES contain the input focus. | |
GradientBackground | Specifies the colors for gradient-fill of ToolBar’s background. | |
Opacity | Specifies the opacity of ToolBar’s background in %. | |
UseGradientBackground | Should the background of ToolBar be gradient-filled? | |
BackgroundFillDirection | If the background of ToolBar is gradient-filled, this property specifies the direction of the fill (either top to bottom or left to right). |
Adding tools to ToolBar
- First of all we’ll start with creating a TForm for the project.
- Right-click your project in solution explorer and click Add New Item…
- Click Windows Forms in Categories section and then choose Inherited Form in the Templates section. Give your inherited form a name (e.g., Form1) and then click the ‘Add’ button.
- On Inheritance Picker window click Browse and browse to the folder where you installed iPack (typically c:\program files (x86)\Bee Mobile\iPack\Bee Mobile Controls CF3.5) and choose BeeMobile.TransparentControls.CF3.dll file.
- The list of inheritable objects contained in the selected library will appear.
- Double click TForm and it will be added to your project.
- Right-click your project in solution explorer and click Add New Item…
- With TForm added to your project set a BackgroundImage for it.
- Put ToolBar on the form in Visual Studio designer.
- Right click it and then click Properties.
- Set the Dock property to: DockStyle.Bottom.
- Set the height of ToolBar to 50.
- Your Visual Studio Designer should now look similar to this:
- Notice that the background of the ToolBar is gradient-filled and semi-transparent. You can specify the level of opacity with Opacity property. Also you can define the colors of gradient-fill with GradientBackground and GradientBackgroundFocused properties (the background will be gradient-filled using GradientBackgroundFocused colors if the ToolBar receives input focus). Set the Color1 of GradientBackground to 128, 192, 255 and Color4 to 0, 128, 255. Erase the values for Color2 and Color3.
- Similarly you can set the BorderColor and BorderColorFocused properties. If you wish no border to be displayed, set a value of Transparent to these properties.
- Find the ‘Tools’ property and click the ‘…’ button to open the Tool Collection Editor.
- Add 8 Tool objects. For now we will only set those Tool properties which fall into ‘Text’ category. Notice that the tools are separated by vertical lines (if you change the orientation of your Toolbar to vertical, the separators will become horizontal lines, of course). You can turn displaying these separators off changing the ShowToolsSeparators property of ToolBar to false.
- Assign the following texts to the tools: Home, Contacts, Favorites, Alarm, Downloads, Info, Settings, Help
- Now still in the Tool Collection Editor select all the tools and change their ForeColor and ForeColorSelected to White.
- Change the FontSelected property for all the tools to: Tahoma, 9pt, style=Bold (once a tool is selected, the font will become bold).
- You can align the text label anywhere within the area of a tool. By default, all of them are aligned to bottom and center of the tool’s area.
- You will notice that even the text label is aligned to the bottom, there is still some space between the the text label and the bottom line of the tool. This is because there is a 2 pixels long margin set by default between the text label and the edge of the tool. You can expand the TextMargins property of a Tool and change the margins, if you like.
Adding images to tools
We will use the following images in this tutorial. Download them to your favorite directory on your hard drive.
The first step to add images to the tools is to load the images into the TImageCollection which you can access through Images property:
- Click the ‘…’ button to bring-up TImage Collection Editor.
- Click Add to add a new image.
- Give a name ‘Home’ to your image.
- Set ImageInDesigner property to the first (home) image (navigate to your favorite folder where you downloaded the images). You will notice that the image has immediatelly appeared over all the tools.
- For the image to appear on the ToolBar during run-time we need to set either ImageFromFile or ImageFromResx property (but NOT both).
- ImageFromFile is a path to the file which contains the image. If your image is contained in a file named ’01home.png’ then that is exactly what you would assign to this property. You also have to make sure that that file would be copied to the device.
- ImageFromResx – we will use this variant in this tutorial.
- Make sure the image you are about to assign to this property is part of your project i.e., it appears in the solution explorer.
- Right-click that image in solution explorer and choose properties.
- Set Build-Action property to Embedded resource.
- Now find out the name of the default namespace of your project. Right-click your project in solution explorer and choose properties.
- In case of this project, the default namespace is ToolBarTutorialVB_CF35 (notice that the default namespace name of your project equals to the name of your project by default).
- Now go back to the TImage we edited before and set its ImageFromResx property to: [DefaultNameSpace].[ImageName] or in our case: ToolBarTutorialVB_CF35.01home.png
- Make sure the image you are about to assign to this property is part of your project i.e., it appears in the solution explorer.
- Notice in the Members section that the index of the Home image is 0.
- Repeat steps 2 – 8 to add the remaining 7 images. Don’t forget to set both ImageFromResx and ImageInDesigner properties for each image.
- This is how your ToolBar should look like now:
- Go to Tool Collection Editor again. We are going to edit the properties which fall into the ‘Image’ category.
- First of all we are going to set ImageMargins property for the Tools. The ImageMargins specify the distance between the edge of the image and the edge of tool in pixels. By default, this equals to (bottom, left, right, top) = (0;2;2;2). The text label overlaps with the image which is not what we want. Increase the bottom margin to value 20. By doing this you force ToolBar always to leave a distance between the bottom edge of the image and bottom edge of the tool of 20 pixels.
- The image is now, however, distorted, which is not what we want. Return the Bottom margin value back to number 2 and set StrechImage property to false instead. Do that for all the tools in the Tool Collection.
- Notice that you can also align the image using ImageAlignment property. Using this property together with ImageMargins – you can position the image pretty much anywhere within the tool.
- The ImageIndex and ImageSelectedIndex define the index of an image in TImage Collection which should appear in the tool when it is not selected and when it is selected. In this tutorial we will use the same image for selected tool as well as non-selected tool. Assign an index of 0 to both of these properties for the Home tool, index of 1 to both of these properties for the Contacts tool, etc.
- Click OK to confirm the changes and close the Tool Collection Editor.
- To view the second page of tools in the designer, set ActivePage property to 1.
Cursor settings
ToolBar can display a graphical cursor in the form of round rectangle under the selected tool. This functionality is turned on by default. You can turn it on or off using DisplayCursor property.
The explanation for the properties can be found in the first section of this tutorial.
Events
Handle the SelectedToolIndexChanged event to handle the change of SelectedToolIndex property.
Final application
If you have any questions or suggestions on this tutorial, please send us a feedback at support@beemobile4.net. Your feedback is appreciated. Thank you.