How do I programatically select a tab in RichTextEditorRibbonBar?

2 posts, 1 answers
  1. Pavel
    Pavel avatar
    32 posts
    Member since:
    Sep 2016

    Posted 27 Dec 2016 Link to this post

    I am adding a new tab to the RichTextEditorRibbonBar like this:

    var lblTempalteName = new RadLabelElement() {
        Text = "Template Name:",
        AutoSize = false,
        Size = new Size(120, 20)};
    var tbxTemplateName = new RadTextBoxElement() {
        Size = new Size(100, 25),
        Padding = new Padding(2) };
    var templateNameGroup = new RadRibbonBarButtonGroup();
    templateNameGroup.Items.AddRange(new RadItem[] { lblTempalteName, tbxTemplateName });
    var lblDataSource = new RadLabelElement() {
        Text = "Data Source:",
        AutoSize = false,
        Size = new Size(120, 20)};
    var ddlDataSource = new RadDropDownListElement() {
        Size = new Size(100, 25),
        DropDownStyle = RadDropDownStyle.DropDownList };
    var dataSourceGroup = new RadRibbonBarButtonGroup() { Margin = new Padding(0, 3, 0, 0) };
    dataSourceGroup.Items.AddRange(new RadItem[] { lblDataSource, ddlDataSource });
    var rbgProperties = new RadRibbonBarGroup()
        Text = "Properties",
        Orientation = Orientation.Vertical
    rbgProperties.Items.AddRange(new RadItem[] { templateNameGroup, dataSourceGroup });
    var templatePropertiesTab = new RichTextEditorRibbonTab() { Text = "Template properties", IsSelected = false};

    The tab that I am adding becomes selected, which isn't what I want. How do I ensure that the "Home" tab is selected. Here's what I tried, but it doesn't work.

    foreach (RibbonTab item in richTextEditorRibbonBar1.CommandTabs)
        item.IsSelected = (item.AccessibleName == "Home");
  2. Answer
    Lance | Principal TSE
    Lance | Principal TSE avatar
    1043 posts

    Posted 27 Dec 2016 Link to this post

    Hello Pavel,

    You don't need to iterate over the tab collection to get the Home tab, here's another approach using index:

    var homeTab = richTextEditorRibbonBar1.CommandTabs[0] as RichTextEditorRibbonTab;
    if (homeTab != null)
        homeTab.IsSelected = true;

    That will select the home tab, I've attached a small runnable demo for you to see it working. 

    If using the index can't work for you and you have to iterate over the CommandTabs collection, I recommend using a foreach with a var and then do a safe cast inside the loop, like this:

    foreach (var commandTab in richTextEditorRibbonBar1.CommandTabs)
        var tab = commandTab as RichTextEditorRibbonTab;
        if (tab?.AccessibleName == "Home")
            tab.IsSelected = true;

    Let us know if you have any further questions. Thank you for contacting Support and for choosing Telerik by Progress.

    Lance | Tech Support Engineer, Sr.
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top