foreach statement to search listbox items

3 posts, 0 answers
  1. Faheem Nadeem
    Faheem Nadeem avatar
    14 posts
    Member since:
    Jul 2009

    Posted 28 Aug 2009 Link to this post

    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. Deyan
    Admin
    Deyan avatar
    2037 posts

    Posted 28 Aug 2009 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Faheem Nadeem
    Faheem Nadeem avatar
    14 posts
    Member since:
    Jul 2009

    Posted 28 Aug 2009 Link to this post

    Thankyou guys for your quick support i am really thankfull.
    Thanks for the code snippets.
Back to Top