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);
gi_edit(...);
gi_combo(...);
const char* pcName
DWORD dwStyle
#define GLS_NR 0x000000
#define GLS_NC 0x010000
#define GES_PASS 0x1
#define GES_NUMB 0x2
#define GCS_DRDNL 0x0
#define GCS_DRDN 0x1
#define GCSS_CSBT 0x0100
#define GSS_RDNL 0x10000000
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)
;
m_Grid->ID[0x00]->Text="Edit Text";
m_Grid->ID[0x01]->SubItem->Insert("Text ListBox Item",-1,0);
m_Grid->ID[0x01]->SubItem->Select=0;
CString strEdit(m_Grid->ID[0x00]->Text);
int nSel(m_Grid->ID[0x01]->SubItem->Select);
CString strSubItem(m_Grid->ID[0x01]->SubItem->Text[0]);
CString strSubItem(m_Grid->ID[0x01]->SubItem->Text);
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:
#define G_BKGND 0xC9DBC8
#define G_GHEAD_CLR 0x9EC29B
#define G_IHEAD_CLR 0xACC2AB
#define G_EDIT_RDNL_CLR 0xE7EEE7
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(...)