Click here to Skip to main content
Click here to Skip to main content

PropertyViewLib

, 5 Aug 2005
Rate this:
Please Sign up or sign in to vote.
A control for easy property control.

Sample Image

Introduction

This control lets you edit variables of objects. The control is designed to ease as much pain as possible, and as a result, only a single line of code is needed to represent each property in the control. Yes, other property controls are out there already, but this one, in my humble opinion, offers exceptional ease of use. Please comment in any way you like.

Using the code

I'll let the code speak for itself, one code strip tells more than a thousand words, so let's dig in.

The IPropertyHost interface

The control is all about implementing the IPropertyHost interface. Objects that need be represented in the property control, must implement this interface. The control will talk to the represented object through this interface.

//To have objects represented in the control, implement this interface
//and SetPropertyHost() on the control. The control will shortly after
//call GetProperties() from the parsed in object, in where you list
//the properties (data members) og the object.
class IPropertyHost
{

public:    

   //The control will ask hosts for propertylist here. This is done when
   //propertyhost is set in the control. Add property items to the list
   //through the parsed in list.
   virtual void GetProperties( EPropList& PropList )
   {
      //add variables to the proplist
   }

   //Called from the view when the property is changing to allow veto from
   //property host. Return true if the change is ok, else false. Override
   //if special constraints are bound to the property. This default
   //implementation allows for any change.
   virtual bool PropertyChanging( const void* pProperty , void* pNewValue )
   {
      return true;    //yes, please change
   }

   //the control will ask you if the property is enabled each time it is
   //redrawn. This enables you to disable a property on the fly.
   virtual bool IsPropertyEnabled( const void* pProperty )
   {
      return true;
   }
}

Getting objects represented in the control

This example shows essentials. Overriding GetProperties() is all that need be done to get started! As you can see, you pass the address of the variable, and the control will read and write to the member through this.

class SomeObject : public IPropertyHost
{

   int     m_nInteger;
   double  m_dAngle;
   int     m_nComboIndex;
   CString m_sText;
   
public:
   
   //
   // IPropertyHost
   //

   virtual void GetProperties( EPropList& PropList )
   {
      PropList.AddPropInt   ( this , "Integer" , &m_nInteger    );
      PropList.AddPropDouble( this , "Angle"   , &m_dAngle      );
      PropList.AddPropCombo ( this , "Combo"   , &m_nComboIndex )
      ->AddString( "Choise0" )
      ->AddString( "Choise1" )
      ->AddString( "Choise2" );
      PropList.AddPropString( this , "String"  , &m_sText       );
   }
   
   virtual bool PropertyChanging( const void* pProperty , void* pNewValue );
   virtual bool IsPropertyEnabled( const void* pProperty );

};

To represent objects in the control, let that object implement the IPropertyHost interface. In the above example, SomeObject implements the IPropertyHost interface and overrides GetProperties(). Call SetPropertyHost() on the control passing an instance of SomeObject, to have that instance represented. The control will shortly after, ask the property host (the SomeObject instance) to list its properties. This way, SomeObject is itself responsible for listing the relevant properties, within its own scope, to the user. Nothing else than GetProperties() function need be changed in order to add or remove properties.

Getting notified on changes

When the user edits a property, the property host itself is notified about the upcoming change, and can deny the change if inappropriate. The control will notify the host with a call to PropertyChanging(). Return true if the property can change the value.

   //To get notified on property changes, override this
   //function. If change is allowed, return true to the property
   //control, telling it to actually apply the change.
   virtual bool SomeObject::PropertyChanging( const void* pProperty , 
                                                    void* pNewValue )
   {
      bool bPleaseChange = true;    //yes, change is ok!

      //if the property being changed is the combo box, i
      //may want to do something with the new index. the index
      //corrosponds the order in which I called AddString()
      if( pProperty == &m_nComboIndex )
      {
         int nNewIndex = *(int*)pNewValue;
         TRACE("combo index changing from %d to %d\n", 
                          m_nComboIndex , nNewIndex );
      }
      
      //if angle is being edited, i'll allow change
      //only if new value is between 0 and 360.
      else if( pProperty == &m_dAngle )
      {
         bPleaseChange = ( 0 <= m_dAngle && m_dAngle < 360 );
      }
      
      //the property control will apply changes
      //to the variable if returning true here.
      return bPleaseChange;
   }

The default PropertyChanging() implementation in IPropertyHost returns true, allowing all changes. Override this, only if you as a property host need be notified on changes, or if you want to deny a certain change.

Enabling and disabling properties on the fly

It is possible to gray out, disable, properties on the fly. On refresh, the control will ask the host if a property is enabled or not.

   //to dynamically enable or disable properties, override
   //this function. The control calls this function on
   //the property host, for each property it draws.
   virtual bool SomeObject::IsPropertyEnabled( const void* pProperty )
   {
      bool bEnabled = true;
      
      //only enable angle property, if
      //combobox is at index 1.
      if( pProperty == &m_dAngle )
      {
         bEnabled = (m_nComboIndex==1);
      }
      
      return bEnabled;
   }

The default IsPropertyEnabled() implementation returns true, enabling all properties always. Only override this if you need dynamic enabling or disabling. You can disable a property by default when adding the property to the property list in GetProperties(). This is useful if you have variables that the user can read, but never change. Additional information of some kind.

Points of Interest

Property types

There are more property types than those shown in the example. Custom property types are easily implemented by subclassing EProperty or a descendant like EIconTextButtonProperty.

Final words

If you find this code useful and use it in an application, I'd like to see a screenshot of your work. This will encourage me to continue working with the control, and make my day. Please send to 'ruskialt' at 'gmail' in the 'com' domain.

History

2005-07-27: Improved look and performance + various additional features

  • Better look & feel

    Strings that don't fit are now shortened and suffixed with "..." to fit. Flicker is reduced, now drawing on memory dc before copying to screen. Node openings now animated, nodes below parent nodes will fall to their new position. Splitters now change mouse cursor when hovering or dragging.

  • Performance enhancements

    The view will now totally skip drawing properties outside the view. Various calculations have been moved to only be calculated when 'dirty'.

  • Various minor new features

    Numeric types now support hex user input. Added 'special case text output' for all numeric types. Splitter added to enable comment pane resize. Multidouble property added to support monitoring a list of doubles. SetType() allows for specifying type of integers, just parse byte width and sign state of the integer in question.

  • Various bug fixes

    Combo box now opens its menu correctly when the view is scrolled. Thousand separator bug fixed. Scrollbars now update their sizes to fit both open and closed node states. Hosts adding child hosts using PropList.AddPropHost(this,&m_SomeHost) are now notified on child change.

2005-04-15: Initial release

  • Source and article released on CodeProject

    Finally this code is released. Thank you CodeProject, for this being possible.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jesper Knudsen
Web Developer
Denmark Denmark
Began programming Amiga machine code in 1992. Was into demo programming and 3D graphics down to the pixel. Switched to PC, C++ and Java in the late 90's. Graduated B.Sc.EE in 2002, now working in maritime industry as a software engineer.

Comments and Discussions

 
QuestionAuto open Checkbox with children when toggling [modified] PinmemberMember 568844325-Feb-13 7:11 
AnswerRe: Auto open Checkbox with children when toggling [modified] PinmemberMember 568844325-Feb-13 8:29 
GeneralRe: Auto open Checkbox with children when toggling PinmemberMember 568844326-Apr-13 14:55 
Generalsomething about Eproperty Pinmemberxiang_yan29-Oct-10 19:27 
QuestionHow to refresh the data? Pinmemberxiang_yan22-Oct-10 22:38 
QuestionRado button as property? PinmemberArrin18-May-10 2:30 
QuestionProperty View Lib Control for Windows Vista Pinmemberprakashchopade19822-Mar-09 0:27 
Questionhow to use the EXTreeCtrl? Pinmemberalphasky_cx31-Jul-08 20:52 
QuestionHow can I put Chinese or JS charactors into the PropertyView? Pinmemberzhang.china13-Dec-07 21:21 
QuestionHow to Make a Edit ComboBox? Pinmembermconstor25-Nov-07 4:19 
AnswerRe: How to Make a Edit ComboBox? PinmemberJesper Knudsen25-Nov-07 9:59 
AnswerRe: How to Make a Edit ComboBox? Pinmembermconstor26-Nov-07 3:51 
QuestionHow to add a edit box after the checkbox? Pinmemberhenpacked6-Nov-07 22:08 
AnswerRe: How to add a edit box after the checkbox? PinmemberJesper Knudsen6-Nov-07 22:51 
QuestionHow to add a spinbutton in the number edit box? Pinmemberhenpacked6-Nov-07 21:50 
AnswerRe: How to add a spinbutton in the number edit box? PinmemberJesper Knudsen6-Nov-07 22:24 
GeneralRe: How to add a spinbutton in the number edit box? Pinmemberhenpacked8-Nov-07 16:04 
GeneralRe: How to add a spinbutton in the number edit box? PinmemberJesper Knudsen25-Nov-07 9:55 
GeneralRe: How to add a spinbutton in the number edit box? Pinmemberunakie17-Aug-08 17:43 
GeneralThanks for your good work.Can you give me you emailaddress ? [modified] Pinmemberhillstream4-Nov-07 16:51 
GeneralMinor bugs with double and int casting PinmemberZitoune 231-Oct-07 6:22 
GeneralRe: Minor bugs with double and int casting PinmemberJesper Knudsen1-Nov-07 10:38 
GeneralRe: Minor bugs with double and int casting PinmemberZitoune 22-Nov-07 6:07 
GeneralRe: Minor bugs with double and int casting PinmemberJesper Knudsen3-Nov-07 10:54 
GeneralRe: Minor bugs with double and int casting PinmemberZitoune 25-Nov-07 0:24 
QuestionHow to let a Property expands itself when my application run? Pinmembermconstor30-Oct-07 17:33 
AnswerRe: How to let a Property expands itself when my application run? PinmemberJesper Knudsen1-Nov-07 10:29 
GeneralRe: How to let a Property expands itself when my application run? PinmemberEntrase2-Jul-08 4:09 
GeneralRe: How to let a Property expands itself when my application run? PinmemberJesper Knudsen2-Jul-08 20:12 
GeneralRe: How to let a Property expands itself when my application run? Pinmembertermal15-Jun-09 3:13 
GeneralRe: How to let a Property expands itself when my application run? PinmemberEntrase16-Jun-09 4:16 
GeneralRe: How to let a Property expands itself when my application run? Pinmembertermal16-Jun-09 4:33 
QuestionHow to reset only one property page Pinmembermanish d w13-Aug-07 1:52 
AnswerRe: How to reset only one property page PinmemberJesper Knudsen13-Aug-07 9:47 
GeneralRe: How to reset only one property page Pinmembermanish d w18-Sep-07 20:42 
GeneralRe: How to reset only one property page PinmemberJesper Knudsen22-Sep-07 21:23 
GeneralRe: How to reset only one property page PinmemberSteveNorthcc27-Sep-07 10:19 
QuestionHow to clear and fill combo box dynamically? Pinmemberaniemsh31-Jul-07 22:15 
AnswerRe: How to clear and fill combo box dynamically? PinmemberJesper Knudsen1-Aug-07 8:40 
QuestionHow to set the focus to default property while startup Pinmemberaniemsh30-Jul-07 3:41 
AnswerRe: How to set the focus to default property while startup PinmemberJesper Knudsen30-Jul-07 19:49 
GeneralRe: How to set the focus to default property while startup Pinmemberaniemsh1-Aug-07 4:48 
GeneralSet Focus to Property Pinmemberdhd26-Jul-07 23:55 
GeneralRe: Set Focus to Property PinmemberJesper Knudsen27-Jul-07 4:43 
QuestionNon Editable property appears editable Pinmemberdhd4-Jul-07 20:44 
AnswerRe: Non Editable property appears editable PinmemberJesper Knudsen5-Jul-07 10:34 
GeneralRe: Non Editable property appears editable Pinmemberdhd11-Jul-07 3:31 
GeneralRe: Non Editable property appears editable PinmemberJesper Knudsen11-Jul-07 9:22 
GeneralComboBox Question PinmemberKrypton_egg25-May-07 8:39 
GeneralRe: ComboBox Question PinmemberJesper Knudsen28-May-07 3:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 5 Aug 2005
Article Copyright 2005 by Jesper Knudsen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid