I received some questions from Gary M. about my Tricking out your applications article. I figured it might be helpful to someone else so I'm posting it here for everyone's benefit. I expect there will be questions sometimes, but if I'm ever just way off on my level, I hope that someone will let me know!
Heres the code section in question:
public TrickForm()
{
InitializeComponent();
backgroundComboBox.Items.Add(
new TaggedString("RoundedAAA", Properties.Resources.RoundedFrame));
backgroundComboBox.Items.Add(
new TaggedString("Star", Properties.Resources.StarFrame));
backgroundComboBox.Items.Add(
new TaggedString("Gear", Properties.Resources.GearFrame));
}
private void backgroundComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
TaggedString val = (TaggedString)backgroundComboBox.SelectedItem;
Bitmap bmp = (Bitmap)val.Tag;
SetFormBackgroundImage(bmp);
}
What is TaggedString? I can't find a definition for it anywhere.
This is something that I created and is in the project. The purpose of it is to make it easy to add arbitrary objects to a list for data binding. There are other ways to do it using formatting, but for whatever reason I did it that way! In retrospect, it was probably unnecessary.
What is the purpose of the word "new?"
TaggedString is a class (like Form or string). To work with a class, you need an object, which is an instance of the class ("Chevy Impala" is analagous to a class, "my current car" is an instance of a "Chevy Impala"). So to create an instance of the TaggedString class you precede the name with new in order to instruct the CLR (the .NET runtime) to set aside memory, initialize it properly, and allow you to work with it. I also took a shortcut which might have made it seem more complicated:
backgroundComboBox.Items.Add( new TaggedString("Rounded", Properties.Resources.RoundedFrame));
could be expanded to:
TaggedString ts = new TaggedString("Rounded", Properties.Resources.RoundedFrame);
backgroundComboBox.Items.Add( ts );
Based on the way that I created the TaggedString class, the combobox will add ts to the list, invoke it's ToString() method, and display "Rounded".
Later on, when an item is selected in the list, the backgroundComboBox_SelectedIndexChanged event handler is called. Three things happen. From the combobox, recall that we added the TaggedString object directly. Now we can use the SelectedItem property of the combobox to retrieve that original object. Since SelectedItem is an object type (the generic superclass) we need to make it clear that we will treat it as the TaggedString that it is. That's called "casting" and that's why we use the (TaggedString) contruction in the first line.
TaggedString val = (TaggedString)backgroundComboBox.SelectedItem;
The next line uses the Tag property of the TaggedString to get at the underlying object (the actual image in this case). Once again, in order to be reusable, Tag is declared as an Object type so we cast as Bitmap.
Bitmap bmp = (Bitmap)val.Tag;
The final step is to change the background image (using a custom method, SetFormBackgroundImage). Since we have the original Bitmap object, we just pass it to the method:
SetFormBackgroundImage(bmp);
What does properties.resources.roundedframe refer to?
The Properties.Resources reference allows you to access embedded files and data, referred to as resources, in the executable. In the project, I embedded several images as resources using the project properties. Then in the code, I can refer to this embedded data as a simple typed property.
Where are the pictures stored?
As stated, the pictures are embedded in the executable (or DLL if you are creating a library), but the pictures also exist in the Resources folder of the project. Once embedded, the image files have nothing to do with the application at runtime.
I hope this is helpful to someone. Keep the questions coming!
posted @ Saturday, March 25, 2006 10:15 AM