Programming the Preview Button


In this section, you ll add the preview feature to the preferences page. When the page is running, users can click the Preview button to see the effect of their settings. The preview is shown using the labelSampleCaption control. What you ll need, then, is a handler for the Preview button s Click event. In the handler, you ll examine the values of different controls the check box, the drop-down list, and the option buttons and based on the settings of these controls, you ll change the properties of the labelSampleCaption control. There is no magic, all-in-one way to do this; it s a matter of walking through each control one at a time, getting the control s value, and applying it to the property of the labelSampleCaption control.

To begin, you ll declare two String variables to store information about the current color name and the current font and one Boolean variable to store the true/false setting of the check box for showing captions. To declare these variables, click the Code tab and at the top of the file, insert the following statements:

Dim captionColorName As Strin Dim captionFontName As Strin Dim showCaption As Boolean

To add code for the Preview button, in Design view, double-click the Preview button to create the skeleton handler. Now I ll walk you through the code required to preview each individual setting. All of the code discussed in this section is part of the buttonPreview_Click handler.

Turning Captions On and Off

When the user unchecks the checkShowCaptions check box, you want to hide the sample caption label, which you do by setting the label s Visible property to false. Of course, if the user checks the check box again, you need to set the Visible property back to true. To toggle the label s visibility based on the check box setting, add the following code inside the buttonPreview_Click handler:

If checkShowCaptions.Checked = True The     labelSampleCaption.Visible = Tru Els     labelSampleCaption.Visible = Fals End If 

Previewing the Current Color

Next is to see what color the user chooses and apply it to the sample caption label. To use the color you get from the drop-down list, you need to go through a conversion process. Here s the code, which you should add right after the check box test you created in the previous section.

captionColorName = listColorNames.SelectedItem .Tex labelSampleCaption.ForeColor =      System.Drawing.Color.FromName(captionColor Name)

The current item in the drop-down list is available in its SelectedItem property, and what you want from that selected item is its Text property. The Text property gets you a color name such as Black or Red or whatever.

You can change the sample caption label s color by setting its ForeColor property. However, when you re setting a color in ASP.NET, you can t just use a color name. Colors are stored and manipulated internally in ASP.NET using a special format, namely a Color object. Therefore, you have to convert the color name from a string into a Color object. The FromName method of the Color object takes a color name, just what you happen to have, and converts it into the appropriate color format.

start sidebar
Namespaces

What s up with that System.Drawing business when you use the Color object? System.Drawing is a namespace. A namespace is like a library of programming objects. The Microsoft .NET Framework has hundreds of different namespaces.

Every object you work with belongs to a namespace. For example, the Label and DropDownList controls are in the System.Web.UI.Control namespace. When your page runs, it looks in the System.Web.UI.Control namespace for the definition of a Label control. As you ve probably deduced, the Color object is in the System.Drawing namespace.

You have to include the namespace when working with the Color object because the page doesn t know where to look for the definition. The page is designed to automatically know about some namespaces, such as System.Web.UI.Control and about a dozen others that contain objects commonly used in Web Forms pages. But the page doesn t know about every namespace in the .NET Framework, because that would be inefficient the page might have to look through hundreds of namespaces to look up an object definition.

There are two ways to work with objects that are not in namespaces that the page already knows about. One way is to use a fully qualified name by including the namespace name as part of the object, as you did with the Color object.

An alternative is to import, or reference, the namespace in the page. This reference tells the page to include that namespace when looking up objects. In that case, you don t have to use the fully qualified name, which can make your code a bit easier to type and to read.

To import a namespace, click the All tab of the designer window. You can add <%@ Import %> tags right after the <%@ Page %> tag. Your code might look like this:

<%@ Page Language="VB" Debug="true" %> <%@ Import namespace="System.Drawing" %>
end sidebar

Previewing the Font

Users specify a font for the caption using the option buttons. Now comes a somewhat tedious part. Because the option buttons are grouped, you know that only one of them is selected. The only way to tell which button is selected is to examine each one to determine whether its Checked property is set to True, as shown in the following code:

If radioFontArial.Checked = True The     captionFontName = "Arial, Helvetica ElseIf radioFontCourier.Checked = True The     captionFontName = "Courier New, Courier ElseIf radioFontTimes.Checked = True The      captionFontName = "Times New Roman, Times End I labelSampleCaption.Font.Name = captionFontName

Because the options are mutually exclusive, you can use the Visual Basic If-ElseIf structure. If you find this structure confusing, just use a series of If-Then blocks. This code should follow the code for setting the color.

The code looks at the Checked property of each option button in turn. If the button is checked, the code sets the captionFontName variable that you declared at the top of the file to a specific font. When this code has finished, the captionFontName variable contains the font name associated with one of the three option buttons.

In case you re wondering why I m using multiple font names such as Arial and Helvetica for the captions, it s because using a variety of fonts is standard Web practice in case the Web page visitors aren t running Windows. The same font has different names on Windows and on the Macintosh, for example. By providing multiple names, you re allowing the browser to find a font name it recognizes within the same family of fonts. Once you ve gotten the selected font name, you just assign it to the sample label s Font.Name property.

I said at the beginning of this section that all the code you need would be in the Click handler for the Preview button. But I ve presented the code in bits and pieces, so now that we ve finished constructing the code, it would be a good time to review what we ve done.

Reviewing the Preview Handler

Here s a listing of the entire Preview button Click handler, in case you ve lost track of where the different blocks of code should go or you just want to see the complete listing.

Sub buttonPreview_Click(sender As Object, e As  EventArgs     If checkShowCaptions.Checked = True The         labelSampleCaption.Visible = Tru     Els         labelSampleCaption.Visible = Fals    End I     captionColorName = listColorNames.Selected Item.Tex     labelSampleCaption.ForeColor =          System.Drawing.Color.FromName(captionC olorName     If radioFontArial.Checked = True The         captionFontName = "Arial, Helvetica     ElseIf radioFontCourier.Checked = True The          captionFontName = "Courier New, Courier     ElseIf radioFontTimes.Checked = True The          captionFontName = "Times New Roman, Times     End I     labelSampleCaption.Font.Name = captionFont Nam End Sub 

Testing the Preview Button

Now that you ve finished programming the Preview button, you can take the page for a spin to see if everything works properly. Press F5 to run the page. Play with the settings and click the Preview button. The sample caption should be updated to reflect your choices. It s kind of fun, isn t it?




Microsoft ASP. NET Web Matrix Starter Kit
Microsoft ASP.NET Web Matrix Starter Kit (Bpg-Other)
ISBN: 0735618569
EAN: 2147483647
Year: 2003
Pages: 169
Authors: Mike Pope
BUY ON AMAZON

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net