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: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Open source and cloud computing have long promised cost efficiencies, yet many organizations have shied away from these technologies due to security and reliability concerns. Now, open source has proven itself stable, and the cloud has become as secure—or even more secure—than on-premises implementations. Read this white paper to learn how you can get the accessibility of open source and the flexibility and affordability of cloud computing combined in a compelling storage option for companies …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds