ListBox.FindString() doesn't find empty strings. Why?

Hi, I've noticed that if a wx.ListBox contains and empty string, a
call to FindString("") return wx.NOT_FOUND instead of the index.

Try this:

import wx
app=wx.App(redirect=None)
f=wx.Frame(None)
list=wx.ListBox(f, choices=["foo","","bar"])
f.Show()
print 'FindString("foo"):', list.FindString("foo")
print 'FindString("bar"):', list.FindString("bar")
print 'FindString(""):', list.FindString("")
print 'GetStrings().index(""):', list.GetStrings().index("") #This is
a workaround
app.MainLoop()

The workaround is to find the index within GetStrings(), but I'm not
sure about the order.

Finally: is this the corrent behaviour of FindString()? Am I missing
something or may it be a bug?

Thanks
Marco

Marco Bertola wrote:

Hi, I've noticed that if a wx.ListBox contains and empty string, a
call to FindString("") return wx.NOT_FOUND instead of the index.

Try this:

import wx
app=wx.App(redirect=None)
f=wx.Frame(None)
list=wx.ListBox(f, choices=["foo","","bar"])
f.Show()
print 'FindString("foo"):', list.FindString("foo")
print 'FindString("bar"):', list.FindString("bar")
print 'FindString(""):', list.FindString("")
print 'GetStrings().index(""):', list.GetStrings().index("") #This is
a workaround
app.MainLoop()

The workaround is to find the index within GetStrings(), but I'm not
sure about the order.

Finally: is this the corrent behaviour of FindString()? Am I missing
something or may it be a bug?

Thanks
Marco

I don't get wx.NOT_FOUND. I get a -1, which means pretty much the same thing. I think it may be a bug as well, but we'll have to wait for Robin to be sure.

Mike

Mike Driscoll wrote:

Marco Bertola wrote:

Hi, I've noticed that if a wx.ListBox contains and empty string, a
call to FindString("") return wx.NOT_FOUND instead of the index.

Try this:

import wx
app=wx.App(redirect=None)
f=wx.Frame(None)
list=wx.ListBox(f, choices=["foo","","bar"])
f.Show()
print 'FindString("foo"):', list.FindString("foo")
print 'FindString("bar"):', list.FindString("bar")
print 'FindString(""):', list.FindString("")
print 'GetStrings().index(""):', list.GetStrings().index("") #This is
a workaround
app.MainLoop()

The workaround is to find the index within GetStrings(), but I'm not
sure about the order.

Finally: is this the corrent behaviour of FindString()? Am I missing
something or may it be a bug?

Thanks
Marco

I don't get wx.NOT_FOUND. I get a -1, which means pretty much the same thing.

Not pretty much, it *is* the same thing:

  >>> import wx
  >>> wx.NOT_FOUND
  -1
  >>>

:slight_smile:

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Marco Bertola wrote:

Hi, I've noticed that if a wx.ListBox contains and empty string, a
call to FindString("") return wx.NOT_FOUND instead of the index.

Try this:

import wx
app=wx.App(redirect=None)
f=wx.Frame(None)
list=wx.ListBox(f, choices=["foo","","bar"])
f.Show()
print 'FindString("foo"):', list.FindString("foo")
print 'FindString("bar"):', list.FindString("bar")
print 'FindString(""):', list.FindString("")
print 'GetStrings().index(""):', list.GetStrings().index("") #This is
a workaround
app.MainLoop()

The workaround is to find the index within GetStrings(), but I'm not
sure about the order.

Finally: is this the corrent behaviour of FindString()? Am I missing
something or may it be a bug?

It might be a bug, but if so it is probably Microsoft's. The Windows version of wxListBox::FindString is just a thin wrapper around the ListBox_FindStringExact API. Although MSDN doesn't say anything about that reacting differently to empty strings...

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

Mike Driscoll wrote:

Marco Bertola wrote:

Hi, I've noticed that if a wx.ListBox contains and empty string, a
call to FindString("") return wx.NOT_FOUND instead of the index.

Try this:

import wx
app=wx.App(redirect=None)
f=wx.Frame(None)
list=wx.ListBox(f, choices=["foo","","bar"])
f.Show()
print 'FindString("foo"):', list.FindString("foo")
print 'FindString("bar"):', list.FindString("bar")
print 'FindString(""):', list.FindString("")
print 'GetStrings().index(""):', list.GetStrings().index("") #This is
a workaround
app.MainLoop()

The workaround is to find the index within GetStrings(), but I'm not
sure about the order.

Finally: is this the corrent behaviour of FindString()? Am I missing
something or may it be a bug?

Thanks
Marco

I don't get wx.NOT_FOUND. I get a -1, which means pretty much the same thing.

Not pretty much, it *is* the same thing:

>>> import wx
>>> wx.NOT_FOUND
-1
>>>

:slight_smile:

I thought it might be, but whenever I think I know something, I almost always get shot down. Thanks!

Mike

Humble suggestion. MSDN states that

lpszFind (FindStringExact’s parameter)

points to the null-terminated string to search for.

I surmise that an empty string is NOT null-terminated, but simply isn’t, while the null-termination is a particular value but anyway a value, like zero. If this is correct, we are not coping with a real bug, but in order to find the true result of FindString(“”) a workaround is needed. Something like:

      if result == wx.NOT_FOUND:

           for i in range(list.Count):

                if list.GetString(i) == "": return i
···

2009/1/23 Robin Dunn robin@alldunn.com

Marco Bertola wrote:

Hi, I’ve noticed that if a wx.ListBox contains and empty string, a

call to FindString(“”) return wx.NOT_FOUND instead of the index.

Try this:

import wx

app=wx.App(redirect=None)

f=wx.Frame(None)

list=wx.ListBox(f, choices=[“foo”,“”,“bar”])

f.Show()

print ‘FindString(“foo”):’, list.FindString(“foo”)

print ‘FindString(“bar”):’, list.FindString(“bar”)

print ‘FindString(“”):’, list.FindString(“”)

print ‘GetStrings().index(“”):’, list.GetStrings().index(“”) #This is

a workaround

app.MainLoop()

The workaround is to find the index within GetStrings(), but I’m not

sure about the order.

Finally: is this the corrent behaviour of FindString()? Am I missing

something or may it be a bug?

It might be a bug, but if so it is probably Microsoft’s. The Windows version of wxListBox::FindString is just a thin wrapper around the ListBox_FindStringExact API. Although MSDN doesn’t say anything about that reacting differently to empty strings…

Robin Dunn

Software Craftsman

http://wxPython.org Java give you jitters? Relax with wxPython!


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

raffaello wrote:

Humble suggestion. MSDN states that

/lpszFind (FindStringExact's parameter)
/

points to the null-terminated string to search for.

I surmise that an empty string is NOT null-terminated, but simply isn't, while the null-termination is a particular value but anyway a value, like zero.

A NULL terminated empty string is a well known thing. It's simply a sequence of bytes where the first (and only) item is a zero byte. If wx had problems making one of those then there would be tons of problems all over the place.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!