CodeExplorer Posted June 16, 2018 Posted June 16, 2018 Remove listbox content Visual C++ ??? I wanna remove all items from listbox, Already tried ResetContent but it doesn't work: CWnd *pWnd = GetDlgItem(IDC_LIST1); CListBox* listbox = static_cast<CListBox*>(pWnd); //dynamic_cast does not work listbox->ResetContent();
atom0s Posted June 16, 2018 Posted June 16, 2018 If you are using MFC you don't use GetDlgItem API, you use the parent dialog as the owner to get its child with its sub-call for GetDlgItem, such as: CListBox* pListBox = pDialog->GetDlgItem(IDC_LIST1); Where pDialog is the pointer to your dialog object.
CodeExplorer Posted June 17, 2018 Author Posted June 17, 2018 @atom0s: GetDlgItem returns 'class CWnd *' or HWND and not CListBox*, so your code is wrong and won't work. The first part code works ok, the listbox->ResetContent(); is the one which doesn't make what I want!
CodeExplorer Posted June 17, 2018 Author Posted June 17, 2018 Found a solution, is not very elegant but it works: CWnd *pWnd = GetDlgItem(IDC_LIST1); CListCtrl* clist = static_cast<CListCtrl*>(pWnd); // CListCtrl* clist = (CListCtrl*)(pWnd); int numOfItems = clist->GetItemCount(); for (int i = numOfItems - 1; i >= 0; i--) clist->DeleteItem(i);
atom0s Posted June 18, 2018 Posted June 18, 2018 You're not using GetDlgItem the way I mentioned which is why you think its wrong.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now