简体   繁体   中英

How to hide the first column of a wxListCtrl in wxWidgets?

The context

In a wxWidgets (version 3.0.2) C++ application, I am trying to hide the first column of a wxListCtrl.

I did not find a member function to do this so I tried to set the width of the column to 0:

myListCtrl->SetColumnWidth(0, 0);

first argument being the column ID and second one the width in pixels ( wxListCtrl documentation ).

After running the program, the header of the first column is hidden as I wanted but the data of each row of the first column overlaps the data of each row of the second column (which is not hidden). It is obviously not what I want. The header and the data of the first column should be hidden.

The question

In wxWidgets 3.0.2, is there a way to hide the first column (header and data of each rows) of a wxListCtrl?

I don't believe you can. You have a few options.

  1. Delete the column using DeleteColumn(int columnIndex) . You aren't losing any data, just the display of it, so you can always re-insert the column and repopulate it if you need to re-add it. Obviously this could be time consuming if your data is excessively large.
  2. Depending on your application, just don't create the column in the first place. You don't say why you want to hide it, so if you just don't want it, don't add it.
  3. Implement your control as a virtual control which gives your application control over what to display where. The burden of data display management falls to you to do manually but you have a great deal more flexibility. Inherit the class with wxLC_VIRTUAL style and implement OnGetItemText http://docs.wxwidgets.org/3.0/classwx_list_ctrl.html#a92370967f97215e6068326645ee76624

Edit : To expand on the comment question, how to get the selected item index:

The wxListCtrl is a little weird when it comes to selected items. I'm sure it has to do with needing to support report, icon, etc. different views. When dealing with a multi-column report mode, you might find that you can only select items in the first column. If you are on Windows, it should automatically be set to "Full Row Select" but I don't know about other OSs.

Anyway, here is a utility method that returns the first selected item (note that you can support multi-selection if you want to).

//Get the item currently selected
int ListView::GetItemSelected() const
{
    for(int i=0; i<GetItemCount(); ++i)
        if (GetItemState(i, wxLIST_STATE_SELECTED) == wxLIST_STATE_SELECTED)
            return i;
    return -1;
}

If you want (and it makes sense), you can connect the list item selected event.

this->Connect(wxEVT_COMMAND_LIST_ITEM_SELECTED, wxCommandEventHandler(ListView::selected_Changed), NULL, this);

and within that event handler, get the selected item and do what needs doing (depending entirely on your application).

You will note that I'm using a derived class here which just makes things a lot easier but you don't have to. You can connect to something like MyMainForm::sqlResults_selectedChanged or whatever.

There is more than one way to accomplish all this and you can also find some good suggestions and help here: https://wiki.wxwidgets.org/WxListCtrl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM