Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Control Data Support

0.00/5 (No votes)
18 Feb 2003 1  
A class to enable data to be set on a control

Introduction

I have often found it useful to be able to attach data to a control in the same way as I would attach my own data to list items in a CListCtrl or CComboBox. For instance, I may want to indicate that a control contains a user name, or that it contains a file name, so that code which automatically builds commands etc from controls can process the contents of the control accordingly.

The CCtrlDataSupport class presented here allows this support to be added to any control. (In fact it can be added to any class whether or not it is a control.)

OK, so the class is not exactly rocket science - it just adds a data member to your class, and a few functions to set and get the data - but I find I make a lot of use of this.

How to use

To use the class you add it to the list of classes from which your control is derived. So to add the support to a CComboBox you would have something like:

class CComboBoxData : public CComboBox, public CCtrlDataSupport
{
};

(Clearly the downside of this is that you then have to use CComboBoxData instead of just using CComboBox, but I rarely use the MFC classes as they are.)

The Class

The class to add control data is below:

////////////////////////////////////////////////////////////////////////////

// Class:    CCtrlDataSupport

// Version:    1

// Created:    19-Feb-2003

// Author:    Paul S. Vickery

// E-mail:    paul@vickeryhome.freeserve.co.uk

////////////////////////////////////////////////////////////////////////////

// Description:

//    Class to allow data to be set on controls etc.

//

// Base class:

//    None.

//

// Functions:

//    CCtrlDataSupport(); // constructor

//    LPVOID GetCtrlDataPtr() const;  // get control data as a void pointer

//    void SetCtrlDataPtr(LPVOID lpData)  // set control data as a void pointer

//    DWORD GetCtrlData() const;  // get control data as a DWORD

//    void SetCtrlData(DWORD dwData)  // set control data as a DWORD

//

////////////////////////////////////////////////////////////////////////////

// You are free to use or modify this code, with no restrictions, other than

// you continue to acknowledge me as the original author in this source code,

// or any code derived from it.

//

// If you use this code, or use it as a base for your own code, it would be 

// nice to hear from you simply so I know it's not been a waste of time!

//

// Copyright (c) 2003 Paul S. Vickery

//

////////////////////////////////////////////////////////////////////////////

// Version History:

//

// Version 1 - 19-Feb-2003

// =======================

// Initial version

// 

////////////////////////////////////////////////////////////////////////////

class CCtrlDataSupport
{
public:
    CCtrlDataSupport() : m_dwData(0) { };
    // get control data as a void pointer

    LPVOID GetCtrlDataPtr() const { return m_lpData; }
    // set control data as a void pointer

    void SetCtrlDataPtr(LPVOID lpData) { m_lpData = lpData; }
    // get control data as a DWORD

    DWORD GetCtrlData() const { return m_dwData; }
    // set control data as a DWORD

    void SetCtrlData(DWORD dwData) { m_dwData = dwData; }
private:
    union { LPVOID m_lpData; DWORD m_dwData; };
};

History

Version 1 - 19 Feb 2003

  • First version

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