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

  • A help desk is critical to the operations of an IT services business. As a centralized intake location for technical issues, it allows for a responsive and timely solution to get clients and their staff back to business as usual. In addition to handling immediate IT issues, a help desk performs several proactive tasks to ensure clients' IT systems remain operational and downtime is minimized. Thus, utilizing a help desk and following best practices can improve the productivity, efficiency and satisfaction of …

  • It's time high-level executives and IT compliance officers recognize and acknowledge the danger of malicious insiders, an increased attack surface and the potential for breaches caused by employee error or negligence. See why there is extra emphasis on insider threats.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds