473,386 Members | 1,736 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,386 software developers and data experts.

Finding value of win listBox?

VM
How can I get the selected value of a Windows Listbox? It only has two items
(which I added through the Collection property) and everytime I get the
selectedIndex, it returns -1. If I use selectedvalue, I receive a Null
exception. From what I've read, the Text property can't be used (MS declared
it a bug).

Thanks.
Nov 16 '05 #1
3 1290
Are you sure something is actually selected? selectedIndex returns -1 if
nothing is highlighted, otherwise it would return the index of the selected
item.

Nathan Price

"VM" <vo******@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
How can I get the selected value of a Windows Listbox? It only has two items (which I added through the Collection property) and everytime I get the
selectedIndex, it returns -1. If I use selectedvalue, I receive a Null
exception. From what I've read, the Text property can't be used (MS declared it a bug).

Thanks.

Nov 16 '05 #2
VM
Can it be used without having to hightlight anything? Would it be possible
to just get the value of the listbox (like a combobox)?

Thanks.
"Nathan Price" <gn@gadgetnate.com> wrote in message
news:eA*************@TK2MSFTNGP10.phx.gbl...
Are you sure something is actually selected? selectedIndex returns -1 if
nothing is highlighted, otherwise it would return the index of the selected item.

Nathan Price

"VM" <vo******@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
How can I get the selected value of a Windows Listbox? It only has two

items
(which I added through the Collection property) and everytime I get the
selectedIndex, it returns -1. If I use selectedvalue, I receive a Null
exception. From what I've read, the Text property can't be used (MS

declared
it a bug).

Thanks.


Nov 16 '05 #3
Hi VM,

A ListBox doesn't have a default value like the ComboBox. It is possible to get the first value of the list if that is what you want (or any other value in the list). ListBox.Items[0] will return the first object in the list. You need to cast it to whatever type it is (usually a string).

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4

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

Similar topics

2
by: Remco Groot Beumer | last post by:
Hello, I created a program in which I use modules and classmodules for setting my variables. For example when I need to set the customerID in a variable I use something like: ...
9
by: Megan | last post by:
Hi- I'm creating a database of music bands with their cds and songs. I'm trying to program an SQL statement so that I can enter a string of text in a textbox, press the 'Enter' key, and have...
2
by: Allan Horwitz | last post by:
When I try to reference the lstproducts.selecteditem.value using autopostback from the lstproducts listbox I get an error? How can I fix my code to get the selected value from the lstproducts...
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,...
1
by: Ed Staffin | last post by:
I know about findstring and findstringexact, but what if I want to find an item based on value instead of text? Is my only option to iterate through the collection? For example, if I have a...
2
by: php_Boi | last post by:
i have designed an application that is a dynamic submission form. i have text fields and listboxes. now i am able to retain the values of the listboxes when i populate the listbox "manually"(single...
5
by: Randy | last post by:
Hi, I'm have a form with a listbox and a few textboxes on it. Based on the user's selection in the listbox, I want the bindingsource to update the textboxes with the corresponding values. I have...
3
by: BinaryGirl23 | last post by:
Hello everyone, I'm having a bit of a problem regarding Access 2003 which I really hope is an easy one to solve! I'm using a multiple-selection listbox control to look up values of one table and...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.