ListBox Question

  • Thread starter Thread starter cjobes
  • Start date Start date
C

cjobes

Hi all,

I need to populate a listbox from a table column. The column has multiple
entries of usernames and I need to pull a unique set of usernames. The table
is part of an untyped Dataset that is created during runtime. There is no
DataAdapter. On an SQL server I would just pull a Unique Set but that
doesn't seem to be an option in my case. So I created the code below. It
works but it is very slow bringing up the list box. Is there a better way of
doing this?
Also, after the listbox is displayed how can I capture which username(s) the
user has highlighted when he/she clicks on a button called "doIt".

Public Sub selList()

Dim dcPick As New DataColumn("User", GetType(String))
dcPick.Unique = True
tbPick.Columns.Add(dcPick)

Dim pRows() As DataRow = _dt.Select()
Dim pSel As DataRow
For Each pSel In pRows
Try
tbPick.ImportRow(pSel)
Catch
Exit Try
End Try
Next
End Sub

Thanks for your help,

Claus
 
Claus,

The way you use now the Try catch block is the worsest way it can be,

You want to create a "distinct" table and even an easy one in my opinion.

The simplest way looks for me in this case,
Create a dataview
Set the sort of that to your distict column
Loop through that dataview
Test everytime for the earlier one when equal you skip when uneaqual you do
that import.

(what is that tbpick by the way?, it is a lot of guessing now, so I cannot
write some sample code)

Cor
 
Hi Cor,

I new I would end up with the worst solution *G*. VB.Net is a painful
learning experience.

