Valhalla Legends Archive

Programming => General Programming => Topic started by: Grok on June 30, 2003, 08:56 AM

Title: ListBox question
Post by: Grok on June 30, 2003, 08:56 AM
Given a populated ListBox, I'd like to delete the blank lines.

   Dim lRet As Long, lPos As Long
   Dim strSearch As String
   strSearch = ""
   lRet = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal strSearch)
   If lRet = LB_ERR Then Exit Sub
   lPos = lRet
   lRet = SendMessage(List1.hwnd, LB_DELETESTRING, lPos, 0)


However, after LB_FINDSTRING, lRet is always -1, no matter how many blank lines are in the listbox.  If I search for any string with strlen>0, the search succeeds.

What is the correct solution for finding blank lines in a listbox?
Title: Quick guess
Post by: Kp on June 30, 2003, 01:12 PM
Quote from: Grok on June 30, 2003, 08:56 AM
Given a populated ListBox, I'd like to delete the blank lines.

   Dim lRet As Long, lPos As Long
   Dim strSearch As String
   strSearch = ""
   lRet = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal strSearch)
   If lRet = LB_ERR Then Exit Sub
   lPos = lRet
   lRet = SendMessage(List1.hwnd, LB_DELETESTRING, lPos, 0)


However, after LB_FINDSTRING, lRet is always -1, no matter how many blank lines are in the listbox.  If I search for any string with strlen>0, the search succeeds.

What is the correct solution for finding blank lines in a listbox?
I note that you're doing "ByVal strSearch" in the SendMessage call.  I may be wrong, but that sounds like it would pass the wrong data.  According to LB_FINDSTRING documentation, the last paramater is a pointer to the string for which to search - so maybe ByRef would be more appropriate?
Title: Re:Quick guess
Post by: Camel on June 30, 2003, 06:16 PM
Quote from: Kp on June 30, 2003, 01:12 PM
Quote from: Grok on June 30, 2003, 08:56 AM
Given a populated ListBox, I'd like to delete the blank lines.

   Dim lRet As Long, lPos As Long
   Dim strSearch As String
   strSearch = ""
   lRet = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal strSearch)
   If lRet = LB_ERR Then Exit Sub
   lPos = lRet
   lRet = SendMessage(List1.hwnd, LB_DELETESTRING, lPos, 0)


However, after LB_FINDSTRING, lRet is always -1, no matter how many blank lines are in the listbox.  If I search for any string with strlen>0, the search succeeds.

What is the correct solution for finding blank lines in a listbox?
I note that you're doing "ByVal strSearch" in the SendMessage call.  I may be wrong, but that sounds like it would pass the wrong data.  According to LB_FINDSTRING documentation, the last paramater is a pointer to the string for which to search - so maybe ByRef would be more appropriate?

It depends on how you've declared SendMessage IIRC. If you have the third param declared as "As Any" or "As String", it will be converted to a pointer automaticly. My suggestion is to declare it "As Long" and use the StrPtr([String]) function to get a pointer to your string for clarity's sake.
But to answer your question Grok, I'm pretty sure you just need to change your -1 to 0, because VB isn't zero-based.
Title: Re:Quick guess
Post by: Grok on July 01, 2003, 03:25 AM
Quote from: Camel on June 30, 2003, 06:16 PM
But to answer your question Grok, I'm pretty sure you just need to change your -1 to 0, because VB isn't zero-based.

The -1 tells LB_FINDSTRING where to begin the search after.  If I used 0, the search would begin after the first entry.  MSDN on LB_FINDSTRING says to use -1 to begin search with the first element of the ListBox.

Note to Kp:  The same code finds non-blank entries just great.  It is only when I search for an empty string that the search fails to find the targets.

Thanks to both of you but this remains unsolved.
Title: Re:ListBox question
Post by: Zonker on July 01, 2003, 04:46 AM
You could always send a LB_GETTEXTLEN msg for each of the items and see if they return 0
Title: Re:ListBox question
Post by: Adron on July 02, 2003, 04:09 PM
Quote
LB_FINDSTRING
An application sends an LB_FINDSTRING message to find the first string in a list box that begins with the specified string.

What string doesn't begin with an empty string? I.e. it doesn't make any sense at all to give it an empty string.

Try LB_FINDSTRINGEXACT instead.