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

RadioButton in CommandBar

2 Answers 175 Views
CommandBar
This is a migrated thread and some comments may be shown as answers.
Angus Cheung
Top achievements
Rank 1
Angus Cheung asked on 24 Jun 2011, 12:19 PM

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 Answers, 1 is accepted

Sort by
0
Ivan Todorov
Telerik team
answered on 29 Jun 2011, 11:00 AM
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.
0
Angus Cheung
Top achievements
Rank 1
answered on 07 Jul 2011, 08:33 AM
Thank you.
Tags
CommandBar
Asked by
Angus Cheung
Top achievements
Rank 1
Answers by
Ivan Todorov
Telerik team
Angus Cheung
Top achievements
Rank 1
Share this question
or