473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Set Listbox Value

How can I set the listbox value and retrieve a listbox value with just text?
I have filled the listbox with items in the items collection property (very
simple, just one column of text items).

I will have the text from a database and I know it is in the list. How can I
set it and retrieve it?

Do I need to loop the listbox to find matching text and set that
selectedindex to true.

Any sample code would be appreciated to get the text and set the text.
Oct 14 '08 #1
4 9708
Derek Hart wrote:
How can I set the listbox value and retrieve a listbox value with just text?
I have filled the listbox with items in the items collection property (very
simple, just one column of text items).

I will have the text from a database and I know it is in the list. How can I
set it and retrieve it?

Do I need to loop the listbox to find matching text and set that
selectedindex to true.

Any sample code would be appreciated to get the text and set the text.

ListBox has FindString and FindStringExact methods.
Here's a sample to get started:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
ListBox1.Items.Add("Banana")
ListBox1.Items.Add("is a")
ListBox1.Items.Add("fruit")
ListBox1.Items.Add("but")
ListBox1.Items.Add("monkey")
ListBox1.Items.Add("is an")
ListBox1.Items.Add("animal")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ThisIndex As Integer
ThisIndex = ListBox1.FindStringExact("monkey")
If ThisIndex >= 0 Then
MessageBox.Show(ListBox1.Items(ThisIndex).ToString , _
"ListBox", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
ListBox1.SelectedIndex = ThisIndex
ListBox1.Items(ThisIndex) = "donkey"
End If
End Sub
--

Teme64 @ http://windevblog.blogspot.com
Oct 15 '08 #2
On Tue, 14 Oct 2008 15:50:14 -0700, "Derek Hart"
<de********@yahoo.comwrote:
>How can I set the listbox value and retrieve a listbox value with just text?
I have filled the listbox with items in the items collection property (very
simple, just one column of text items).

I will have the text from a database and I know it is in the list. How can I
set it and retrieve it?

Do I need to loop the listbox to find matching text and set that
selectedindex to true.

Any sample code would be appreciated to get the text and set the text.
Listbox has a SelectedValue property. I don't know how it behaves if
the Listbox allows multiple items to be selected.
Oct 15 '08 #3
Derek,

In the same way as a Combobox

Create a collection by instance a datatable which is the most simple to use
with a listbox

Set that as the DataSource
Set one column to the displaymember
Set another column to the valuemember

You are ready

Cor

"Derek Hart" <de********@yahoo.comwrote in message
news:ui**************@TK2MSFTNGP05.phx.gbl...
How can I set the listbox value and retrieve a listbox value with just
text? I have filled the listbox with items in the items collection
property (very simple, just one column of text items).

I will have the text from a database and I know it is in the list. How can
I set it and retrieve it?

Do I need to loop the listbox to find matching text and set that
selectedindex to true.

Any sample code would be appreciated to get the text and set the text.
Oct 15 '08 #4
"Derek Hart" <de********@yahoo.comwrote in message
news:ui**************@TK2MSFTNGP05.phx.gbl...
How can I set the listbox value and retrieve a listbox value with just
text? I have filled the listbox with items in the items collection
property (very simple, just one column of text items).

I will have the text from a database and I know it is in the list. How can
I set it and retrieve it?

Do I need to loop the listbox to find matching text and set that
selectedindex to true.

Any sample code would be appreciated to get the text and set the text.
Did

myListBox.Text = someTextIGotFromMyDatabase

not work for you?
Oct 15 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: gdbjohnson-AT-yahoo-dot-ca-nospamplz | last post by:
I have a ListBox built of a simple custom object for the ListItems used to be able to hold a Data Value, and a Display Value, with accessors for each. I have overridden the ToString method to...
2
by: collie | last post by:
Hi, I have 2 listboxes. The first gets populated from the db as soon as the page loads. The second listbox get populated based on the user's selection from the first listbox. However,...
1
by: Daniel | last post by:
hi, I had an asp:listbox, and everytime i click item inside, the bar automatically go to the top, is there any way to keep the scroll position? I turn on the smartNavigation, it still doesn't...
4
by: Moe Sizlak | last post by:
Hi There, I am trying to return the value of a listbox control that is included as a user control, I can return the name of the control but I can't access the integer value of the selected item,...
4
by: collie | last post by:
HI, I need to populate 2 listboxes from a db. When the page loads then the first listbox needs to be populated and based on selection from that listbox the second listbox needs to be populated...
0
by: David J | last post by:
Hi, I am strugling with the propertygrid and a listbox. I am using the universaldropdowneditor from the codeproject (code below). However I am populating the listbox via a datasource. The problem...
5
by: Matthew Wells | last post by:
I have a listbox set to simple multi select. For this example, users only select one item at a time. I have command buttons on the form for First, Previous, Next, Last, New (record). The form...
2
by: rn5a | last post by:
A ListBox lists all the files & directories existing in a directory on the server. If an item in the ListBox happens to be a directory, then the name of the directory is appended with the text ....
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
5
by: Academia | last post by:
(If you've seen this in the drawing NG, sorry. I inadvertently sent it there.) I have a listbox populated with Objects. The Class has a String field that ToString returns. I assume that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.