Click to See Complete Forum and Search --> : CCheckListBox!!


vittal
March 30th, 1999, 04:22 AM
Hi,

I have a cheklistbox created in my application. I have created using the CListBox and setting the OwnerDraw as TRUE. I

am adding a few strings to the checklistbox and trying to delete them if they are UNCHECKED.

PROBLEM!!!

But it is not deleteing all the strings and the output is not the predicted one.

The code I am using for deletion is....

////////////////////CODE////////////

CCheckListBox *clb = (CCheckListBox *)GetDlgItem(IDC_PEAKLIST);

int NumofItems;

NumofItems = clb->GetCount();

for (int i=0;i<NumofItems;i++)

{

int Checked;

int num;

Checked = clb->GetCheck(i);

if (Checked == 0)

{

num = clb->DeleteString(i);

if (num = LB_ERR)

AfxMessageBox("Error deleteing string!!");

}

}

////////////////////////////////////////////////////////////

Is it a known bug or a problem?

Please help me to solve the problem.....


Thanks in advance........

Vittal

Reshmi
March 30th, 1999, 04:47 AM
Hi,


The code you have written seems to be correct. But how did you create CCheckListBox. Did you create a CListBox using controls and used it as a place holder for Checklistbox. If this is the case you should get the desired result. If not explain how you have created ChecklistBox.


Reshmi

vittal
March 30th, 1999, 05:09 AM
Hi Thanks for the early reply.

* I Added a ListBox into by dialog box using the dialog editor.

* Set the OwnerDraw of the control as TRUE

* Added a Control Variable using the Class Wizard.

* Changed the CListBox to CCheckListBox in the constructor code...

i.e.,

The code is

class mydialog : public CDialog

{

// Construction

public:

mydialog(CWnd* pParent = NULL); // standard constructor

// Dialog Data

//{{AFX_DATA(mydialog)

enum { IDD = IDD_DIALOG1 };

CCheckListBox m_control; ///************CCheckListBox instead of CListBox*********/

//}}AFX_DATA

}

Is this the right way? (It should be becos all other operations are working fine)

Is there anyother way of doing it.


Please help me ASAP...


Thanks in Advance

Vittal

Reshmi
March 30th, 1999, 05:41 AM
Hi,

Just check if the following code works for u

CCheckListBox *clb = (CCheckListBox *)GetDlgItem(IDC_LIST1);

int NumofItems;

NumofItems = clb->GetCount();

for (int i=0;i<NumofItems;i++)

{

int Checked;

int num;

Checked = clb->GetCheck(i);

if (Checked == 0)

{

num = clb->DeleteString(i);

if (num == LB_ERR)

AfxMessageBox("Error deleteing string!!");

else

{

NumofItems = clb->GetCount();

i-=1;

}





}


}


If this works please let me know


Reshmi

Martin Speiser
March 30th, 1999, 07:33 AM
Hi Reshmi,


the code is not correct. If an item in the control is deleted, the item count is decremented by the control, but the loop counter is incremented. In this case the counter doesn't point to the next item, but to the one behind.


There are several ways to prevent this. First, count backwards, like "for ( int i = nNumOfItems - 1; i >= 0; --i )". Second way is *not* to increment the counter after deleting an item, but to decrement nNumOfItems. Third way, the most error proof, get all items of the CCheckListBox in memory, for instance an array or better a list, make the neccessary modifications, erase all items in the control, and insert the items in the array or list. I prefer the third way, because the maintenance is better in future versions.


Martin

vittal
March 30th, 1999, 08:15 AM
Hi Reshmi,


Thanks a lot. It is working fine.....

But what do you say about Martin's opinion.


Anyway thanx for your help...

Vittal

Reshmi
March 30th, 1999, 10:17 PM
Hi ,

You feel that the code which I corrected and posted is also not correct?

After posting my first response only I found out that his loop is not correct.

Then I corrected the loop . Do u mean to say that that code is also not correct?

I checked it with a sample application and it was working fine with all cases.


Thanks

Reshmi

Martin Speiser
March 31st, 1999, 02:00 AM
Hi Reshmi,


looks like I was to fast with my answer. No, the code in your second reply is correct. I would prefer a while-loop in this case, but it should work.


Martin