Our focus in this section will be the Panel class. This class can contain and position controls just like the Form class, and supports automated scrolling since it inherits from the ScrollableControl class.2 We will not position controls within a panel in this chapter, but we will use this class to fix some of the problems we have seen when drawing directly on the form. We will draw our photo directly in a Panel, and solve the following problems we noticed when drawing directly on the form: Our image was off-center vertically for the Scale to Fit display option. The DisplayRectangle property included the vertical space occupied by the scroll bar, which threw our calculations off. Here, we will use the panel s DisplayRectangle property, so that the image will be centered exactly inside the panel. The 3-D border we used for the PictureBox control was gone. We could have attempted to draw a border inside the form using the ControlPaint.DrawBorder3D method, but a Panel provides a much easier solution. The Panel class provides a BorderStyle property much like the corresponding PictureBox property, so the .NET framework will draw the border for us. The status bar was part of the scrollable area. Since the Form object managed the scrolling, the StatusBar control on the form was caught up in the scrolling logic. In this section, the scrolling will be managed by the Panel class independent of the form and status bar. As a result, our status bar will return to and remain at its natural position at the base of the form. Before we get into the required changes, figure 7.5 shows how our three display modes will appear by the end of this section. As you can see, the application looks much more polished here than when we drew directly on the form. Note especially the excellent centering, the fine border, and the well-behaved scroll bars.

Using Barcode drawer for Font Control to generate, create DataMatrix image in Font applications.

www.OnBarcode.com

As you will see, the code to draw the image inside a panel is very similar to drawing the image directly on the form. We will need to add a new panel, update some of our menu handlers and the drawing of the status bar, and finally draw the image into the panel. 7.4.1 ADDING A PANEL Adding a Panel object in Visual Studio is much like adding any other control. You open the Toolbox and drag a Panel onto the form. In the source code, the panel is added using the Control property of the parent form. We will look at both of these, beginning with the use of Visual Studio.

Using Barcode printer for iPhone Control to generate, create UCC-128 image in iPhone applications.

www.OnBarcode.com

Take a look at the MainForm.cs source file to see how the panel is created. As you can see, this code looks very similar to the code for other controls from prior chapters. A private instance is created in the MainForm class, initialized in the InitializeComponent method, and added to the form using the Form.Controls property.