10

This is easy in .NET (not my question) but I'm trying to figure out if it is possible to simply change the text of a string in a Win32 list box control given an index.

There is a GetText function that takes an item index but nothing to change the text of an existing item/string. My workaround will be to remove it and add it back in the box (which is also a weird prospect since there is no single command to add a string + item data -- these must be done carefully by inserting the string and then setting the item data on the index of the inserted string, which is tricky (not possible?) with sorting active).

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Jared Updike
  • 7,165
  • 8
  • 46
  • 72

3 Answers3

8

Yes, the lack of a LB_SETITEMTEXT message is a bit weird.

You should put your Delete+Insert+SetData calls between calls to WM_SETREDRAW...

Anders
  • 97,548
  • 12
  • 110
  • 164
  • 1
    Yup, this is the correct solution. The WinForms controls do exactly the same thing under the hood. There is no more elegant solution that you're missing. – Cody Gray - on strike Mar 12 '11 at 08:37
  • 1
    @Cody: That's disheartening but not surprising. Win32: turning 30 second programming tasks into 30 minute escapades, since 1993. Even if you have the 1500 page Petzold reference on your desk. – Jared Updike Mar 12 '11 at 22:27
  • @Jared: Yes, it's unlikely that an API created back in the mid-80s would still be regarded as "cutting edge" nowadays. Mind you, it *certainly* was then: Windows programming in 1988 was truly revolutionary (well, there was the Mac...). What GDI alone managed to do is quite admirable. It's easy to criticize. The `ListView` control is *much* newer than the `ListBox`. It's not surprising that it also offers more features. As others have suggested, if you want those features, choose a different control. But if it takes you 30 minutes to delete an item and insert a new one, consider a new career. – Cody Gray - on strike Mar 13 '11 at 07:20
  • @Cody: Doesn't take 30 min to implement the workaround, it takes 30 min to double check the entire List Box API on msdn, skim the Petzold index/chapter, post to SO, and verify with my co-worker (Microsoft alum) that, yes, experienced .NET devs would expect SetItem to exist (30 sec task), but it doesn't, so go ahead with the 30 sec workaround. I hoped more experienced Win32 devs could verify my approach instead of tear me down defending a 8-years-late to the party API (cf. Smalltalk-80 or Apple Lisa). Switching all my code (ListBox to ListView) is also more than a 30s task but I'll consider it. – Jared Updike Mar 13 '11 at 15:54
2

At the risk of being off topic...

I tend to use the ListView control all of the time. You'll want it in report view to mimic a listbox, and, as a plus, it supports multiple columns.

Oh.. and it has a LVM_SETITEM Message :)
http://msdn.microsoft.com/en-us/library/bb761186(v=VS.85).aspx

Lynn Crumbling
  • 12,985
  • 8
  • 57
  • 95
  • The problem with using a listview in report mode like this is that you need to adjust the column width based on the width of the items if you hide the column header... – Anders Mar 12 '11 at 05:00
-1

Although this question is old, but I think this documentation presented by Microsoft will be able to answer anyone questions based on this one.

So according to Microsoft documentation which you can find here

Changes the text of a list-view item or subitem. You can use this macro or send the LVM_SETITEMTEXT message explicitly.

void ListView_SetItemText(
   hwndLV,
   i,
   iSubItem_,
   pszText_
);

And it also presents other macros for managing the list box. You can build a wrapper around this macros to simplify handling list view controls, etc.

Peter
  • 1,124
  • 14
  • 17