RadioButton in CommandBar

3 posts, 0 answers
  1. Angus Cheung
    Angus Cheung avatar
    13 posts
    Member since:
    Aug 2009

    Posted 24 Jun 2011 Link to this post

    Hi,

     

    I have just upgraded my RadControls for WinForms from Q1 2010 SP1 to Q1 2011.

    Since I have been using RadToolStrips in my project, I got warnings telling me to use CommandBars instead of RadToolStrip.

    So, I tried switching all the RadToolStrips to CommandBars, but I noticed the only way to add RadioButtons in a CommandBar is to use CommandBarHostItem. However, I couldn't put more than 1 RadioButton in a CommandBarHostItem. I had to create 1 CommandBarHostItem for each RadioButton, and if I do it that way, multiple RadioButtons can be selected at one time, which defeated my purpose of using RadioButtons because I want only one button in a group of radio buttons can be selected at one time.

    Are there any solutions for my case?

     

    Thanks

  2. Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 29 Jun 2011 Link to this post

    Hello Angus Cheung,

    Thank you for your question.

    Here is an extension of RadRadioButtonElement  which can be used in RadCommandBar:
    public class CommandBarRadioButtonElement : RadRadioButtonElement
    {
        public string RadioGroup
        {
            get;
            set;
        }
     
        protected override void OnToggleStateChanged(StateChangedEventArgs e)
        {
            if (this.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On)
            {
                RadElement current = this;
     
                while (current.Parent != null)
                {
                    current = current.Parent;
                }
     
                this.CheckElementTree(current);
            }
        }
     
        private void CheckElementTree(Telerik.WinControls.RadElement element)
        {
            if (element == null)
            {
                return;
            }
     
            foreach (RadElement child in element.Children)
            {
                CommandBarRadioButtonElement radioButton = (child as CommandBarRadioButtonElement);
                if (radioButton != null && radioButton.RadioGroup == this.RadioGroup && radioButton!=this)
                {
                    radioButton.ToggleState = Telerik.WinControls.Enumerations.ToggleState.Off;
                }
     
                CheckElementTree(child);
            }
        }
     
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(RadRadioButtonElement);
            }
        }
    }

    You can use this class with CommandBarHostItems the following way:
    this.commandBarHostItem1.HostedItem = new CommandBarRadioButtonElement() { RadioGroup = "Group1", Text = "Radio1" };
    this.commandBarHostItem2.HostedItem = new CommandBarRadioButtonElement() { RadioGroup = "Group1", Text = "Radio2" };
    this.commandBarHostItem3.HostedItem = new CommandBarRadioButtonElement() { RadioGroup = "Group2", Text = "Radio3" };
    this.commandBarHostItem4.HostedItem = new CommandBarRadioButtonElement() { RadioGroup = "Group2", Text = "Radio4" };

    I hope you find this useful. Do not hesitate to ask if you have any additional questions.

    Greetings,
    Ivan Todorov
    the Telerik team
    Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Angus Cheung
    Angus Cheung avatar
    13 posts
    Member since:
    Aug 2009

    Posted 07 Jul 2011 Link to this post

    Thank you.
Back to Top