tbPick is a temp table that I create for the purpose of getting the unique
set of usernames. Because I use it in different subs I dimmed it at the form
class level. That's why you don't see it in the code. I need to catch the
list of possible usernames for the duration of the program run for reuse.
The user is supposed to select one or more users and I run a Select (where
username(s) equal... against another table.

In another sub I have set the listbox to:
Me.pickBox.DataSource = tbPick
Me.pickBox.DisplayMember = "User"

I also haven't figured out how I can catch which user(s) the user selects
before he/she hits the "go" button.

Thanks,
Claus
 
Claus,

Is this something you want to archieve

\\\
Private Sub Form12_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'creating a very simple datatable
Dim dt As New DataTable
dt.Columns.Add("user")
Dim names() As String = New String() {"Geoff", "Cor", _
"Herfried", "Jay", "Herfried"}
For Each usern As String In names
Dim dr As DataRow = dt.NewRow
dr(0) = usern
dt.Rows.Add(dr)
Next
'sample distinct
Dim dtclone As DataTable = dt.Clone
Dim dv As New DataView(dt)
dv.Sort = "user"
Dim myselold As String = ""
For i As Integer = 0 To dv.Count - 1
If myselold <> dv(i)("user").ToString Then
Dim drn As DataRow = dtclone.NewRow
For y As Integer = 0 To drn.ItemArray.Length - 1
drn(y) = dv(i)(y)
Next
myselold = dv(i)("user").ToString
dtclone.Rows.Add(drn)
End If
Next
ListBox1.DataSource = dtclone
ListBox1.DisplayMember = "user"
'One Herfried is enough
End Sub
///

I hope this helps?

Cor
 
Cor,

Thanks for the sample. I might not understand this right but I can't do the

Dim names() As String = New String() {"Geoff", "Cor", "Herfried", "Jay",
"Herfried"}

because I don't know the usernames that are in the tbSelect table until I
read them.

Also, how do I find out which name(s) the user selected from the listbox?

Claus
 
Claus,

As I wrote in the sample, that is to create the sample datatable.
Thanks for the sample. I might not understand this right but I can't do
the

Dim names() As String = New String() {"Geoff", "Cor", "Herfried", "Jay",
"Herfried"}

because I don't know the usernames that are in the tbSelect table until I
read them.

I assume you have somewhere your own datatable with usernames.

Cor
 
Cor,

Dohhhhhh, now I feel really stupid. Ok, I did adapted your sample to use the
existing table. In the designer view the i and y are underlined, saying they
are not declared and the "AS" is underlined in both cases (for i as
integer=0...), saying "syntax error".

I also did some search on google groups to find out more about the listbox
selected item. I tried:
Dim pickTry = pickBox.GetItemText(pickBox.SelectedItems)
but when I halt the program and check the value of pickTry it says the
object name and not the actual value.

Claus
 
Cor,

I fixed that syntax error by declaring i and y before starting the routine
and taking the AS out of the code. It works very well and much faster than
before. Thanks.

I still can't get the selected value into a variable. The variable holds the
Type instead of the actual value. I also can't select more than one value in
the listbox. I have seen apps doing that so I know it's possible. I will do
some further reseach on it.

Thanks again,
Claus
 
I found the property to set to MultiExtended. Still no luck with getting the
value of the selected items.
 
I'm really stuck here. All my books say to get the value of a selected item
in a listbox you do something like this:
dim xValue=listbox1.selecteditem.tostring()
to test this I inserted:
messagebox.show("testing: "+listbox1.selecteditem.tostring())
what it displays is:
"Testing: System.Data.DataViewRow"

How can I get the actual value of the selected item?

Thanks,
Claus
 
You have to set the DisplayMember property of the ListBox to the name of the
Field from which it gets its data.

If you are using MultiExtended, to allow multiple selections of items, you
capture the selected items with:
Dim soc As ListBox.SelectedObjectCollection = ListBox1.SelectedItems

The collection items are ListViewItems, so loop through the collection:
dim lvi as listviewitem
dim s as string
For Each lvi in soc
s = lvi(0)
Next

If you are using a Value field to represent a Key for the UserID, that field
would be assigned to the ValueMember field of the ListBox.
 
Just In Case

If you are using Option Strict
Make this change from the previous post:
For Each lvi in soc
s = lvi(0).tostring
Next

You can also reference any field from the datasource. Here's a snippet of
code to illustrate:

(in Load event)

Dim dt As New DataTable()
Dim dc0 As New DataColumn("UserID",
System.Type.GetType("System.Int32"))
Dim dc1 As New DataColumn("UserName",
System.Type.GetType("System.String"))
Dim dc2 As New DataColumn("StartDate",
System.Type.GetType("System.DateTime"))

dt.Columns.Add(dc0)
dt.Columns.Add(dc1)
dt.Columns.Add(dc2)

Dim dr As DataRow = dt.NewRow
dr.Item(0) = 1
dr.Item(1) = "Bob"
dr.Item(2) = #3/4/1999#
dt.Rows.Add(dr)

dr = dt.NewRow
dr.Item(0) = 2
dr.Item(1) = "Sam"
dr.Item(2) = #3/9/2004#
dt.Rows.Add(dr)
Me.ListBox1.DisplayMember = "UserName"
Me.ListBox1.DataSource = dt

(in button click event)

Dim soc As Windows.Forms.ListBox.SelectedObjectCollection =
ListBox1.SelectedItems
Dim drv As DataRowView
For Each drv In soc
Dim id As Int32 = drv(0)
Dim user As String = drv(1)
Dim d As Date = drv(2)
Next
 
Correction: The SelectedObjectCollection contains DataRowView objects, not
ListViewItem objects as I stated in my first post on this thread. Sorry.
(That's what can happen when code is not checked out first.)

First post erroneous code:

Dim soc As Windows.Forms.ListBox.SelectedObjectCollection =
ListBox1.SelectedItems
dim lvi as listviewitem
dim s as string
For Each lvi in soc
s = lvi(0)
Next

The second post has it right:

(see post for datasource setup)
Dim soc As Windows.Forms.ListBox.SelectedObjectCollection =
ListBox1.SelectedItems
Dim drv As DataRowView
For Each drv In soc
Dim id As Int32 = drv(0)
Dim user As String = drv(1)
Dim d As Date = drv(2)
Next

Though I would even change the correct code, and put the Dim statements
outside the loop.

Charlie
 
Thanks Charlie,

I'm currently on the road and will try this tonight.

Claus
 
Thanks Charlie,

As always, it works like a charme. I wish there was better documentation
around so I would not have to bother you so often. I have 3 books that are
supposed to be really good but more often than I would like, they don't have
the answers. I wish there was a book out that had a windows forms project
with all the code and then takes the reader through it by example.

Claus
 
Thanks for letting me know. I agree. Learning by example is very effective.
The first book I used was full of mistakes. Once I realized that, the book
became like "Where's Waldo?". In some way, it was helpful, because it made
me more of a detective. But I sure do like how VB helps the programmer nail
errors, and how intellisense provides a list of members, even for derived
classes. I think that is what made VB the most used language in the world.

I sent you an e-mail at your web address.
 
Claus,

Next time show how you have setted the displaymember, mostly it has to do
with a case sensitive situation. This part is case sesitive.

We live in another time segment of the world which means that there can be
big time gabs.

However I hope this helps?

Cor
 
Cor,

Here is the code that does the job including preparing the search string:

Dim pickTry As String = ""
Dim soc As Windows.Forms.ListBox.SelectedObjectCollection = _
pickBox.SelectedItems()
Dim drv As DataRowView
For Each drv In soc
If Len(pickTry) < 1 Then
pickTry = "user='" + drv(0) + "'"
Else
pickTry = pickTry + " or user='" + drv(0) + "'"
End If
Next

Thanks again for your help. I'm slowly getting a hang of it.

Claus
 
Claus,

You can do something as this (when you have not set multiselect to true)
Dim picktry As String = DirectCast(ListBox1.SelectedItem,
DataRowView).Item(0).ToString

What you can do as well is setting the datavalue of your listbox to the
"user" as well, that is the same as your displaymember (so both the same),
than it becomes
Dim picktry As String = ListBox1.SelectedValue.ToString

I hope this helps?

Cor
 
Cor,

Thanks, in this case multiselect is true. That's why I had to do the loop.

Claus
 

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

Back
Top