|
-
July 14th, 2004, 03:57 AM
#1
ListView InsertItem
Hi ALL,
I'm writing a code inserting an item in a listview control,
Code:
TCHAR tstr[255];
_tcscpy(tstr,_T("something"));
CListCtrl& m_itemsCtl = m_listview.GetListCtrl ();
LV_ITEM lv_item;
lv_item.mask = LVIF_TEXT|LVIF_PARAM;
lv_item.iSubItem = 0;
lv_item.iItem = 0;
lv_item.state = ~LVIS_SELECTED|~LVIS_FOCUSED;
lv_item.stateMask = LVIS_SELECTED|LVIS_FOCUSED;
lv_item.pszText = tstr;
int iItem = m_itemsCtl.InsertItem(&lv_item);
The code above inserts a listitem at position 0 (lv_item.iItem = 0), but when i execute this code the InsertItem returns different values and doesn't insert the listiem in the first position.
How to resolve this problem?
I need help.
Thanks in advance.
-
July 14th, 2004, 04:12 AM
#2
 Originally Posted by hajer
The code above inserts a listitem at position 0 (lv_item.iItem = 0), but when i execute this code the InsertItem returns different values and doesn't insert the listiem in the first position.
How to resolve this problem?
Does your list control have the LVS_SORTASCENDING or LVS_SORTDESCENDING style set?
-
July 14th, 2004, 04:14 AM
#3
 Originally Posted by hajer
Hi ALL,
I'm writing a code inserting an item in a listview control,
.......................
The code above inserts a listitem at position 0 (lv_item.iItem = 0), but when i execute this code the InsertItem returns different values and doesn't insert the listiem in the first position.
How to resolve this problem?
I need help.
1. Thanks in advance.
What do you mean by "when i execute this code "?
2. According to MSDN ( CListCtrl::InsertItem )
........
Return Value
The index of the new item if successful or -1 otherwise.
If you'd call InsertItem more than once - it could return you any (also not zero) values depending on the sort style of your list control
-
July 14th, 2004, 04:19 AM
#4
I don't have the LVS_SORTASCENDING or LVS_SORTDESCENDING style set.
Thank you,
The style of my listview control is :
WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP;
I don't have the LVS_SORTASCENDING or LVS_SORTDESCENDING style set.
Hajer.
-
July 14th, 2004, 04:20 AM
#5
I mean by "when i execute this code" , when i execute my whole code, in which i have my listView.
Hajer.
-
July 14th, 2004, 04:36 AM
#6
I'm nor sure about this, but since you set the LVIF_PARAM mask
Code:
lv_item.mask = LVIF_TEXT|LVIF_PARAM;
the lParam member is valid or must be filled in. So it takes the trash data from it and use it and affect the insertion. But this is just a hunch.
-
July 14th, 2004, 04:42 AM
#7
try this:
Code:
TCHAR tstr[255];
_tcscpy(tstr,_T("something"));
LV_ITEM lv_item;
lv_item.mask = LVIF_TEXT|LVIF_PARAM;
int i = 0;
lv_item.iItem = i;
lv_item.iSubItem = 0;
lv_item.state = ~LVIS_SELECTED|~LVIS_FOCUSED;
lv_item.stateMask = LVIS_SELECTED|LVIS_FOCUSED;
lv_item.pszText = tstr;
for(;i < 5; i++)
int iItem = m_list.InsertItem(&lv_item);
-
July 14th, 2004, 04:54 AM
#8
Stange behaviour
Hi ALL,
When i tried to check if the LVS_SORTDESCENDING or LVS_SORTASCENDING are set, i wrote the following code :
Code:
style = WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP;
if (style & LVS_SORTDESCENDING)
AfxMessageBox("LVS_SORTDESCENDING ");
if (style & LVS_SORTASCENDING )
AfxMessageBox("LVS_SORTASCENDING ");
When i launch the program, the two box messages appeared 
How may i resolve this?
Hajer.
-
July 14th, 2004, 05:18 AM
#9
I don't understand the behaviour, but il made some modifications and when i set the LVS_EX_FULLROWSELECT style or the LVS_EX_HEADERDRAGDROP,
The LVS_SORTDESCENDING style is set automatically.
The problem i encountred now is how to add the LVS_EX_FULLROWSELECT style without the LVS_SORTDESCENDING style.

Hajer.
-
July 14th, 2004, 05:22 AM
#10
 Originally Posted by hajer
I don't understand the behaviour, but il made some modifications and when i set the LVS_EX_FULLROWSELECT style or the LVS_EX_HEADERDRAGDROP,
The LVS_SORTDESCENDING style is set automatically.
The problem i encountred now is how to add the LVS_EX_FULLROWSELECT style without the LVS_SORTDESCENDING style.
Note that LVS_EX_FULLROWSELECT and LVS_EX_HEADERDRAGDROP are extended styles, you can't combine them with your normal window styles. Use SetExtendedStyle() for them.
-
July 14th, 2004, 05:40 AM
#11
Thank You
Thank you gstercken,
I used SetExtendedStyle (LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
It works allright.
Hajer.
-
July 23rd, 2004, 03:19 AM
#12
Hi I also similar kind of probelm , In Report style fot a list ctrl .
When I insert items , every new item enters at the top , so if I enter 1,2,3,4 in list control one by one it will look like
4
3
2
1
But I want that every new item should be entered at bottom and i also dont want sorting , sequence in my case is very important.
So how to insert every new item at the last of list ctrl. to have such kind of result
1
2
3
4
where 1 entered first , 2 secondly and so on.
Unmanaged in a .NET world
-
July 23rd, 2004, 03:40 AM
#13
Well, first make sure that you're list is not sorted. Second, can do something like this:
Code:
CListCtrl &list = GetListCtrl();
int n = list.GetItemCount();
n = list.InsertItem(n,"last item");
list.SetItemText(n,...);
-
July 23rd, 2004, 04:48 AM
#14
Unmanaged in a .NET world
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|