Introduction
It all began with a research for a dynamic multicolumn combobox. What I found had restrictions in the count of rows or fixed count of columns during the creation of the control. So I decided to develop a more flexible one.
Everything started with the multi column feature. Later on, I saw a wonderful feature during my development work at my job with the Janus GridEX control. It provided FormatConditions
to define colors and font styles depending on field values, so I implemented my own variation of them.
Background
There are some specialities to take into account :
- In addition to the format conditions, you can define a default format condition (type
FCO_Default
) that is used if no matching format condition could be found. For default format conditions, the Value1
pointer can be NULL
. - The columns can be defined with a static width, or a dynamic width. The dynamic width is calculated by the function
CalcDropdownListWidth()
. So after changes or adding rows this function should be called. - Not all format condition operators make sense for every type of value (
Between
makes no sense with strings, Contains
makes no sense with numeric values), so handle with care...
Using the Code
- In your resource editor, create a combobox and set the owner draw property to
Variable
, the Include strings property to true
and the dropdown type to DropDownList
. - Use the classwizard to create a
CComboBox
object in your window. - Include the MCComboBox.h file in your class file.
- Rename the
CComboBox
object type to CMCComboBox
. - In the
OnInitDialog()
and OnInitialUpdate()
you must define the columns. Format conditions and the row data can be added later in the code.
Here's the code snippet to produces the fantastic dropdown list you can see at the top of the article. The code creates a multicolumn combobox with 3 columns, 3 format conditions, one default format condition and 30 rows of data :
this->m_cboMultiColumn.Add_Column( );
this->m_cboMultiColumn.Add_Column( );
this->m_cboMultiColumn.Add_Column( );
this->m_cboMultiColumn.Set_ShowFormatConditionInEditField( true );
CFormatCondition *pFormatCondition =
this->m_cboMultiColumn.Create_FormatCondition( _T("TestCondition1"), 0,
CFormatCondition::FCO_Equal, new CMCCell( CString(_T("Row 3" ) ) ) );
if ( pFormatCondition != NULL )
{
pFormatCondition->Set_Bold( true );
pFormatCondition->Set_TextColor( RGB( 255, 0, 0 ) );
pFormatCondition->Set_BackColor( RGB( 0, 0, 0 ) );
}
pFormatCondition = this->m_cboMultiColumn.Create_FormatCondition
( _T("Date 1"), 1, CFormatCondition::FCO_Equal,
new CMCCell( COleDateTime(2007, 2, 5, 12,0,0 ), CMCCell::CT_Time ) );
if ( pFormatCondition != NULL )
{
pFormatCondition->Set_TextColor( RGB( 0, 255, 0 ) );
}
pFormatCondition = this->m_cboMultiColumn.Create_FormatCondition
( _T("Date 2"), 2, CFormatCondition::FCO_GreaterThan, new CMCCell( (int)4 ) );
if ( pFormatCondition != NULL )
{
pFormatCondition->Set_TextColor( RGB( 0, 0, 255 ) );
}
pFormatCondition = this->m_cboMultiColumn.Create_FormatCondition
( _T("Default"), 2, CFormatCondition::FCO_Default, new CMCCell( (int)0 ) );
if ( pFormatCondition != NULL )
{
pFormatCondition->Set_TextColor( RGB( 255, 192, 64 ) );
pFormatCondition->Set_BackColor( RGB( 128, 0, 0 ) );
pFormatCondition->Set_Italic( true );
pFormatCondition->Set_StrikeThru( true );
}
COleDateTime dtDateTime( 2007, 2,5,10,0,0);
for ( int nIndex = 0; nIndex < 30; nIndex++ )
{
CMCRow *pRow = new CMCRow( );
if ( pRow )
{
pRow->Set_ItemData( (long)( nIndex + 1 ) );
CString strText;
strText.Format( _T("Row %d"), nIndex, nIndex );
pRow->Set_CellValue( 0, strText );
pRow->Set_CellValue( 1, dtDateTime, CMCCell::CT_Time );
pRow->Set_CellValue( 2, nIndex );
this->m_cboMultiColumn.Add_Row( pRow );
}
dtDateTime += COleDateTimeSpan( 0, 1, 0, 0 );
}
this->m_cboMultiColumn.CalcDropdownListWidth( );
That's it. Have fun with it.