list box checked names

  • Thread starter Thread starter Hrvoje Voda
  • Start date Start date
H

Hrvoje Voda

I got the index of all checked values, but I don't know how to read data of
that value?

For example.. in list box I have Names of users. First is John(index 1),
Second is Jack(index 2) and so on...
How to get the names ?

Hrcko
 
Hi,

If you have all the indexes you can use the following statements:

YourList.Items[index].Text --> First column
YourList.Items[index].SubItems[columnIndex].Text --> other columns

Hope this helps
SAlva

Cheers
Salva
 
I don't have Text after items.
I'm using checked ListBox.


Salvador said:
Hi,

If you have all the indexes you can use the following statements:

YourList.Items[index].Text --> First column
YourList.Items[index].SubItems[columnIndex].Text --> other columns

Hope this helps
SAlva

Cheers
Salva



Hrvoje Voda said:
I got the index of all checked values, but I don't know how to read data
of
that value?

For example.. in list box I have Names of users. First is John(index 1),
Second is Jack(index 2) and so on...
How to get the names ?

Hrcko
 
FIrst of all, when talking about a control, it's vitally important that
you say if you are using a WinForms control (for Windows apps), or a
WebControl (for ASP.NET apps). They are quite different, but they often
have the same names.

So, I'm going to assume that you are using a WinForms CheckedListBox
control, and that you've gotten the list of indexes via
checkedListBox.CheckedIndices property. Well, the simpliest thing is to
ignore that and use checkedListBox.CheckedItems which gives you exactly what
you want.

If you really want to continue using checkedListBox.CheckedIndices, you
can access the items via the Items property:

foreach( int i in checkedListBox.CheckedIndices)
{
string name = checkedListBox.Items as string;
}
 
The CheckedItemCollection stores objects so you need to cast each item to
its original type, which in your case appears to be System.String

foreach(int i in checkedListBox1.CheckedIndices)
{
string s = (string)checkedListBox1.Items;
// do something with s
}
 
This doesn't work.

As a result I get : System.Windows.Forms.ListBox+SelectedObjectCollect

But, when I put items in listBoxCollection (in properties) then I get a
result.

Is it a problem because I'm using dataset?


James Curran said:
FIrst of all, when talking about a control, it's vitally important that
you say if you are using a WinForms control (for Windows apps), or a
WebControl (for ASP.NET apps). They are quite different, but they often
have the same names.

So, I'm going to assume that you are using a WinForms CheckedListBox
control, and that you've gotten the list of indexes via
checkedListBox.CheckedIndices property. Well, the simpliest thing is to
ignore that and use checkedListBox.CheckedItems which gives you exactly
what
you want.

If you really want to continue using checkedListBox.CheckedIndices, you
can access the items via the Items property:

foreach( int i in checkedListBox.CheckedIndices)
{
string name = checkedListBox.Items as string;
}

Hrvoje Voda said:
I got the index of all checked values, but I don't know how to read data of
that value?

For example.. in list box I have Names of users. First is John(index 1),
Second is Jack(index 2) and so on...
How to get the names ?
 
Back
Top