(MFC) CListBox -> Edit Item in List? - c++

I'm currently using the CListBox Control for MFC. Is it possible to retain the data inside the List Item but edit it, without deleting then re-adding it?
Thanks!

You can do this by extending CListBox. Check out this code project article.

There's no way to do this, unfortunately.
Even the native list box class doesn't provide any way to edit an entry, without removing and inserting.
The best you can do is extending the CListBox class, either by sub-classing or deriving from it.

The best way (if possible, sure), is replacing CListBox with CListCtrl

Related

How can I sort Items in `CListCtrl` when the button is clicked?

How can I sort Items in CListCtrl when the button is clicked?
I made a Dialog based application in MFC.
I put CListCtrl control on my Dialog and set its view style to report type.
I have two columns in this list view.
In here when I clicked the "Sorting" button, it should sort item in the list.
I saw many examples related to this, but none is working for me.
Can some one guide me how to do that?
I assume that you mean the column header when you write "sorting button".
You probably forgot to put this into the message map of your dialog:
ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST1, OnColumnclickList1)
IDC_LIST1 is the id of the list control which may be different in your code.
Are you somewhat familiar with MFC ? If not, you should follow one of the numerous tutorials available on the net.
I would use a CMFCListCtrl. It is easier, as it as built in sorting functionality and you only have to provide a function with the comparing items algorithm for sorting. I have succeeded using it. You have to override the OnCompareItems on your class derived from CMFCListCtrl. I also recommend you to do a call to EnableMarkSortedColumn(); after your list is created.
The CListCtrl has a SortItems method.
All you need to do is provide your Compare function to it as the parameter.
I don't know much about the contents of your list control so I can't really give you more information.
Please read up on it and extend your question with example code and details of which column needs to be sorted and I can help more.
If the contents of your cells are just text then I usually just return the value of:
return Value1.CollateNoCase(Value2);
There are also lots of tutorials on the Internet. For example:
http://www.codeproject.com/Articles/27774/CListCtrl-and-sorting-rows
In fact, that tutorial suggests us in SortItemsEx. That is what I would use.

Is there a way to add my own button inside the edit common control?

Say, if I have a default EDIT common control in my MFC-based dialog window:
I'm looking for a way to add a small "X" (or delete) button inside of it (here's my Photoshop rendering of what I need):
Is there a way to do it by modifying the default edit control?
Please consider using new class CMFCEditBrowseCtrl. It does have method CMFCEditBrowseCtrl::EnableBrowseButton() to do exactly what you need.
If I wanted more than one button, I would investigate alternatives:
See the CMFCEditBrowseCtrl class' code. Then decide if derive a class from it and extend; or else derive from CEdit, copy CMFCEditBrowseCtrl code and extend.
Case the edit is multi-line, I would investigate the methods CEdit::SetRect and CEdit::SetRectNP. Case it is single-line I would look to CEdit::SetMargins. Then implement normal buttons over the text area of the edit.
Please refer this article for CMFCEditBrowseCtrl class..
https://www.codeproject.com/Articles/35722/MFC-Feature-Pack-CMFCEditBrowseCtrl

Disable/Hide Items in a CListCtrl MFC

I want to hide or disable Items with SetItemState(). The CListCtrl is in Report View.
It must be something like this m_List.SetItemState(1, DISABLE, DISABLE);
I searched but didnt find the right nState
If there is another solution than SetItemState, it also will be ok
Can anyone help me?
There is no item state to represent a disabled (grayed) item. See docs
The only way to solve this is your own implementation and using custom draw. That is the way I do it.
You can easily prevent the user to select an item when you trap LVN_ITEMCHANGING. Just filter the state Change to LVIS_SELECTED and return TRUE to prevent the change.
You can give visual feedback. I would derive a class CMFCListCtrl and override OnGetCellBkColor and OnGetCellTextColor methods to achieve it.
And I would override the its response to selection to unselect when selecting an unselectable item. However for this part, I am not so sure if it is doable.

How to extend listControl class in C++ and add new functions?

Hi I need to extend the CListControl class in C++/MFC, which will add several new features in the list control,
Any one have good sample code ?
Or could you please tell me how can i start it ?
Thanks in advance!
Or just write the new features and listControl into a ActiveX or COM ??
Which is better ?
TO add functionality such as you suggest in your comments above I wouldn't even make a derivation of CListCtrl. It would make more sense, IMO, to create a CListCtrlManager class that handles things such as you suggest and then handles populating an associated CListCtrl.
Thing is if you wish to derive from a CListCtrl then it is USUALLY done for handling owner draw. There is very little functionality that REQUIRES a derivation. For example I have a derived list ctrl that provides row colouring based on certain information as well as a checkbox in the list view. To handle that I had to set the owener draw flag and handle list ctrl drawing directly, but you do not need to make a derivation to handle the functionality you desire.

How to implement CEditListCtrl

How to implement CEditListCtrl?. List control with edit capabality (Report/Grid view).
I have a list view in Report View. It has some values. I need to extend this to edit the values present in the list view.
I declared a class which inherits from CListCtrl. And I have handled the two Window messages to start and end the edit. Upon getting the messages I am displaying a Text box. But I am not getting the control inside these message handlers. Is there a way to know the reason?
Or Is there a other way to implement this.
There are some neat grid controls on Code Project which might help:
http://www.codeproject.com/KB/miscctrl/gridctrl.aspx
http://www.codeproject.com/KB/library/gridprojects.aspx
http://www.codeproject.com/KB/MFC/UltimateGrid.aspx
Thanks for all answers
I have done it easily.
I have handled the WM_LBUTTONDOWN. This handler pops up the edit box to get the new
value for the field
Handled LVN_ENDLABELEDIT to know the end of update.
After receiving the above message, updated the values.
“One thing I forgotten was to set the flag in the resource view for CListCtrl (Edit Labels to TRUE)”
We have to implement OnPaint() (in CListCtrl's derived class) also otherwise the UI won't update properly
You need to use a CComboBox which is basically a combined CEdit and CListCtrl
This question was also asked here:
How to edit columns in-place with CListCtrl?
You can read my answer on that page.