Make clistbox with check boxes in VC

Source: Internet
Author: User

You must use a list control with a check box in the project ...... But because the project is a VC project, there are no check boxes in the existing clistbox or clistctrl in VC. Speaking of this, experts may say that, in simple ways, they can redraw one by themselves or be a little lazy. They can also find someone else on the Internet to use the ready-made classes.

In fact, there is another easier way to find this article on the Internet:

[How to Use the cchecklistbox class in a dialog box]

Create in your resource file an ordinary list box in a dialog box. whichever other attributes that you choose, the list box must be ownerdrawn and include the hasstrings attribute. assume that in this case, you have assigned an ID of idc_checklistbox to
ListBox.

Create an instance of a cchecklistbox object in the header file of your dialog box.

CCheckListBox m_CheckListBox;

Over-ride oninitdialog () in your dialog box and subclass the list box that you created in your resource file. set the style of the check box as you wish. see the documentation provided with your compiler.

m_CheckListBox.SubclassDlgItem(IDC_CHECKLISTBOX, this);m_CheckListBox.SetCheckStyle(BS_AUTOCHECKBOX);

Since cchecklistbox is derived from clistbox, you have access to all the class members of clistbox. Add strings to the list box using addstring () as follows.

m_CheckListBox.AddString("Test String");

Cchecklistbox provides several Class Members that allow you to access or set the check state of the items in the list box. for example, to see whether the first item in the list box is checked (the index of the first item wocould be zero), use getcheck ().

int iIndex = 0;int iState;iState = m_CheckListBox.GetCheck(iIndex);

The method is clever.

MFC has a cchecklistbox class that supports the checkbox style, so we can directly use the ListBox control, and then subclass it into cchecklistbox during initialization, and then set the style parameter. Note that the key to success is to modify the two attributes of The ListBox control, namely, the owner draw is set to fixed (lbs_ownerdrawfixed) and the has strings is set to true (lbs_hasstrings ), otherwise, the operation fails and an error is returned.

The specific implementation steps are as follows --

1. Drag and Drop a ListBox control in the window. Assume that the resource ID is idc_list1;

2. Modify the attributes of The ListBox control (lbs_ownerdrawfixed | lbs_hasstrings );

3. Define the cchecklistbox object in the header file of the window class;

// XXXDlg.hCCheckListBox m_CheckList;

4. Write down two lines in the CPP file at the initialization point;

// Xxxdlg. cppbool cxxxdlg: oninitdialog (){//... m_checklist.subclassdlgitem (idc_list1, this); // idc_list1 is the resource ID m_checklist.setcheckstyle (bs_autocheckbox) of The ListBox control );//...}

OK, that's easy! Add a few pieces of data to see if there is a check box!

m_CheckListBox.AddString("Test String");

It is easy to determine whether the check box is selected, as shown below:

int iIndex = 0;int iState;iState = m_CheckListBox.GetCheck(iIndex);

The above tests passed in vc6 and 8.

 

Supplement: The ttreeview control with a check box in Delphi --

It's easy to add this sentence during initialization. (Treeview1 is the control name)

SetWindowLong(TreeView1.Handle, GWL_STYLE, GetWindowLong(TreeView1.Handle, GWL_STYLE) or $00000100);

You don't need to find any third-party controls or customize them!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.