Can't Select Index 0 of a Listbox

P

Paul Sullivan

Hi folks. I was hoping that someone can help me out here.

I am adding items to a listbox from a database. So far, I've had no
problems. However, when I try to use the SetSelected method and there
is only one item in the list box (who's index is 0), it won't highlight
(or at least, it doesn't show it on the screen).

Here my relevant code:

' Load Product List
dbQuery = "SELECT fullname FROM product_version_view ORDER BY fullname"
Dim dbProductCommand As New SqlClient.SqlCommand(dbQuery,
Me.KBConnection)
Dim dbProductReader As SqlClient.SqlDataReader
dbProductReader = dbProductCommand.ExecuteReader

Do While dbProductReader.Read
Me.ProductList.Items.Add(dbProductReader.Item("fullname").ToString)
Loop
dbProductReader.Close()
dbProductCommand.Dispose()

[... do other stuff ...]

' Select Products
dbQuery = "SELECT fullname FROM article_product_view WHERE kbid = " &
Me.kbid
Dim dbSProductCommand As New SqlClient.SqlCommand(dbQuery,
Me.KBConnection)
Dim dbSProductReader As SqlClient.SqlDataReader
dbSProductReader = dbSProductCommand.ExecuteReader

Do While dbSProductReader.Read
cnt =
ProductList.FindStringExact(dbSProductReader.Item("fullname").ToString)
Me.ProductList.SetSelected(cnt, True)
Debug.WriteLine("Selecting index " & cnt)
Loop

dbSProductReader.Close()
dbSProductCommand.Dispose()


I even tried doing ProductList.SetSelected(0, True), but that didn't
seem to work either. And yes, the strings do match - if you look on
the debug window, it will say index 0 was selected, but not on the
screen.

Anyone out there have any ideas?

Thanks,
Paul
(e-mail address removed)
 
P

Paul Sullivan

Boy do I feel stupid right now. I found the solution.

My code (listed above) executes in a public sub called "initForm"
(which takes care of loading everything frome the database into the
form). That form is "started" from another form, with the following
code:

dim newArticleEdit as new ArticleEditForm
newArticleEdit.MdiParent = Me.Parent
newArticleEdit.initForm()
newArticleEdit.Show()

But, when I show the form first, then run the initForm sub - it works
properly.

Sorry folks - I didn't think it mattered which one I did first, but
obviously it does. Doh!!!

Thanks for everyone's time and help.
 
G

grc

I don't know if you found an answer or not - but this is it:

http://www.error-bank.com/microsoft.public.dotnet.framework.windowsforms/66601_Thread.aspx

Executive summary:

The handle isn't initialized, so something like the following put before your code will sove the problem:

Dim x as System.InPtr
x = Me.ProductList.Handle

( I actually do C# programming, I think the above should work in VB ).




**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top