This is a migrated thread and some comments may be shown as answers.
Radbutton back color
8 Answers 125 Views
This is a migrated thread and some comments may be shown as answers.
Thanh
Top achievements
Rank 1
Thanh asked on 25 Jan 2016, 08:16 AM

Hi,

 I'm working with RadButton, (like attached image). When click on WorkWeek button, the background is changed as attached image, but after click it change to color as the color when mouse on hover the button. But I want when after click it still keep color as the color when I click, and then I click on another button (Week button for example), that backcolor is set for another button (not set for WorkWeek button any more). How can I do it?

 Look forward to your reply,

Pham Thanh.

8 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 25 Jan 2016, 02:50 PM
Hi Thanh,

Thank you for writing.

The BackColor property is usually set through a theme defining various colors for the different states. TPF exposes an API for overriding the theme settings at run-time preserving the styles defined for all the element states: Override Theme Settings at Run Time.

I created a simple example for toggling a color between two buttons inside RadRibbonBar. Please check my code snippet below: 
public partial class Form1 : Form
{
    private Color initialColor;
 
    public Form1()
    {
        InitializeComponent();
 
        this.initialColor = this.radButtonElement1.BackColor;
 
        this.radButtonElement1.Click += radButtonElement1_Click;
        this.radButtonElement2.Click += radButtonElement2_Click;
    }
 
    private void radButtonElement1_Click(object sender, EventArgs e)
    {
        this.ResetColor(this.radButtonElement2);
        this.ChangeBackColor(((RadButtonElement)sender));
    }
 
    private void radButtonElement2_Click(object sender, EventArgs e)
    {
        this.ResetColor(this.radButtonElement1);
        this.ChangeBackColor(((RadButtonElement)sender));
    }
 
    private void ResetColor(RadButtonElement button)
    {
        if (button.BackColor != this.initialColor)
        {
            button.ResetThemeValueOverrides();
        }
    }
 
    private void radButtonElement_Click(object sender, EventArgs e)
    {
        this.ChangeBackColor(((RadButtonElement)sender));
    }
 
    private void ChangeBackColor(RadButtonElement button)
    {
        button.SetThemeValueOverride(Telerik.WinControls.Primitives.FillPrimitive.BackColorProperty, Color.LightGreen, "",
            typeof(Telerik.WinControls.Primitives.FillPrimitive));
        button.SetThemeValueOverride(Telerik.WinControls.Primitives.FillPrimitive.GradientStyleProperty, GradientStyles.Solid, "",
            typeof(Telerik.WinControls.Primitives.FillPrimitive));
    }
}

I am also sending you a gif file showing the result on my end.

Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Thanh
Top achievements
Rank 1
answered on 26 Jan 2016, 06:43 AM

Thank for your code, it is really clear.

There is a small issue that I want to change to color as the color when button is clicked on. So that when user change theme, the color is changed according that theme. How can I get the color as the color when I click on button?

 Or telerik have anyway that I can set group of button, then it can mark the button have just clicked?

0
Hristo
Telerik team
answered on 26 Jan 2016, 02:07 PM
Hi Thanh,

Thank you for writing.

Themes define different styles and settings for the various element states. These styles are usually defined via repository items: Working with Repository Items.

In the context of the discussed scenario, you could read a particular setting knowing the responsible repository. Please check my code snippet below: 
private Color initialBackColor;
 
public Form1()
{
    InitializeComponent();
 
    Theme theme = ThemeRepository.FindTheme(this.radRibbonBar1.ThemeName);
    StyleRepository repository = theme.FindRepository("ButtonMouseDownFill");
    PropertySetting setting = repository.FindSetting("BackColor");
    this.initialBackColor = (Color)setting.EndValue;
}

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Thanh
Top achievements
Rank 1
answered on 27 Jan 2016, 04:09 AM

 I have used your code, the value of theme is "Office2013Light", and the repository is null

0
Hristo
Telerik team
answered on 27 Jan 2016, 04:04 PM
Hi Thanh,

Thank you for writing back.

As I said earlier the styles are usually defined through repository items and the different themes for some settings use unique repositories. This way they define their own style.

Our VSB tool provides information about the theme settings. You can read the name of a repository and at run-time find it and use its style according to your scenario. Considering the Office2013Theme, you can get the back color this way: 
this.radRibbonBar1.ThemeName = "Office2013Light";
Theme theme = ThemeRepository.FindTheme(this.radRibbonBar1.ThemeName);
StyleRepository repository = theme.FindRepository("FillSolid(146;192;224)");
PropertySetting setting = repository.FindSetting("BackColor");
this.initialBackColor = (Color)setting.Value;

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Thanh
Top achievements
Rank 1
answered on 28 Jan 2016, 07:48 AM
Thanks for your code. I got the color I want. :)
0
Craig
Top achievements
Rank 2
answered on 30 Sep 2016, 07:10 PM
Does this still work in 2016.3? I am trying it for a CommandBarButton but nothing happens and if I don't reset it, the next time through the color change isn't reflected in the setting value.
0
Hristo
Telerik team
answered on 03 Oct 2016, 03:51 PM
Hi Craig,

Thank you for writing.

Yes, it is working. The CommandBarButton, however, has a different hierarchy and its back color is directly set without creating a fill primitive. You can change it in the default state like this: 
this.commandBarButton1.SetThemeValueOverride(VisualElement.BackColorProperty, Color.LightGreen, "");

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms. For more information check out this blog post and share your thoughts.
Asked by
Thanh
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Thanh
Top achievements
Rank 1
Craig
Top achievements
Rank 2
Share this question
or