8.2 Changing Control Properties

 <  Day Day Up  >  

You want to alter the appearance of a Windows Form control.


Technique

Change the properties of a Windows Form control by opening the associated Windows Form in the form designer and selecting the control you want to change. The property browser tool window displays all the properties available for that control. To change a property, select the input box located to the right of the property name . Based on the data type of that property, a UI Type Editor control appears. For standard text properties, you can simply enter the new text. For property types that accept a predefined set of values, you see a drop-down arrow. Clicking that arrow displays a drop-down list of possible values. Other UI Type Editors include browse buttons for file-based properties such as the Image property, color-chooser controls for color -based properties such as BackColor , or a layout-based editor for properties that alter the layout of a control, as with the Anchor property. Additionally, as each property field obtains focus, a short description of the property appears at the bottom of the property browser window.

Comments

One of the fascinating features of Windows Forms is the ability to drastically design and alter the appearance of a user interface without ever having to write custom code. If you do get to the point of having to write code to change a property on a form, the names of the properties within the property browser are the same as the property defined in the class, allowing you to immediately see the effects before committing your changes to code. Each property within the control class uses a data type that maps to the type editor used during the design phase. For instance, the Text property on a Label control uses a string data type. The BackColor property, however, uses the System.Drawing.Color data type. Therefore, the code to change the properties of a Label control contains varying data types, as shown in Listing 8.2. In this listing, the Label control uses a different font from the font used as a default and also takes advantage of control colors to change the ForeColor and BackColor properties.

Listing 8.2 Defining Control Properties Using a Variety of Data Types
 private void InitializeComponent() {     this.mylabel = new System.Windows.Forms.Label();     this.SuspendLayout();     // label properties     this.mylabel.BackColor = System.Drawing.SystemColors.ControlDark;     this.mylabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;     this.mylabel.Font = new System.Drawing.Font("Team MT", 24F,         System.Drawing.FontStyle.Bold,         System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));     this.mylabel.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)),         ((System.Byte)(0)), ((System.Byte)(192)));     this.mylabel.Location = new System.Drawing.Point(24, 64);     this.mylabel.Name = "mylabel";     this.mylabel.Size = new System.Drawing.Size(232, 120);     this.mylabel.TabIndex = 0;     this.mylabel.Text = "This is a label control";     this.mylabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;     // form properties... } 
 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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