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 …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds