For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > inserting item into list view









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author inserting item into list view
Steve

2005-11-22, 7:04 pm

I have this code:


<code>
HWND hList = GetDlgItem(hDlg, IDC_LIST_CHARACTERS);

LVCOLUMN column;

// character
column.mask = LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM;
column.pszText = "Character name";
column.cx = 200;
column.iSubItem = 1;
SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&column);

// number of bones
column.mask = LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM;
column.pszText = "Number of Bones";
column.cx = 90;
column.iSubItem = 2;
SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&column);


LVITEM item;

item.mask = LVIF_TEXT|LVIF_;
item.pszText = "This is a test";
item.iItem = 0;
item.iSubItem = 1;
SendMessage(hList, LVM_INSERTITEM, 0, (LPARAM)&item);


item.mask = LVIF_TEXT;
item.pszText = "asdasdasdasd";
item.iItem = 1;
item.iSubItem = 2;
SendMessage(hList, LVM_INSERTITEM, 0, (LPARAM)&item);
</code>


When this finishes, there are no items listed in my list control. I have
stepped through the code and everything looks fine except that the result
from LVM_INSERTITEM is -1;
This is weird, I can insert the columns, what would be wrong with the item?

Anyone see a problem with this?

Thanks for reading,
Steve


David Lowndes

2005-11-22, 7:04 pm

>item.mask = LVIF_TEXT|LVIF_;
>item.pszText = "This is a test";
>item.iItem = 0;
>item.iSubItem = 1;
>SendMessage(hList, LVM_INSERTITEM, 0, (LPARAM)&item);


Steve,

User LVM_INSERTITEM to inset the basic item, use LVM_SETITEMTEXT to
set any sub-item text. Set iSubItem to 0 for LVM_INSERTITEM.

Dave
Igor Tandetnik

2005-11-22, 7:04 pm

Steve <sss@sss.com> wrote:
> I have this code:
>
> LVITEM item;
>
> item.mask = LVIF_TEXT|LVIF_;
> item.pszText = "This is a test";
> item.iItem = 0;
> item.iSubItem = 1;
> SendMessage(hList, LVM_INSERTITEM, 0, (LPARAM)&item);
>
>
> item.mask = LVIF_TEXT;
> item.pszText = "asdasdasdasd";
> item.iItem = 1;
> item.iSubItem = 2;
> SendMessage(hList, LVM_INSERTITEM, 0, (LPARAM)&item);
> </code>


You misunderstand how listview works. The text in the first (leftmost)
column is considered an item. Text in other columns belongs to subitems
of this item.

First, you must insert an item with LVM_INSERTITEM. It should have
iSubItem set to 0. The message returns an index of freshly inserted
item.

Then, you set subitems with LVM_SETITEM. Here, iItem should use the
index returned by LVM_INSERTITEM, and iSubItem should identify the
desired subitem (0 for the item itself).
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Steve

2005-11-22, 7:04 pm


> You misunderstand how listview works. The text in the first (leftmost)
> column is considered an item. Text in other columns belongs to subitems
> of this item.
>
> First, you must insert an item with LVM_INSERTITEM. It should have
> iSubItem set to 0. The message returns an index of freshly inserted
> item.
>
> Then, you set subitems with LVM_SETITEM. Here, iItem should use the
> index returned by LVM_INSERTITEM, and iSubItem should identify the
> desired subitem (0 for the item itself).
> --
> With best wishes,
> Igor Tandetnik


Thanks Igor!

I am much closer now ;)
But I still have a weird issue.
Here is the code that I am now using
<code>
item.mask = LVIF_TEXT;
item.pszText = "first item";
// item.iItem = 0;
item.iSubItem = 0;
LRESULT index = SendMessage(hList, LVM_INSERTITEM, 0, (LPARAM)&item);


item.mask = LVIF_TEXT;
item.pszText = "I am a character";
item.iItem = index;
item.iSubItem = 1;
SendMessage(hList, LVM_SETITEM, 0, (LPARAM)&item);

item.mask = LVIF_TEXT;
item.pszText = "24";
item.iItem = index;
item.iSubItem = 2;
SendMessage(hList, LVM_SETITEM, 0, (LPARAM)&item);
</code>

I see the two items and they are in the correct columns. However, if I
click on the item in the second column, I can select it, but the item in the
first column will not select. It's very strange. Is there some other step
that I'm missing?

Thanks again for the quick reply,
Steve


Steve

2005-11-22, 7:04 pm


"David Lowndes" <DavidL@example.invalid> wrote in message
news:lg77o19kpddvu40ei9998d08888l15v2s0@
4ax.com...
>
> Steve,
>
> User LVM_INSERTITEM to inset the basic item, use LVM_SETITEMTEXT to
> set any sub-item text. Set iSubItem to 0 for LVM_INSERTITEM.
>
> Dave


Thanks for the reply David, I appreciate it!


Igor Tandetnik

2005-11-22, 7:04 pm

Steve <sss@sss.com> wrote:
> I am much closer now ;)
> But I still have a weird issue.
>
> I see the two items and they are in the correct columns. However, if
> I click on the item in the second column, I can select it, but the
> item in the first column will not select. It's very strange. Is
> there some other step that I'm missing?


You need a column for subitem=0 (the main item). You only add two
columns for subitems 1 and 2.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Steve

2005-11-22, 7:04 pm


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:eaKGSt77FHA.1140@tk2msftngp13.phx.gbl...
> Steve <sss@sss.com> wrote:
>
> You need a column for subitem=0 (the main item). You only add two
> columns for subitems 1 and 2.


So although I only want 2, I need to add 3? I'm .
subitem index is 1 based, so for the left most column, I would use index 1

I'm trying to understand how this works...

given that I want to have 2 columns visible, do I add 3 columns? I'm sure
this sounds stupid, but I just don't get it



Steve

2005-11-22, 7:04 pm


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:eaKGSt77FHA.1140@tk2msftngp13.phx.gbl...
> Steve <sss@sss.com> wrote:
>
> You need a column for subitem=0 (the main item). You only add two
> columns for subitems 1 and 2.



wait, I see. I re-read(again) your post... I understand now, please
disregard my last post. Thank you for your help!


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com