Iterate trough RadRibbonBar

5 posts, 0 answers
  1. Michael
    Michael avatar
    6 posts
    Member since:
    Oct 2011

    Posted 14 Mar 2012 Link to this post

    Hi guys,

    I'm working on a program where I need to change Text and ToolTips of different RadRibbonBar Elements.
    The reason for this is that I need to be able to change the language on the fly.
    This came as a request for an already existing program.
    As the language will be changed for the whole program and all windows, I wanted to use a public function,
    that will be called when I press a button and when a window is loaded / get the focus.
    The Language information itself is stored in a database table.


    private void radButtonElementEnglish_Click(object sender, EventArgs e)
    {
         ChangeLanguage(this, "English")
    }


    Changing Text  and ToolTips on Textbox, RadTextBox or other controls is no problem. I just can't find a way to iterate though a RadRibbonbar. Same counts for Tooltips on a RadGridView.
    I added further table columns that hold names and types of RibbonBarElements as kind of a path to get to an element.
    "radRibbonBar.ribbonTab.radRibbonBarGroup", "radRibbonBar<Form.Name>.ribbonTabMain.radRibbonBarGroupSave"
    Still din't find a way to diretcly address a RadButtonElement for example.

    public void ChangeLanguage(Form frm, String Language = "English")
    {
     RadTextBox RadTextBoxTip = null;
     RadDropDownListCustom radDropDownListCustomTip = null;
     RadLabel radLabelTip = null;
     RadButtonElement radButtonElementTip = null;
     RadGridView radGridViewTip = null;
     SqlCommand commandGetData = null;
     try
     {
        commandGetData = SQLQuery();
        commandGetData.CommandText = "SELECT  Element_Name, Element_Typ, Element_Text_EN, ToolTip_Text_EN, Element_Text_DE, ToolTip_Text_DE, From Language WHERE Windowr_Name = @Window_Name";
        commandGetData.Parameters.AddWithValue("@Window_Name", frm.Name.ToString());
        commandGetData.Connection.Open();
        SqlDataReader DataReader = commandGetData.ExecuteReader();
        if (DataReader.HasRows)
        {
             while (DataReader.Read())
        {
         switch (DataReader["Element_Typ"].ToString())
         {
           case ("radTextBox"):
              RadTextBoxTip = (RadTextBox)frm.Controls.Find(DataReader["Element_Name"].ToString(), true)[0];
              if (Language == "English")
              {
            RadTextBoxTip.Text = DataReader["Element_Text_EN"].ToString();
            RadTextBoxTip.TextBoxElement.TextBoxItem.ToolTipText = DataReader["ToolTip_Text_EN"].ToString();
              }
              if (Language == "German")
              {
            RadTextBoxTip.Text = DataReader["Element_Text_DE"].ToString();
            RadTextBoxTip.TextBoxElement.TextBoxItem.ToolTipText = DataReader["ToolTip_Text_DE"].ToString();
              }
              break;
           case ("radLabel"):
                radLabelTip = (RadLabel)frm.Controls.Find(DataReader["Element_Name"].ToString(), true)[0];
            // same as radtextbox
            //radLabelTip.Text
            //radLabelTip.LabelElement.ToolTipText
            break;
           case ("radRibbonbonbar"):
            //..
            RadRibbonBar RadRibbonbarTip = (RadRibbonBar)frm.Controls.Find(DataReader["Element_Name"].ToString(), true)[0];
            //RadRibbonbarTip.Text = ..
            //No Tooltip here
            break;
           case ("ribbonTab"):
            //..
            break;
           case ("radButtonElement"):
                var c = GetAll(frm, typeof(RadRibbonBar));
            MessageBox.Show("Total Controls: " + c.Count());
            break;
           //more case:
           default:
            break;
         }
        }
        DataReader.NextResult();
           }
        commandGetData.Connection.Close();
          }
          catch (System.Data.SqlClient.SqlException ex)
          {
        // showEvents(ex);
          }
          finally
          {
        if (commandGetData != null) { commandGetData.Dispose(); }
          }
       }
     }
     
    public IEnumerable<Control> GetAll(Control control, Type type)
    {
       var controls = control.Controls.Cast<Control>();
       return controls.SelectMany(ctrl => GetAll(ctrl, type))
      .Concat(controls)
      .Where(c => c.GetType() == type);
    }

    There is another thread has some info but I'm still not able to figure out how to solve my problem
    http://www.telerik.com/community/forums/winforms/ribbonbar/findcontrol-to-find-a-radbuttonelement.aspx


    many thanks,
    -Michael

  2. Michael
    Michael avatar
    6 posts
    Member since:
    Oct 2011

    Posted 14 Mar 2012 Link to this post

    And why is there only a RibbonTab and no RadRibbonTab?
    -Michael
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Michael
    Michael avatar
    6 posts
    Member since:
    Oct 2011

    Posted 16 Mar 2012 Link to this post

    I found a way that doesn't make too much work.
    A further datafiled which contains the names of the individual elements (Element_Typ_Pfad_Name) helps to
    get down to individual elements like that:

    case ("radButtonElement"):
    try{
        String[] elementPathNames = DataReader["Element_Typ_Pfad_Name"].ToString().Split(new Char[] { '.' });
        RadRibbonBar rbr = (RadRibbonBar)frm.Controls.Find(elementPathNames[0], true)[0];
        RibbonTab rtab = (RibbonTab)rbr.CommandTabs[elementPathNames[1]];
        RadRibbonBarGroup rtgroup = (RadRibbonBarGroup)rtab.Items[elementPathNames[2]];
        RadButtonElement radButtonElementTip = (RadButtonElement)rtgroup.Items[DataReader["Element_Name"].ToString()];
        if (Language == "English")
        {
           radButtonElementTip.Text = DataReader["Element_Text_EN"].ToString();
           radButtonElementTip.ToolTipText = DataReader["ToolTip_Text_EN"].ToString();
        }
        //if (Language == "....
     
     
    }
    catch{}
    break;

    Saample content of the Element_Typ_Pfad_Name
    radRibbonBar1.ribbonTab1.radRibbonBarGroup1

    If you know a better way let me know

    -Michael

  5. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 16 Mar 2012 Link to this post

    Hello Michael,

    Thank you for writing.

    I have prepared a sample project which demonstrates how to iterate over all elements in the RadRibbonBar structure and change their text. 

    I hope you will find this useful. If you have further questions, do not hesitate to write back.

    Regards,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  6. Michael
    Michael avatar
    6 posts
    Member since:
    Oct 2011

    Posted 19 Mar 2012 Link to this post

    Hi Ivan,
    many thanks,
    this will be quite useful.

    -Michael
Back to Top
UI for WinForms is Visual Studio 2017 Ready