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