CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Mar 2003
    Posts
    125

    Question 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.

  2. #2
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Quote 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?

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430
    Quote 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

  4. #4
    Join Date
    Mar 2003
    Posts
    125

    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.

  5. #5
    Join Date
    Mar 2003
    Posts
    125
    I mean by "when i execute this code" , when i execute my whole code, in which i have my listView.

    Hajer.

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360
    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Feb 2002
    Posts
    3,788
    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);

  8. #8
    Join Date
    Mar 2003
    Posts
    125

    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.

  9. #9
    Join Date
    Mar 2003
    Posts
    125
    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.

  10. #10
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Quote 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.

  11. #11
    Join Date
    Mar 2003
    Posts
    125

    Thank You

    Thank you gstercken,
    I used SetExtendedStyle (LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);

    It works allright.
    Hajer.

  12. #12
    Join Date
    May 2003
    Location
    Pakistan
    Posts
    223
    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

  13. #13
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360
    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,...);
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  14. #14
    Join Date
    May 2003
    Location
    Pakistan
    Posts
    223
    thanx it really worked.
    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
  •  





Click Here to Expand Forum to Full Width

Featured