Home All Groups Group Topic Archive Search About

Assigning ListBox Current Selection to TextBox

Author
30 Jan 2006 2:21 PM
sajones
Hello,

I'm a novice user having troubles retrieving data from a listbox in VB
Express 2005.

My listbox is populated by a table from an Access database.

I'm trying to assign the current listbox selection to a textbox using:
"Me.txtSelection.Text = CStr(lstUsers.Items(Me.lstUsers.SelectedIndex))"

When running this code I get the following error:

"InvalidCastException was unhandled.  Conversion from type'DataRowView' to
type 'String' is not valid."

I'm assuming that this means that I'm trying to return the entire record
rather than a single field in the record.  Unfortunately I don't know how to
return just a single field and I couldn't find anything in 'Help'.

Any assistance is greatly appreciated.

Thanks,
Scott

Author
30 Jan 2006 4:26 PM
Bart Mermuys
Hi,

Show quoteHide quote
<sajo***@netta.us> wrote in message news:drl7cv03uu@enews4.newsguy.com...
> Hello,
>
> I'm a novice user having troubles retrieving data from a listbox in VB
> Express 2005.
>
> My listbox is populated by a table from an Access database.
>
> I'm trying to assign the current listbox selection to a textbox using:
> "Me.txtSelection.Text = CStr(lstUsers.Items(Me.lstUsers.SelectedIndex))"
>
> When running this code I get the following error:
>
> "InvalidCastException was unhandled.  Conversion from type'DataRowView' to
> type 'String' is not valid."

If you want to get the text from the selected item in a ListBox, you could
use:
Me.txtSelection.Text = Me.lstUsers.Text

>
> I'm assuming that this means that I'm trying to return the entire record
> rather than a single field in the record.

True, if you want to access other fields you can use:

Dim drv As DataRowView = DirectCast( lstUsers.SelectedItem, DataRowView)
Me.txtSelection.Text = drv("fieldname").ToString()

Note that if you bind the TextBox to the same BindingSource as the ListBox
it can automatically show the selected listbox text.

HTH,
Greetings


Show quoteHide quote
>  Unfortunately I don't know how to
> return just a single field and I couldn't find anything in 'Help'.
>
> Any assistance is greatly appreciated.
>
> Thanks,
> Scott
Author
30 Jan 2006 6:45 PM
sajones
Show quote Hide quote
On 30-Jan-2006, "Bart Mermuys" <bmermuys.nospam@hotmail.com> wrote:

> Hi,
>
> <sajo***@netta.us> wrote in message news:drl7cv03uu@enews4.newsguy.com...
> > Hello,
> >
> > I'm a novice user having troubles retrieving data from a listbox in VB
> > Express 2005.
> >
> > My listbox is populated by a table from an Access database.
> >
> > I'm trying to assign the current listbox selection to a textbox using:
> > "Me.txtSelection.Text = CStr(lstUsers.Items(Me.lstUsers.SelectedIndex))"
> >
> > When running this code I get the following error:
> >
> > "InvalidCastException was unhandled.  Conversion from type'DataRowView'
> > to
> > type 'String' is not valid."
>
> If you want to get the text from the selected item in a ListBox, you could
>
> use:
> Me.txtSelection.Text = Me.lstUsers.Text
>
> >
> > I'm assuming that this means that I'm trying to return the entire record
> > rather than a single field in the record.
>
> True, if you want to access other fields you can use:
>
> Dim drv As DataRowView = DirectCast( lstUsers.SelectedItem, DataRowView)
> Me.txtSelection.Text = drv("fieldname").ToString()
>
> Note that if you bind the TextBox to the same BindingSource as the ListBox
>
> it can automatically show the selected listbox text.
>
> HTH,
> Greetings
>
>
> >  Unfortunately I don't know how to
> > return just a single field and I couldn't find anything in 'Help'.
> >
> > Any assistance is greatly appreciated.
> >
> > Thanks,
> > Scott

You're a genius!  It worked great!  Thanks for the help.

So much for online help...

Thanks,
Scott