.
GDI+ FAQ
Skip Navigation LinksWelcome : Windows Forms Tips and Tricks : Accessing dialog properties

Dialog Property access in Windows Forms.

People making the transition from legacy technologies to Windows Forms often ask about the right way to access properties in a form or dialog. MFC used a property exchange mechanism that was called to populate the controls in a dialog with their initial values and then again, when the editing was complete, to extract the information from the controls and replace the values in the dialog which could be read out by the calling code. Windows forms dispenses with that method but there is no clear strategy for performing a transfer of information from an application, to a dialog, then a control within the dialog and back out again. You are therefore left with a rule-of-thumb method that basically means "do it yourself"

Although there is no method set in stone, these guidelines will help you to create a consistent user-interface strategy which will be easy to maintain.

When creating a form the designer makes the controls private members of the form so accessing the control properties directly isn't possible unless you change the access of the objects to public. This is not recommended because the designer maintains the controls using a form of source-serialization so changing what the designer does can be a bad thing hence the directive never to change InitializeComponent manually.

Forms or dialogs need properties to be exposed that can be accessed by the calling program so that you can do something like...

C#

   Dialog dlg=new Dialog();

   dlg.TheValue=this.label1.Text;

   if(dlg.ShowDialog()==DialogResult.OK)

    this.label1.Text=dlg.TheValue;

 VB

   Dim dlg As New Dialog()

   dlg.TheValue = Me.label1.Text

   If dlg.ShowDialog() = DialogResult.OK Then

     Me.label1.Text = dlg.TheValue

   End If

Here you see that Dialog exposes a property called TheValue. Before the dialog is shown, the property is initialized with the data, the dialog is allowed to do whatever it needs to to present the information to the user and enable editing and then, depending on the return value of the dialog, the value is used or discarded.

Inside the dialog class a few things have to happen.

  • When the dialog is loaded, the information from the properties need to be copied into the controls on the form. This is done in the OnLoad override.

  • When the form is closed, the data from the controls needs to be copied to the public properties so that they can be read.

The dialog that handles this is shown in the following listing.

Show me code in C#

Show me code in VB

Summary.

  • Create a public property of the type required such as string for text-boxes, bool for check-boxes and so-on.

  • When the dialog is created, initialize all the public properties with the initial values.

  • When the dialog's OnLoad override is called, update the private controls with the values from the public properties.

  • When the OnClosing override is called, transfer the contents of the private controls to the public properties.

  • Depending on the DialogResult, use or discard the dialog properties in the calling method.

 

Return to Windows Forms Tips and Tricks.

Copyright Bob Powell 2004. All rights reserved.

 

Sponsored By
DaraizeTechnologies.com

&
Proteus Groupe

Bob Powell

Create your badge

Copyright © Bob Powell 2000-2012.  All rights reserved.