Dynamically Change Button Graphics

While there are several methods for doing this, we are demonstrating the use of css stylesheets in this example.
CLICK HERE to view the exported content of this lesson.
DOWNLOAD SOURCE FILES AND TBK
CLICK HERE to view the exported content of this lesson.
- Gather your button graphics, resize them so that each is the same size.
- Put all the graphics into a common folder. In this case we are calling our folder [img].
- Create a file called "button_styles.css" with the following content. SAVE the file in the [img] folder.
NOTE: We named our swap graphics "1.png", "2.png", ... "8.png" to correspond with the name of objects on our page. You can use any naming convention that work in your project.- Code: Select all
.my_caption {
/* Cross-browser approach! Works in IE8 too!
Demonstrates you can change the button captions (optional) */
font-family: Arial;
font-size: 12pt;
font-weight: bold;
color: #D88537;
}
.btn_1, .btn_1:hover {
background: url( ../img/1.png ) center center no-repeat !important;
}
.btn_2, .btn_2:hover {
background: url( ../img/2.png ) center center no-repeat !important;
}
.btn_3, .btn_3:hover {
background: url( ../img/3.png ) center center no-repeat !important;
}
.btn_4, .btn_4:hover {
background: url( ../img/4.png ) center center no-repeat !important;
}
.btn_5, .btn_5:hover {
background: url( ../img/5.png ) center center no-repeat !important;
}
.btn_6, .btn_6:hover {
background: url( ../img/6.png ) center center no-repeat !important;
}
.btn_7, .btn_7:hover {
background: url( ../img/7.png ) center center no-repeat !important;
}
.btn_8, .btn_8:hover {
background: url( ../img/8.png ) center center no-repeat !important;
}
- Add the [img] folder to the included export list using the PowerPac DHTML Export Tree Manager.
- Create a button on your ToolBook page that is sized to the typical size of your graphics. Choose either a command button or a button with border style set to none. Add a caption for the button. We captioned our button "Dynamically changed button graphics" and named it "mySwitch".
- Let's create a group of 8 objects named "1" through "8" and we will use this to trigger actions that will swap the graphics on the newly created button. We named the group "Selections".
Here is how our layout looks: - Create an action for On load page for the group "Selections" that loads the css stylesheet we created earlier.
- Create an action that sets the button caption using pgButtonHTML() by setting the caption class to a selector from our stylesheet.
- Create an On click action that dynamically changes the class name of our button to one of the selectors in the "button_styles.css" file. We use name of target on the group because it will return the name of the current item in the group that we clicked.
NOTE: Also set the parameter appendClasses = false. This parameter is only available in PowerPac v12.10 or greater.
- Export the page. Click on any object in the group "Selections" and you should see the graphic in the button change to correspond to the png files in the [img] folder.
DOWNLOAD SOURCE FILES AND TBK