Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ChoiceListButton Control

0.00/5 (No votes)
21 Feb 2000 1  
A dropdown menu button with checkbox menu items
  • Download demo project - 26 Kb

    sample image

    Synopsis

    I wanted to have a button that would display a selection of choices to the user. I could of course use an ownerdraw combo box. The problem with that solution, is that, when the dropdown is collapsed it will display the last chosen item always insted of some descriptive text.

    That was why I opted for a custom solution.

    ITCLib

    Optionally, the control can use the freeware library 'ITCLib' from Interface technologies, Inc. This is a great library and I will suggest that every MFC developer takes a look at it - it is a real time saver.

    ITCLib has only been used for painting the small arrow on the button, and is as such not an integreated part of the control. That is why all references to ITCLib is initially disabled via preprocessor statements, so the sample should compile fine on installations without the library.

    To enable ITCLib:

    • If you have ITCLib installed and you want to enable it, do the following:
    • In Project Settings - C/C++ - Preprocessor - Preprocessor definitions, add _ITCDLL
    • In Project Settings - C/C++ - Preprocessor - Additional include directories, add the folder with your ITCLib header files
    • In Project Settings - Link - Input - Additional Library Path, add the folder with your ITCLib lib-files

    That's it!

    If you want to use you own button class for painting the arrow or what ever it is you want to paint on the button surface, you could enherit CChoiceWindowBtn from your own class instead of CButton or ITCImageButton.

    Using the control

    To use the control, create a project and add these files:

    ChoiceWindow.h
    ChoiceWindow.cpp
    ChoiceWindowBtn.h
    ChoiceWindowBtn.cpp
    drop_arrow.bmp (or another bitmap)

    Assign the bitmap the resource name "DROP_ARROW" - notice as  a string - not as an integer ID.

    Next add a button to a dialog and use the member functions from you WM_INITDIALOG handler.

    Add an object of type CChoiceWindowBtn to the dialog class. Make sure to add the header include too.

    Example - notice the use of preprocessor definitions.

    #ifdef _ITCDLL
    	m_btnDropDown.SubclassControl(IDC_OPTIONS, this);
    	m_btnDropDown.AddImage("DROP_ARROW", -1, RGB(255,0,255));
    	m_btnDropDown.SetImageAlign(DT_RIGHT|DT_VCENTER);
    #else
    	m_btnDropDown.SubclassDlgItem(IDC_OPTIONS, this);
    #endif
    
    	m_btnDropDown.AddChoice("Name", TRUE);
    	m_btnDropDown.AddChoice("Address", TRUE);
    	m_btnDropDown.AddChoice("Notes", FALSE);
    	m_btnDropDown.AddChoice("Telephone", TRUE);
    

    The API

    The control is accessed through the class CChoiceWindowButton. It has a few simple member functions:

    CChoiceWindowButton::AddChoice

    void AddChoice(LPCTSTR szText, BOOL bChecked=FALSE)

    Return value
    None
    Parameters
    szText: The text to display as a choice.
    bChecked: Should the choice be iniitally checked or not.
    Remarks
    Adds a new choice to the list.

     

    CChoiceWindowButton::Reset

    void Reset()

    Return value
    None
    Parameters
    None
    Remarks
    Deletes all the items from the choice list.

    CChoiceWindowButton::GetCheck

    BOOL GetCheck(int iItem)

    Return value
    Boolean - the state of the choice specified by iItem
    Parameters
    nItem: Zero based index of the item to query the state for.
    Remarks
    Returns TRUE if the choice had a check mark, and FALSE if the choice was not checked by the user.

     

    CChoiceWindowButton::SetCheck

    void SetCheck(int iItem, BOOL bCheck=TRUE)

    Return value
    void
    Parameters
    iItem: Zero based index of the item to query the state for.
    bCheck: Boolean value. If true, the item is checked otherwise it is unchecked.

    CChoiceWindowButton::GetItemCount

    int  GetItemCount()

    Return value
    The number of items in the list.
    Parameters
    none

     

    CChoiceWindowButton::GetListCtrl

    CListCtrl&  GetListCtrl()

    Return value
    A reference to the underlying CListCtrl object 
    Parameters
    none
    Remarks
    You access the list control like this:
    CListCtrl& list = btnChoiceList.GetListCtrl();

     

    CChoiceWindowButton::RemoveAt

    void  RemoveAt(int iIndex)

    Return value
    None
    Parameters
    The index of the item to remove from the list.

     

    CChoiceWindowButton::m_bDisableIfEmpty

    BOOL m_bDisableIfEmpty

    Remarks
    Boolean value, that specifies if the button is disbaled if the choice list does not contain any items. The default value is TRUE.

     

    CChoiceWindowButton::Reset

    void Reset()

    Return value
    None
    Parameters
    None
    Remarks
    Removes all the items from the list.


    CChoiceWindowButton::SetItemData

    void SetItemData(int iIndex, DWORD dwData)

    Return value
    None
    Parameters
    iIndex - Index of the item to associate the data with,
    dwData - A 32 bit value to associate with the item. This may be zero.
    Remarks
    Associates a 32 bit value with an item.


    CChoiceWindowButton::GetItemData

    DWORD SetItemData(int iIndex)

    Return value
    DWORD - The 32 bit value associated with the item.
    Parameters
    iIndex - Index of the  item to associate the data with,
    Remarks
    Retrieves the 32 bit value associated with an item.

    History

    July 1999
    Version 1.0 was released at CodeGuru as "ChoiceListButton".

    August 1999
    Minor bugfix to the class was posted at CodeGuru.com.

    15 Dec 1999
    Posted at CodeProject.com.

    February 2000
    Version 1.0.2 posted at CodeProject.com. Added SetItemData() and GetItemData() member functions.

  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here