How to select the items in a wxListCtrl with matching text Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Nico
Knows some wx things
Knows some wx things
Posts: 31
Joined: Sun Nov 07, 2010 5:19 pm

How to select the items in a wxListCtrl with matching text

Post by Nico »

Hello,

I have a wxListCtrl object containing a number of items all of which are displayed as a wxString. The wxListCtrl object is named "VarsListCtrl" and contains only one column (column 0).

Now I would like to create a method that will set those items in the list which match a given wxString exactly to be "selected". However, I seem to have a problem getting the text from an item.

Using Google I found some sites that discuss (slightly) related topics and using those as examples I tried to do it like this:

Code: Select all

void MyClass::AutoSelectParameter(wxString parameterLabel)
{
	long idx = -1;
	int savedState;
	wxString itemText;

	// Loop over all items
	for ( idx = ( VarsListCtrl->GetItemCount() - 1 ); 0 <= idx; idx-- )
	{
	    // created a local wxListItem for attempt 2 and 3. 
	    wxListItem item;                // this was commented out when I tried it with GetItemText(idx);
	    item.SetId(idx);                // this was commented out when I tried it with GetItemText(idx);
	    item.SetColumn(0);              // this was commented out when I tried it with GetItemText(idx);
	    item.SetMask(wxLIST_MASK_TEXT); // this was commented out when I tried it with GetItemText(idx);
	    VarsListCtrl->GetItem(item);    // this was commented out when I tried it with GetItemText(idx);
	    // Save the itemtext
	    // attempt 3 results in an empty string
	    itemText = item.m_text;

	    // attempt 2 resulted in an empty string
	    // itemText = item.GetText();

	    // attempt 1 resulted in an empty string
	    // itemText = VarsListCtrl->GetItemText(idx);

        if (itemText.compare(parameterLabel)==0)
        {
            VarsListCtrl->SetItemState(idx,wxLIST_STATE_SELECTED,wxLIST_STATE_SELECTED);
        }
	}	
}
But "itemText" remains an empty string, where "parameterLabel" contains the proper text, so the comparison never matches.

Also if it would match I'm not sure if this would indeed set the correct item to be selected. As I would like to call this function a couple of times to select items with different Strings it is also important that an item that is selected will remain selected whether it matches the gieven String or not.

Is there anybody who knows what I should change to get this to work?
Thanks in advance for any and all help.

Kind regards, Nico

p.s. As I am not sure if it is important I'd better mention that I am trying to do this before anything becomes visible.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to select the items in a wxListCtrl with matching te

Post by doublemax »

You need to set the id, column and mask, so your code look ok to me. Is this a wxListCtrl in report mode and not virtual?

Check the return value of the GetItem() call. If it returns false, you could try tracing into the call with a debugger and see why.
p.s. As I am not sure if it is important I'd better mention that I am trying to do this before anything becomes visible.
That shouldn't matter, but you should at least try calling the code after the control is visible and see if it makes any difference.
Use the source, Luke!
Nico
Knows some wx things
Knows some wx things
Posts: 31
Joined: Sun Nov 07, 2010 5:19 pm

Re: How to select the items in a wxListCtrl with matching te

Post by Nico »

While I probably should know I am not sure about the mode. I did not intentionally declare it as virtual and don't believe I overwriten "OnGetItemText", but neither did I specify that the report mode should be used. Could you tell me how to enforce this, so I can rule this cause out?

Kind regards, Nico
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to select the items in a wxListCtrl with matching te

Post by doublemax »

Apparently your control is not in "virtual" mode. You'd have to set the wxLC_REPORT flag on creation and override OnGetItemText.
Use the source, Luke!
Nico
Knows some wx things
Knows some wx things
Posts: 31
Joined: Sun Nov 07, 2010 5:19 pm

Re: How to select the items in a wxListCtrl with matching te

Post by Nico »

Got it.

Like you suggested I also tried to do it after the ListCtrl was made visible and then it does work. This means it is a problem with the initialization rather then the code I used to select an item.

Thanks a lot for your help.

Kind regards, Nico
Post Reply