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

Control Data Support

, 18 Feb 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
A class to enable data to be set on a control
<!-- Add the rest of your HTML here -->

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Paul Vickery
Software Developer (Senior)
United Kingdom United Kingdom
Originally from an electronics background, I moved into software in 1996, partly as a result of being made redundant, and partly because I was very much enjoying the small amount of coding (in-at-the-deep-end-C) that I had been doing!
 
I swiftly moved from C to C++, and learned MFC, and then went on to real-time C on Unix. After this I moved to the company for which I currently work, which specialises in Configuration Management software, and currently program mainly in C/C++, for Windows. I have been gradually moving their legacy C code over to use C++ (with STL, MFC, ATL, and WTL). I have pulled in other technologies (Java, C#, VB, COM, SOAP) where appropriate, especially when integrating with third-party products.
 
In addition to that, I have overseen the technical side of the company website (ASP, VBScript, JavaScript, HTML, CSS), and have also worked closely with colleagues working on other products (Web-based, C#, ASP.NET, SQL, etc).
 
For developing, I mainly use Visual Studio 2010, along with an in-house-designed editor based on Andrei Stcherbatchenko's syntax parsing classes, and various (mostly freeware) tools. For website design, I use Dreaweaver CS3.
 
When not developing software, I enjoy listening to and playing music, playing electric and acoustic guitars and mandolin.
Follow on   Google+   LinkedIn

Comments and Discussions

 
GeneralVery good! PinmemberWREY16-Jun-04 11:30 
GeneralUse PropertyBag instead of a data pointer PinmemberPatje19-Feb-03 6:39 
GeneralRe: Use PropertyBag instead of a data pointer PinmemberWREY16-Jun-04 10:51 
GeneralRe: Use PropertyBag instead of a data pointer PinmemberPatje16-Jun-04 22:45 

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 | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 19 Feb 2003
Article Copyright 2003 by Paul Vickery
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid