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

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds