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

CommandBarDropDownList FindItem

3 Answers 151 Views
CommandBar
This is a migrated thread and some comments may be shown as answers.
Xander
Top achievements
Rank 1
Xander asked on 01 Mar 2011, 08:12 PM
Hello, it there any way how to find CommandBarDropDownList Item by string value?
I need following functionality. but I miss FindItem function.
CommandBarDropDownList c = new CommandBarDropDownList();
c.Items.Add("a");
c.Items.Add("b");
c.Items.Add("c");
  
c.SelectedItem = c.FindItem("b");

Only way I know is to loop over c.Items and check, but I expect some easier way in components for commandbars (and it was also included in RadToolStrip and is in standard MS).

Thanks for any help, I had to overlook it somewhere, but documentation is quite brief and not many examples.
Zbynek

3 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 02 Mar 2011, 09:31 AM
Hello Xander,

Sadly there is no FindString() or FindStringExact() methods in the CommandBarDropDown, but in the case you mentined you can just use c.SelectedText = "b";
or you can always use linq to peform searches, maybe even an extension method, like so:
public static class CommandBarDropDownExtensions
{
    public static RadListDataItem FindItem(this CommandBarDropDownList dropDown, string str)
    {
        return dropDown.Items.Where(i => i.Text.Contains(str)).FirstOrDefault();
    }
}

and then use it like:
c.SelectedItem = c.FindItem("b");
Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Peter
Telerik team
answered on 04 Mar 2011, 09:55 AM
@Emanuel, thank you for your suggestion

Hi Xander, actually there are FindItem and FindStringExact methods in CommandBarDropDown. However, you should access them using the DropDownListElement property. Consider the following sample:

c.SelectedIndex = c.DropDownListElement.FindStringExact("a");

I hope this helps. In case you have further questions, I will be glad to be of assistance.

Regards,
Peter
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Xander
Top achievements
Rank 1
answered on 10 Mar 2011, 10:40 AM
Thanks, that's a lot easier.
Tags
CommandBar
Asked by
Xander
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Peter
Telerik team
Xander
Top achievements
Rank 1
Share this question
or