Creating a Group Combo Box

   

Environment: VC6.0, W2K

The purpose of this article is to demonstrate a sample class to "group" the MFC CComboBox class. The grouping allows the user to make an assignment from a set of values.

For example, if a user wanted to map several IP addresses in a one-to-one relationship, the software could show all the available IP addresses, and allow the user to select the mapping. When an IP address is selected, that IP address is removed from the other combo boxes in the list. When an IP address is unselected, the address is added to all the other combo boxes in the list.

In the Demo Project (provided below and shown above), variables one through five are mapped to choices one through six. Obviously, there are more choices than variables, but only the "Not Selected" choice can be selected in multiple combo boxes at any time. When the OK button is pressed, the values of the choices are reported to the user in a message box.

Also, while this class is relatively simple, I am new at developing Windows software, and would appreciate any constructive feedback. If you notice anything that I could have done better, or any errors, PLEASE send me an e-mail to let me know your thoughts.

Downloads

Download demo project - 45 Kb
Download source - 5 Kb


Comments

  • How we can add an string to list box item of combobox by code?

    Posted by Legacy on 05/01/2003 12:00am

    Originally posted by: zeinalzadeh

    How we can add an string to list box item of combobox by code.While I exit from program that string value be saved on list box data of combobox?

    Reply
  • Better way

    Posted by Legacy on 04/07/2003 12:00am

    Originally posted by: Alex

    Why not use a listcontrol and override pretranslate message to handle Ctrl & shift arrows to re-order values to top/bottom and going up down. It'll look a lot neater. (I have done this, here's a hint...)
    
    

    BOOL CMyDialog::PreTranslateMessage( MSG* pMsg )
    {
    if(pMsg)
    {
    // Only bother with messages aimed at the list window
    if(pMsg->hwnd == m_sListCtrl.m_hWnd)
    {
    // Got to be a key down event
    if(pMsg->message == WM_KEYDOWN)
    {
    // Must be either key up or key down
    switch(pMsg->wParam)
    {
    case VK_UP:
    case VK_DOWN:
    {
    // Only do something if the shift or CTRL key are pressed
    BOOL bShift = GetAsyncKeyState(VK_SHIFT);
    BOOL bControl = GetAsyncKeyState(VK_CONTROL);
    if(!bShift && !bControl)
    {
    break;
    }

    // UP key?
    if(pMsg->wParam == VK_UP)
    {
    if(bShift)
    {
    // SHIFT VK_UP
    // Move up one place
    OnMoveUp();
    }
    else //if(bControl)
    {
    // CTRL VK_UP
    // Move to top
    OnMoveToTop();
    }
    }
    // Otherwise DOWN
    // (because we checked it was an up or a down earlier)
    else
    {
    if(bShift)
    {
    // SHIFT VK_DOWN
    // Move down one place
    OnMoveDown();
    }
    else //if(bControl)
    {
    // CTRL VK_DOWN
    // Move to bottom
    OnMoveToBottom();
    }
    }

    // Doing it here rather than passing this key on to the list window
    // So return TRUE to say we've processed it
    return TRUE;
    }
    break;
    default:
    break;
    }
    }
    }
    }

    return FALSE;
    }

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds