Representation of data is one the central aspects of any application. Tabular representation of data is the most common method of representing given data. Each language/framework provides its own technique, in the form of controls, to present data in tabular format. MFC (Microsoft Foundation Classes) is no exception. One of the controls that it provides is the MSFlexGrid control.
Programming MSFlexGrid in MFC (VC++) (Page 1 of 4 )
In this discussion, I will focus on the basics of MSFlexGrid. The first section will be about the whys and wherefores of MSFlexGrid. In the second section, I will focus on the steps required to use MSFlexGrid. The third and fourth sections will be about using MSFlexGrid in a real world application. That's the outline for this discussion.
MSFlexGrid - What is it?
Simply put, Microsoft FlexGrid or MSFlexGrid is a control that is used to display and operate upon tabular data. It provides a flexible way to sort, merge and format tables. The table generated by MSFlexGrid can have strings as well as pictures. Thus, MSFlexGrid provides all the essential services expected from a control that works on tabular data.
To understand a control, its essential to know two main aspects of a control. They are properties and events. The former is concerned with the attributes of the control such as its look, its rows, columns, and so forth, whereas the latter is concerned with changes in the attributes of the control.
Properties are the methods (also called getters and setters) that help in controlling the attributes or data-fields of a control. The most commonly used properties of MSFlexGrid are:
GetRows and SetRows
GetTextArray and SetTextArray
GetRowSel and GetColSel
GetTextMatrix and SetTextMatrix
As you have seen, each property is a combination of getter and setter methods. Below I will discuss each one in detail.
First, the two methods called GetRows and SetRows are known together as the Rows property. This property is used to get or set the number of rows in a grid. SetRows takes an integer argument that tells how many rows there will be in the grid. Similarly, the GetRows method returns the number of rows in the currently displayed grid.
GetTextArray and SetTextArray together form the TextArray property. This property can be used to set or retrieve the values without using Rows and the corresponding Cols properties. Both GetTextArray and SetTextArray take an argument of the long data-type. This argument is used to retrieve or set the value of the cell specified by the argument. The argument is calculated by multiplying the required row's number with the column value returned by GetCol, and adding it to the value of the required column number. GetTextArray returns the value of the cell determined by the value in the passed argument. SetTextArray accepts one more argument of type LPCTSTR (which is another name for const char*). The second argument is used to set the new value of the cell specified by the first argument.
Together, GetColSel and SetColSel form the ColSel property. The ColSel property can be used to get the value of a selected column or set the value of a selected column. GetColSel returns a long value indicating the selected column. SetColSet takes a long value that indicates the column to be selected. Akin in its behavior to the ColSel property, there is also a RowSel property that performs similar operation on rows.
The GetTextMatrix and SetTextMatrix methods together form the TextMatrix property. This property can be used to retrieve or set the value of an arbitrary cell without changing Row and Col properties. The GetTextMatrix takes two arguments of the long type specifying the row and column whose value needs to be retrieved. The SetTextMatrix, along with the two long arguments, takes an extra argument of type LPCTSTR. The third argument is the value that needs to be set in the cell specified by the first two arguments.
Now that we've covered all the properties, it's time to look at events. By definition, an Event is "a change in the state of an object." The most common events supported by MSFlexGrid are OnKeyPress and OnDblClick. Both of these are common to other controls as well.
The OnKeyPress event is fired when the MSFlexGrid has focus and the user presses any key. The ASCII value of the pressed key is available through the keyAscii argument. It is of the type short FAR*.
The OnDblClick event, as the name suggests, occurs when the user double clicks a cell. It doesn't pass on any values.
That completes an overview of MSFlexGrid. Next let us look at the steps involved in using MSFlexGrid in an application.