Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / MFC
Article

Multicolumn Combobox with Additional Format Conditions

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
9 May 2007CPOL2 min read 78.6K   5.3K   39   9
Multicolumn Combobox with additional format conditions depending on the displayed values
Screenshot - MCComboBox_pic.jpg

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 :

C++
//    Adding the columns
this->m_cboMultiColumn.Add_Column( );
this->m_cboMultiColumn.Add_Column( );
this->m_cboMultiColumn.Add_Column( );

//    also use the format conditions in the edit control
this->m_cboMultiColumn.Set_ShowFormatConditionInEditField( true );

//    add the format conditions
//    red text and black background, if in column 0 the text "Row 3" appears
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 ) );
}

//    green text if in column 1 a date/time equal to 05.02.2007 12:00:00 appears
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 ) );
}

//    blue text if in column 1 a date/time greater than 05.02.2007 12:00:00 appears
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 ) );
}

//    default format condition with golden text on dark red background, 
//    italic and striked out (uhhhhh.....)
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 );
}

//    add 30 rows of data...
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 );
}
//    recalc the needed width for the dropdown list
this->m_cboMultiColumn.CalcDropdownListWidth( ); 

That's it. Have fun with it.

License

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


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow SORT by first COLUMN ??? Pin
rodnarox16-Jul-08 23:23
rodnarox16-Jul-08 23:23 
AnswerRe: How SORT by first COLUMN ??? Pin
rodnarox27-Aug-08 0:31
rodnarox27-Aug-08 0:31 
GeneralThank YOU Pin
rodnarox16-Jul-08 22:57
rodnarox16-Jul-08 22:57 
QuestionUsing MCComboBox Control With C# Pin
jasper01813-Jul-07 10:14
jasper01813-Jul-07 10:14 
AnswerRe: Using MCComboBox Control With C# Pin
Gismow15-Jul-07 20:10
Gismow15-Jul-07 20:10 
Hi,

I'm sorry, but the actual code is implemented as C** MFC Control and it's - as I know - not usable in C# resp. .NET.

But I'm thinking about to migrate it to C#.

Kind Regards
gismow
GeneralRe: Using MCComboBox Control With C# Pin
jasper01818-Jul-07 9:19
jasper01818-Jul-07 9:19 
GeneralSuggestion Pin
Hans Dietrich9-May-07 19:40
mentorHans Dietrich9-May-07 19:40 
AnswerRe: Suggestion Pin
Gismow9-May-07 20:29
Gismow9-May-07 20:29 
GeneralRe: Suggestion Pin
Hans Dietrich10-May-07 20:53
mentorHans Dietrich10-May-07 20:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.