By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,313 Members | 1,145 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,313 IT Pros & Developers. It's quick & easy.

Can't Select Index 0 of a Listbox

P: n/a
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("ful lname").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
wi*****@google.com

Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi,

Since FindStringExact returns one value why dont you try this.

ProductList.SelectedIndex = cnt

Ken
-------------------
"Paul Sullivan" <wi*****@gmail.com> wrote in message
news:11********************@z14g2000cwz.googlegrou ps.com...
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("ful lname").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
wi*****@google.com
Nov 21 '05 #2

P: n/a
"Paul Sullivan" <wi*****@gmail.com> schrieb:
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).


Maybe your problem is related to his bug:

BUG: ComboBox Does Not Clear When You Set SelectedIndex to -1
<URL:http://support.microsoft.com/?scid=kb;EN-US;327244>

Try setting 'SelectedIndex' twice.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3

P: n/a
That solution would be okay, if there was only going to be one item in
the listbox that I wanted to select - which is the case right now.
But, in the very, very near future there will be multiple items in the
listbox that I need to progmatically select.

Here's what my code is doing:

1. Reads a list of products from the product_version_view table and
inserting them into the list box.
2. It then reads the list of products from the article_product_view
table and selects the matching items in the listbox.

(This is for a knowledge base where articles can apply to a single or
multiple products. The relationship between articles and products
exist in the article_product_view table.)

So, using FindStringExact does what I need it to do, but SetSelected
doesn't when the listbox item index is 0.

Nov 21 '05 #4

P: n/a
Paul,

When you do this sample I made now for that you will see that the index 0 is
selected.

I did not see direct errors in your code as well, so maybe you can check
using this what is maybe another thing that can give the error.
\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.AddRange(New Object() {"1", "2", "3", "4", "5"})
ListBox1.SelectionMode = SelectionMode.MultiExtended
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).ToString < "4" Then
ListBox1.SetSelected(i, True)
End If
Next
End Sub
///

I hope this helps?

Cor
Nov 21 '05 #5

P: n/a
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.

Nov 21 '05 #6

P: n/a
grc
I don't know if you found an answer or not - but this is it:

http://www.error-bank.com/microsoft....01_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...
Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.