7.8 Assigning Custom Data to a Windows Form

 <  Day Day Up  >  

You want to dynamically change a form's properties using a custom configuration file.


Technique

Select the form you want to edit in the form editor. Expand the Dynamic Properties property in the property browser and click the browse button in the Advanced field. Place checkmarks next to the form properties that you want to dynamically configure using a configuration file, as shown in Figure 7.5. This process creates a .config file that will be deployed with your application. Each property that this file dynamically configures will be listed in the appSettings element using attributes for a key and its associated value . Changing a value attribute has the effect of changing that form's corresponding property when the application runs.

Figure 7.5. Use the Dynamic Properties dialog to select the properties whose values are read from the application configuration file.

graphics/07fig05.gif

You can also use the configuration file to control custom properties of your Windows Form, although you are unable to do so using the form editor. To create a dynamic property that is mapped to a property you created, add a new element in the .config file named add whose key attribute corresponds to the name of the property you want to control and whose value attribute is the value to set on the property. The following configuration file sets the property named DisplayWelcome to false when the form is displayed:

 
 <?xml version="1.0" encoding="utf-8" ?> <configuration>     <appSettings>         <add key="Form1.DisplayWelcome" value="False" />     </appSettings> </configuration> 

In the constructor for your Windows Form, create a new System.Configuration.AppSettingsReader object. Set the property that you are dynamically controlling equal to the value returned from the GetValue method defined in the AppSettingsReader method. This method accepts two parameters. The first parameter denotes the name of the property whose value you want to retrieve, and the second parameter is a System.Type object corresponding to the data type of the property. You can create a System.Type object by using the typeof operator and passing the actual data type of the property to that operator. Finally, because the GetValue property returns a generic System.Object , cast the return value to the proper data type when setting your property:

 
 public Form1() {     InitializeComponent();     System.Configuration.AppSettingsReader configurationAppSettings =         new System.Configuration.AppSettingsReader();     this.DisplayWelcome =         ((bool)(configurationAppSettings.GetValue("Form1.DisplayWelcome",             typeof(bool)))); } 

Comments

Configuration files let you alter the behavior of an application without having to rebuild the application's binary files. To circumvent overpopulating the system registry and to facilitate platform independence in the future, applications built using the .NET Framework can take advantage of application-setting configuration files. A configuration file is generated automatically when you use the form editor in Visual Studio .NET to create a dynamic property. In the first example given in the technique recipe, when you enable a standard Windows Form property to be dynamically read from a configuration file, the form editor does two things. First of all, it creates a configuration file, which is named based on the name of the executable with a .config file extension. The form editor then adds a new element named add in that XML-based configuration file. Finally, the form editor generates the code necessary to read the value from the configuration file and sets the corresponding property within your class.

The form editor works well for properties already defined within the System.Windows.Forms.Form class but does not have the ability to create dynamic properties for any extra properties you have added. This, of course, doesn't mean it can't be done. All you have to do is emulate what the form editor does and add the necessary plumbing yourself. However, the form editor generates the dynamic property code within your class's InitializeComponent method. When you create your own dynamic properties, take note that if you follow that pattern, it is entirely possible that the form editor will overwrite your code if placed within that method. Therefore, place any dynamic-property configuration code within your class constructor or another method that is not used for code generation, such as your form's Load event handler.

 <  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