CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2013
    Posts
    9

    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?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  3. #3
    Join Date
    Apr 2013
    Posts
    9

    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.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Disable CCheckListBox items without access to code that calls AddString

    Quote Originally Posted by mjh75 View Post
    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

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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?

  6. #6
    Join Date
    Apr 2013
    Posts
    9

    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);
    			}
    		}
    	}
    }

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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?

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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?

  9. #9
    Join Date
    Apr 2013
    Posts
    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).

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Disable CCheckListBox items without access to code that calls AddString

    Quote Originally Posted by mjh75 View Post
    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

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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
  •  





Click Here to Expand Forum to Full Width

Featured