This is a migrated thread and some comments may be shown as answers.

foreach statement to search listbox items

2 Answers 670 Views
ComboBox and ListBox (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Faheem Nadeem
Top achievements
Rank 1
Faheem Nadeem asked on 28 Aug 2009, 07:33 AM
Hi,
I am encountering a problem with searching radlistbox items. MyCode:
private void searchListBox(object sender, EventArgs e) 
        { 
            int count = 0
            foreach (RadListBoxItem item in this.lbLiveClients) 
            { 
                if(item.DescriptionText == this.txtFindLiveClient.Text) 
                { 
                    this.lbLiveClients.SelectedIndex = count
                    break; 
                } 
                count++; 
            } 
        } 

gives an error:

foreach statement cannot operate on variables of type 'Telerik.WinControls.UI.RadListBox' because 'Telerik.WinControls.UI.RadListBox' does not contain a public definition for 'GetEnumerator'.

Any Solution to this:
I have items that have both text and description text. I wanted to search description text for some reason. then i decided on simple for loop.

            for (int i = 0; i < this.lbLiveClients.Items.Count; i++) 
            {  
                this.lbLiveClients.Items[i]. 
            } 

but no description text property there for items there, only the text property.
Any Solution to any of this.??
Thanks in advance

2 Answers, 1 is accepted

Sort by
0
Deyan
Telerik team
answered on 28 Aug 2009, 11:09 AM
Hi Faheem,

Thanks for writing and for your code snippets.

1. You cannot use the RadListBox control as a enumerated object in a foreach loop since it does not implement the IEnumerable interface. If you wish to enumerate all items in a RadListBox control with a foreach loop, you should change your code the following way:

private void searchListBox(object sender, EventArgs e)  
        {  
            int count = 0;  
            foreach (RadListBoxItem item in this.lbLiveClients.Items)  
            {  
                if(item.DescriptionText == this.txtFindLiveClient.Text)  
                {  
                    this.lbLiveClients.SelectedIndex = count;  
                    break;  
                }  
                count++;  
            }  
        }  

2. The RadListBoxItem class has a DescriptionText property which you can use to get or set the text which appears as a description for a RadListBoxItem object. You can access this property the following way (by using a foreach loop):

foreach (RadListBoxItem item in this.radListBox1.Items) 
    string descriptionText = item.DescriptionText; 

Or if you are using a for loop:

for (int i = 0; i < this.lbLiveClients.Items.Count; i++) 
    string descriptionText = (this.lbLiveClients.Items[i] as RadListBoxItem).DescriptionText;  
}  

I hope this help. Do not hesitate to write back if you need further assistance.
 

Regards,
Deyan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Faheem Nadeem
Top achievements
Rank 1
answered on 28 Aug 2009, 11:34 AM
Thankyou guys for your quick support i am really thankfull.
Thanks for the code snippets.
Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
Faheem Nadeem
Top achievements
Rank 1
Answers by
Deyan
Telerik team
Faheem Nadeem
Top achievements
Rank 1
Share this question
or