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

GridCtrl

, 6 Jun 2005
Rate this:
Please Sign up or sign in to vote.
This article describes GridCtrl that consists of common controls such as EditBox, ComboBox and Button. The GridCtrl helps you create, exchange and manage layouts.

Sample Image

Introduction

Some times I work at forms that have many data fields, such as combo, edit and buttons. Also, I need removing, dynamically adding and managing the layout of these fields automatically. Finding nothing such as I needed, I decided to create a control by myself, that should have these features.

I want to express my thanks to Mathias Tunared for his AdvComboBox that I am using in my project.

What the GridCtrl does:

  • Creates three controls: edit, combo or button; that have a static (header) field on left side;
  • Combines these fields into column and row, that have header (on top);
  • Allows you to have access to any field with help index or ID manager;
  • Makes exchanging data with fields possible;
  • Adds or removes any field at run-time.

How to use

At first, you should create GridCtrl. For that, use the function Create, that is defined as:

bool CGridCtrl::Create(DWORD dwStyle,RECT& rct,CWnd* pParent,UINT nID);
  • DWORD dwStyle - style of the GridCtrl which can be one of the following:
    • 0 - create simple grid;
    • GS_AUTOVSIZE - create grid that will change its own vertical size.
  • RECT& rct - size of GridCtrl. If you define GS_AUTOVSIZE, RECT::bottom has no effect.
  • CWnd* pParent - parent window.
  • UINT nID - ID of GridCtrl.

Before adding a field, I should explain some features. The GridCtrl has a class CGridMgr, that manages operations like insertion, deletion etc. It's available via the overloaded operator->. There are two ways in GridCtrl for managing these operations: using the appropriate functions or using properties.

gi_hdr(const char* pcName=0, DWORD dwStyle=0, 
        DWORD dwID=-1, void* pvData=0); //Header item
gi_edit(...);  //Edit item
gi_combo(...); //Combo item

const char* pcName //- title;
DWORD dwStyle      //- style

/////// Layout style /////////
#define GLS_NR        0x000000    //create with new row
#define GLS_NC        0x010000    //create with new col

/////// Grid edit style //////////////
#define GES_PASS    0x1        //password
#define GES_NUMB    0x2        //number

/////// Grid combo style /////////////
#define GCS_DRDNL    0x0        //Drop down list
#define GCS_DRDN    0x1        //Drop down

/////// Grid custom style ////////////
#define GCSS_CSBT    0x0100 
// Item with custom button
// (send command message to parent when click)

/////// Grid shared style ////////////
#define GSS_RDNL    0x10000000    //Item is readonly
  • DWORD dwID - ID of item (any message sent to parent is a standard notifications message).
  • void* pvData - User defined data.

Now, we have enough information for creating GridCtrl and adding some items. Let me show an example to you, so that you can fully appreciate the process:

    CGridCtrl m_Grid;

    m_Grid <<gi_hdr("Header")
        <<gi_edit("Edit",0,0x00)
        <<gi_combo("Combo",0,0x01)

        <<gi_hdr("Header",GLS_NR)
        <<gi_edit("Edit(password)",GES_PASS,0x02)
        <<gi_combo("Combo (drop down list) with cbutton",GCS_DRDNL|GCSS_CSBT,0x03)
    ;

    //Follow code example how to exhange data with items:

    //set text to edit
    m_Grid->ID[0x00]->Text="Edit Text";
    //insert item into ComboBox
    m_Grid->ID[0x01]->SubItem->Insert("Text ListBox Item",-1,0);
    //select first item
    m_Grid->ID[0x01]->SubItem->Select=0;

    //get text from edit
    CString strEdit(m_Grid->ID[0x00]->Text);

    //get current selectetd item from combo
    int nSel(m_Grid->ID[0x01]->SubItem->Select);

    //get text from first item
    CString strSubItem(m_Grid->ID[0x01]->SubItem->Text[0]);

        //or

    CString strSubItem(m_Grid->ID[0x01]->SubItem->Text);
    //get text from current selected item

