-
April 19th, 2013, 07:08 PM
#1
Disable CCheckListBox items without access to code that calls AddString
I need to be able to disable the items on a CCheckListBox but I can't change the code that calls AddString. I know I can use the CCheckListBox::Enable function to disable an item if I have an index but I don't have the index which would be provided by AddString. I've tried intercepting the LB_ADDSTRING message and then looping through the items in the control but the string has not been added at this point so the last item in the list is never disabled. I used Spy++ to see what messages were being sent and I noticed that LB_GETTEXT was sent so I tried intercepting this message (ugly hack) but this caused my app to hang - I assume because of the number of times the message is sent. Is there a way to disable the items?
-
April 19th, 2013, 08:54 PM
#2
Re: Disable CCheckListBox items without access to code that calls AddString
How do you know which item you want to disable if you don't know its index? You want to disable all items? Just set up a for loop.
-
April 19th, 2013, 09:01 PM
#3
Re: Disable CCheckListBox items without access to code that calls AddString
Correct, I want to disable all items. I already tried setting up a for loop when I intercept the LB_ADDSTRING message but the item hasn't been added to the list at this point so it isn't included in the loop and isn't disabled.
-
April 20th, 2013, 03:19 AM
#4
Re: Disable CCheckListBox items without access to code that calls AddString
Originally Posted by mjh75
I already tried setting up a for loop when I intercept the LB_ADDSTRING message but the item hasn't been added to the list at this point so it isn't included in the loop and isn't disabled.
Could you show your code?
Victor Nijegorodov
-
April 20th, 2013, 07:14 AM
#5
Re: Disable CCheckListBox items without access to code that calls AddString
How are these strings getting added to the list box if not through your code?
-
April 24th, 2013, 01:38 PM
#6
Re: Disable CCheckListBox items without access to code that calls AddString
Apologies for the delay in responding...
The strings are added in my code but I want to create a generic solution that can be used by all dialogs to reduce maintenance and risk of coding error.
I have a hook that receives the messages and then passes them to this function. CMyDialog is the base class of the dialog which contains the CCheckListBox. I've verified that the LB_ADDSTRING message is received in this function. I've left out some of the code between the hook and this function to make this easier to show. I've verified that the part that is left out is working correctly.
Code:
void CMyDialog::HandleMessage(CWPSTRUCT *pCWP)
{
if(!pCWP)
return;
if(pCWP->message == LB_ADDSTRING)
{
CWnd *pWnd = CWnd::FromHandle(pCWP->hwnd);
if(pWnd->GetRuntimeClass() == RUNTIME_CLASS(CCheckListBox))
{
CCheckListBox *pCB = (CCheckListBox*)pWnd;
for(int nIndex = 0; nIndex < pCB->GetCount(); nIndex++)
{
pCB->Enable(nIndex, FALSE);
}
}
}
}
-
April 24th, 2013, 01:42 PM
#7
Re: Disable CCheckListBox items without access to code that calls AddString
It's not clear what you are trying to do. Are you only trying to prevent items from getting added to a control? Are you trying to disable other items while you are adding a new item? Something else?
Also, what is causing the AddString? Is it a button click? If so, can you get the handle to the button and disable it?
-
April 24th, 2013, 01:52 PM
#8
Re: Disable CCheckListBox items without access to code that calls AddString
Whatever it is you're trying to do isn't clear, but it sure seems like you're going about it the wrong way. Why not derive a new class from CCheckListBox and do whatever processing you want in there?
-
April 24th, 2013, 02:08 PM
#9
Re: Disable CCheckListBox items without access to code that calls AddString
I'm trying to disable all items in the control. I have multiple dialogs in my application and I need to provide a generic solution that will disable all items in any Arjay:
CCheckListBox in any of these dialogs based on a condition. So sometimes they need to be disabled and sometimes they need to be enabled. I don't want to write code to disable the items in each dialog because 1) I'll have to do this in a large number of dialogs; 2) it will increase the risk of someone breaking the functionality; 3) it will have to be added to dialogs that are created in the future. If I put this behavior in the base class it will be inherited by all existing dialogs and future dialogs (assuming they're derived), there will be no maintenance effort, and there will be a consistent solution instead of different for each dialog.
I cannot rely on the action that causes the AddString because it will be different in each dialog.
GCDEF:
I tried deriving a new class from CChecklistBox but couldn't figure out what to override. AddString is not in the list of overrides. Deriving a new class would also require a change in each dialog which I'm trying to avoid (though a small change that I'm willing to make at this point).
-
April 24th, 2013, 02:18 PM
#10
Re: Disable CCheckListBox items without access to code that calls AddString
Originally Posted by mjh75
I tried deriving a new class from CChecklistBox but couldn't figure out what to override.
Just implement a new method in the derived class that will disable all the items in the control.
Then use your derived class instead of the CChecklistBox everywhere in your application/project.
Now if you'd like to disable all items in some of the controls you would only need to call the implemented method for disable all items!
Edit: I forgot to add (presuming it is obvious) that you would have to add control member variables of the implemented derived class for all the controls you would enable/disable)
Last edited by VictorN; April 24th, 2013 at 02:21 PM.
Victor Nijegorodov
-
April 24th, 2013, 02:21 PM
#11
Re: Disable CCheckListBox items without access to code that calls AddString
Just add an LB_ADDSTRING handler to your derived class.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|