Lab 11
comp 125-609, Goldwasser
Tuesday, 16 November 1999

Purpose: Four Additional Controls
Today's lab is designed to help you learn to use four additional controls from chapter 5.2 of the text.

As a sample application, we will design a window which allows the user to select font attributes. Besides having controls which allow the user to specify the attributes, there should be one command button with caption "Apply", and there should be one picture box with width=3135 and height=855. Whenever the user clicks on the Apply button, you should clear the picture box and then print the string "Visual Basic", centered in the picture box, using the current font selections (we will discuss how to center the text later in this lab).


The user should be able to select the following font attributes:
  • There should be a list box which allows the user to select Picture1.Font.Name from a list of all available font names. Set the list box's Sorted property to True to get the list sorted. (see below for how to get all available font names at runtime)
  • There should be option buttons to allow the user to select Picture1.ForeColor from the choices: black, red, blue, green.
  • There should be option buttons to allow the user to select Picture1.Font.Size from the choices: 8, 10, 12, 14, 18, 24.
  • There should be a set of check boxes allowing the user to select any of the following conditions to be true: Picture1.Font.Bold, Picture1.Font.Italic, Picture1.Font.Underline, Picture1.Font.StrikeThrough.

  • You should only need to write code for two routines: Form_Load() and Command1.Click()

    Form_Load() will be used to set up the list box for the font name choices. The choices could have been set up at runtime, however the list of available fonts depends on the particular machine you are running on. So the robust way to do this is to have your program make the listbox match the available fonts at runtime. Visual Basic makes this possible with the following two methods: Screen.FontCount is equal to the number of available fonts. The names of these fonts are stored in an array Screen.Fonts() which is indexed from 0 to FontCount - 1. You should write a loop which goes through this array, adding each available font name to the listbox.

    Command1.Click() is activated when the user clicks "Apply" and this is where we will put all the code for actually making changes to the Picture1.Font along with redisplaying the sample. To set the font according to the user's selections, you will have to walk through all the relevant control properties setting the real font properties to match. After you have updated the picture box font properties, you will want to clear the screen and print the string "Visual Basic" using the new font.

    Finally, we have asked that you properly center the font in the picture box. It is probably best for you to ignore this requirement until you have everything else working. To properly center the sample, you will have to set the CurrentX and CurrentY values accordingly before calling Print. Of course, what to set them to depends on the new choice of font properties. Recall that Picture1.TextHeight("Visual Basic") and Picture1.TextWidth("Visual Basic") will return the relative size of the string in the picture box. You will have to propery use this information along with the current picture box scale information (which you may choose to change).


    Still have some extra time?

    You could make the program more robust by completely removing the command button and its Click routine, and instead writing event procedures for all of the invidual controls being used in this program. In this way, you can update the sample text immediately whenever the user changes a selection.


    This lab is due before leaving class today. When you have completed te program, please call me over to your computer to test it.