Function and classes

  • Class CGridCtrl
    • bool Create(DWORD dwStyle,RECT& rct,CWnd* pParent,UINT nID) - see top of this article.
    • _GridMgr* operator->() - gives access to a _GridMgr object.
    • CGridCtrl& operator<<(grid_item& gi) - insert new item to back.
    • void MoveWindow(RECT* rct) - move grid control according to the set GS_AUTOVSIZE flag.
  • template<class TObj>class CGridMgr
    • CItemMgr<TOBJ>* ID[] - get property for accessing item manager with ID.
    • CItemMgr<TOBJ>* Index[] - get property for accessing item manager with Index.
    • bool ReadOnlyAll - put property set's all item as read only.
    • DWORD Count - get property return number of item in manager.
    • void DeleteAll() - delete all items from grid manager.
  • template<class TObj> class CItemMgr
    • bool Insert(grid_item& gi) - insert item into item manager.
    • bool Delete() - remove item from item manager.
    • CString Name - put/get property set/get header item name.
    • CString Text - put/get property set/get item text.
    • GI_TYPE Type - get property return type of item.
    • void* Data - put/get property set/get user defined data to/from item.
    • CSubItemMgr<CADVCOMBOBOX>* SubItem - get property return subitem manager for an item as combo box.
    • int ID - get property return item ID.
    • int Index - get property return item index.
    • bool ReadOnly - put/get property set/get read only state.
    • void SetFocus() - sets focus to item.
  • template<class TCombo> class CSubItemMgr
    • void Insert(const char* pcText,LPARAM lData=-1,int nIndex=-1) - insert subitem into combobox.
    • void Delete(int nIndex=-1) - delete by index. If nIndex = -1, last item will be deleted.
    • int Find(const char* pcText) - find item with specified string. If the find succeeds return index of the item, otherwise return -1.
    • int Find(LPARAM lData) - find item with specified data, return value same as int Find(const char* pcText).
    • int Count - get property, return count item in combobox.
    • int Select - put/get property, set/get selection to currently selected item in combo box.
    • CString Text[] - get property, get text from item with index.
    • LPARAM Data[] - get property, get data from item with index.

Color and messages

Color of GridCtrl has been selected at the time of development, but you can change any color using macros:

//GridCtrl.h

#define G_BKGND          0xC9DBC8        //background
#define G_GHEAD_CLR      0x9EC29B        //color of header (from item top)
#define G_IHEAD_CLR      0xACC2AB        //color of header (at item left)
#define G_EDIT_RDNL_CLR  0xE7EEE7        //background of readonly items

Any message from items in the grid are sent to parent window that created the grid. Thr ID of message is the ID that is passed to one of these:

gi_hdr(...)
gi_edit(...)
gi_combo(...)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Arshynkin Maksym
Architect
Ukraine Ukraine
No Biography provided

Comments and Discussions

 
General"class" should change to "typename" in vc9 Pinmemberstonexin22-Feb-11 15:27 
GeneralRe: "class" should change to "typename" in vc9 Pinmembersl11l11l11l30-May-11 22:18 
GeneralRe: "class" should change to "typename" in vc9 Pinmemberstonexin31-May-11 4:08 
GeneralRe: "class" should change to "typename" in vc9 Pinmembersl11l11l11l31-May-11 17:18 
GeneralItem split percentage Pinmemberjaclambert20-Nov-07 9:40 
GeneralSolution for problem with Visual Styles enabled Pinmembermi-chi4-Jul-07 4:58 
GeneralFeature request PinmemberBoniolopez28-May-06 23:03 
GeneralRe: Feature request PinmemberArshinkin Maxim20-Jul-06 3:13 
Yes, It is a good idea. But now I try to write for windows only less and less. This control was modified and have corossplatform architecture all code was refactored and has been written under Qt library. It have worked under windows and linux. But I not send its to codeproject.

QuestionHow can i handle the button click event? Pinmemberdirlee26-Dec-05 14:59 
AnswerRe: How can i handle the button click event? PinmemberL2Max5-Jan-06 1:32 
GeneralVery usefull Pinmembercaesten5-Dec-05 12:11 
GeneralExactly what i was looking for Pinmembertabor2521-Jul-05 10:29 
GeneralI can't test it in VC6 PinsussAnonymous21-Jun-05 23:41 
GeneralRe: I can't test it in VC6 Pinmemberi8423-Aug-05 21:53 
GeneralRe: I can't test it in VC6 Pinmemberonlysfang25-Oct-08 2:10 
GeneralRe: I can't test it in VC6 Pinmembershaonew14-Dec-09 20:05 
GeneralVisual Studio Format Version 7.00 Pinmemberjan marco15-Jun-05 18:08 
GeneralRe: Visual Studio Format Version 7.00 PinmemberPinselwascher22-Jun-05 4:25 
GeneralCOOL PinmemberMMs_xH8-Jun-05 23:03 
GeneralRe: COOL PinsussAnonymous9-Jun-05 4:20 
GeneralRe: COOL PinmemberMMs_xH9-Jun-05 4:50 
GeneralRe: COOL PinmemberL2Max9-Jun-05 8:51 

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
Web02 | 2.8.140721.1 | Last Updated 7 Jun 2005
Article Copyright 2005 by Arshynkin Maksym
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid