Click here to Skip to main content
15,916,280 members
Home / Discussions / C#
   

C#

 
Generalevents hold objects Pin
bob dole++28-Feb-05 14:39
bob dole++28-Feb-05 14:39 
QuestionHow to format the string Pin
oohungoo28-Feb-05 14:26
oohungoo28-Feb-05 14:26 
AnswerRe: How to format the string Pin
Rob Graham28-Feb-05 15:33
Rob Graham28-Feb-05 15:33 
Generalread Com+ components Pin
28-Feb-05 11:22
suss28-Feb-05 11:22 
Generalsql connection path help i need - pl see the code inside Pin
chandtec28-Feb-05 10:58
chandtec28-Feb-05 10:58 
GeneralConvert ListBox selectedvalue into INT Pin
vibyvej28-Feb-05 9:24
vibyvej28-Feb-05 9:24 
GeneralRe: Convert ListBox selectedvalue into INT Pin
Kwai Cheng Kane28-Feb-05 10:26
Kwai Cheng Kane28-Feb-05 10:26 
GeneralRe: Convert ListBox selectedvalue into INT Pin
vibyvej28-Feb-05 11:31
vibyvej28-Feb-05 11:31 
Thanks for your reply.

This is a Windows Forms application.

I use "ltbxLeft.DisplayMember" to display company name, use "ltbxLeft.ValueMember" to get the ID of the company:

ltbxLeft.DataSource = this.dataSet.Tables[tableName];
ltbxLeft.DisplayMember = columnName;
ltbxLeft.ValueMember = columnID;

By calling this method: "private void ltbxLeft_SelectedValueChanged(object sender, System.EventArgs e)", I can get:
ltbxLeft.SelectedIndex, ltbxLeft.SelectedValue, and ltbxLeft.SelectedItem

Everything works just fine, except that I cann't convert the ltbxLeft.SelectedValue, (which is an integer/the company ID in the table), into an integer, by whatever means (int.Parse; Convert.Int32,.....).

ltbxLeft.SelectedIndex gives me the number of the clicked listbox item (0,1,2...);
ltbxLeft.SelectedItem gives me the company name (by using dr = (DataRowView)(ltbxLeft.SelectedItem) to get it).

I can also get a string from ltbxLeft.SelectedValue.ToString() and display it in a textbox.

But I cann't convert this string into integer.

I have read Microsoft's example on http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformslistcontrolclassselectedvaluechangedtopic.asp

which is about "ListControl.SelectedValueChanged Event". I have run the example, tried to modify the code and tried to convert the string in the example. I got the same problem!

Jeff Levinson wrote the following in DotNetMonster (http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-general/762/ListBox-Problem).

I think he is right. But I donn't know how to use thte CType. I got a namespace problem with Ctype.

Jeff Levinson------------
The key thing to remember in .NET is that combo and list
boxes do not store simple values, they store types. So
what you've done in this situation is you've bound a
dataset or a datatable to the listbox. You've set it up
to display one value by setting the DisplayMember
property. At this point you can do two things:
1. Set the ValueMember property (and use the
Listbox.SelectedValue property to retrieve the
information which returns a discreet value) or
2. Perform a ctype on the selected item to convert it to
a datarow and then extract the value you want.

Option 2 is more flexible, but Option 1 is a faster
operation.


GeneralRe: Convert ListBox selectedvalue into INT Pin
Md Saleem Navalur28-Feb-05 19:41
Md Saleem Navalur28-Feb-05 19:41 
GeneralRe: Convert ListBox selectedvalue into INT Pin
vibyvej28-Feb-05 20:18
vibyvej28-Feb-05 20:18 
GeneralQuick Question… Pin
HahnTech28-Feb-05 9:13
HahnTech28-Feb-05 9:13 
GeneralRe: Quick Question… Pin
Dave Kreskowiak28-Feb-05 9:37
mveDave Kreskowiak28-Feb-05 9:37 
GeneralRe: Quick Question… Pin
adocoder28-Feb-05 10:12
adocoder28-Feb-05 10:12 
GeneralRe: Quick Question… Pin
HahnTech28-Feb-05 10:17
HahnTech28-Feb-05 10:17 
GeneralRe: Quick Question… Pin
adocoder28-Feb-05 10:17
adocoder28-Feb-05 10:17 
GeneralDataGrid with indeterminate number of column. Need Your Help! Pin
adocoder28-Feb-05 9:07
adocoder28-Feb-05 9:07 
GeneralSepia ColorMatrix Pin
holl708828-Feb-05 8:24
holl708828-Feb-05 8:24 
GeneralRe: Sepia ColorMatrix Pin
frankforward16-Sep-09 4:00
frankforward16-Sep-09 4:00 
GeneralRe: Sepia ColorMatrix Pin
frankforward16-Sep-09 4:01
frankforward16-Sep-09 4:01 
GeneralRe: Sepia ColorMatrix Pin
Don Grout4-Aug-11 9:55
Don Grout4-Aug-11 9:55 
GeneralSerialization. Pin
fmarcos28-Feb-05 8:12
fmarcos28-Feb-05 8:12 
GeneralRe: Serialization. Pin
turbochimp28-Feb-05 9:45
turbochimp28-Feb-05 9:45 
GeneralRe: Serialization. Pin
fmarcos1-Mar-05 1:31
fmarcos1-Mar-05 1:31 
GeneralDataSet problem Pin
Kwai Cheng Kane28-Feb-05 7:39
Kwai Cheng Kane28-Feb-05 7:39 
GeneralRe: DataSet problem Pin
Rob Graham28-Feb-05 8:29
Rob Graham28-Feb-05 8:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.