42
LESSON 3 Making Controls arrange theMselves
TRY IT
In this Try It, you have a chance to practice using the Anchor and Dock properties. You build the
application shown in Figure 3-8.
FIGURE 38
When the form resizes, the TextBoxes and LinkLabel stretch horizontally.
When the form resizes, the
PictureBox also stretches vertically. Notice in Figure 3-8 that the cover
image is rather tall and thin. When the
PictureBox grows taller, it can display a larger version of
the cover image. The control displays the image as large as possible without distorting it.
Note that the program you build won’t actually do anything except sit there looking pretty and
resizing controls when the form resizes. The techniques you need to make it respond to list selections
are covered in later lessons.
You can download the code and resources for this Try It from the book’s web
page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find
them in the Lesson03 folder in the download.
Lesson Requirements
In this lesson, you:
Create the program’s three main controls: a
MenuStrip, a Panel, and a StatusStrip. Use
Dock properties to make these three controls stay in their proper positions.
Add controls to the
Panel.
Use the
Anchor property to make the ListBox stretch vertically when the form resizes.
Use
Anchor properties to make the TextBoxes and LinkLabel stretch horizontally when the
form resizes.
Use
Anchor properties to make the PictureBox resize vertically when the form resizes.
596906c03.indd 42 4/7/10 12:31:41 PM
Try It
43
Hints
Remember that the
TextBoxes and LinkLabel stretch with the GroupBox that contains
them, not the form itself. If you don’t make the
GroupBox stretch, the controls it contains
won’t either.
To make the
File menu, add a MenuStrip to the form, click it, click the Type Here box that
appears, and type &File. (The ampersand gives the “F” the underline.) Making the menu do
something useful is covered in Lesson 5, so don’t worry about it now.
To make the status strip label, add a
StatusStrip to the form and click it. Click the little
dropdown arrow on the
StatusStrip and select StatusLabel. Click the new StatusLabel
and use the Properties window to set its Text to
“This is a StatusStrip”.
Add some items to the
ListBox and add a picture to the PictureBox, but don’t worry
about making the program take any actions.
Step-by-Step
Create the program’s three main controls: a
MenuStrip, a Panel, and a StatusStrip. Use
Dock properties to make these three controls stay in their proper positions.
1. Start a new project named BetterBookList. Set the form’s Size and MinimumSize
properties to
726, 286.
2. Add a MenuStrip to the form. (Notice that by default the MenuStrip has Dock =
Top
.) Use the MenuStrip hint from the “Hints” section of this lesson to create the
empty File menu.
3. Add a Panel to the form. Set its Dock property to Fill. Set its BackColor property to
light green.
4. Add a StatusStrip to the form. (Notice that by default the StatusStrip has Dock =
Bottom
.) Use the StatusStrip hint from the “Hints” section of this lesson to create
the
“This is a StatusStrip” label.
Add controls to the
Panel.
1. Add controls to the form in roughly the positions shown in Figure 3-8.
2. Set the LinkLabel’s AutoSize property to False and make it the same size as the
TextBoxes.
3. Enter some Text values in the TextBoxes and LinkLabel so you have something to
look at. Enter enough items in the
ListBox so they won’t all fit when the form is its
initial size.
4. Set the PictureBox’s SizeMode property to Zoom. Place a relatively tall, thin image in
its
Image property.
596906c03.indd 43 4/7/10 12:31:41 PM
44
LESSON 3 Making Controls arrange theMselves
Use the
Anchor property to make the ListBox stretch vertically when the form resizes.
1. Set the ListBox’s Anchor property to Top, Bottom, Left.
Use
Anchor properties to make the TextBoxes and LinkLabel stretch horizontally when the
form resizes.
1. Set the GroupBox’s Anchor property to Top, Bottom, Left, Right.
2. Set the TextBoxes’ and the LinkLabel’s Anchor property to Top, Left, Right.
Use
Anchor properties to make the PictureBox resize vertically when the form resizes.
1. Set the PictureBox’s Anchor property to Top, Bottom, Left.
Run the program and see what happens when you resize the form.
Please select Lesson 3 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. (SimpleEdit) Create a new project named SimpleEdit, putting it somewhere you can easily
find so you can add enhancements in later lessons. Give it a
MenuStrip and StatusStrip
with appropriate (default)
Dock values. Add a RichTextBox control and set its Dock property
to
Fill. (That’s all for now. In later lessons you add features to this program.)
2. Make a New Customer dialog box similar to the one shown
in Figure 3-9. Make the First Name, Last Name, Street, City,
and Email
TextBoxes resize horizontally when the form
resizes. Use the OK and Cancel buttons as the form’s accept
and cancel buttons, and attach them to the form’s lower-
right corner.
3. The SplitContainer control displays two areas sepa-
rated by a splitter. The user can drag the splitter to
divide the available space between the two areas. Make
a program similar to the one shown in Figure 3-10. Feel
free to use a different picture and information. Make
the
PictureBox display its image as large as possible
without distortion. Set the bottom
TextBox’s MultiLine
property to
True and make it stretch vertically and horizontally as the form resizes. Make
the other
TextBoxes stretch horizontally. Set the SplitContainer’s Panel1MinSize and
Panel2MinSize properties to 100.
FIGURE 39
596906c03.indd 44 4/7/10 12:31:42 PM
Exercises
45
FIGURE 310
You can download the solutions to these exercises from the book’s web page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find them in
the Lesson03 folder of the download.
596906c03.indd 45 4/7/10 12:31:42 PM
Click here to Play
596906c03.indd 46 4/7/10 12:31:42 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.141.219