Radbutton back color

9 posts, 0 answers
  1. Thanh
    Thanh avatar
    9 posts
    Member since:
    Dec 2015

    Posted 25 Jan Link to this post

    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.

  2. Hristo
    Admin
    Hristo avatar
    716 posts

    Posted 25 Jan Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Thanh
    Thanh avatar
    9 posts
    Member since:
    Dec 2015

    Posted 26 Jan Link to this post

    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?

  5. Hristo
    Admin
    Hristo avatar
    716 posts

    Posted 26 Jan Link to this post

    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
  6. Thanh
    Thanh avatar
    9 posts
    Member since:
    Dec 2015

    Posted 26 Jan in reply to Hristo Link to this post

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

  7. Hristo
    Admin
    Hristo avatar
    716 posts

    Posted 27 Jan Link to this post

    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
  8. Thanh
    Thanh avatar
    9 posts
    Member since:
    Dec 2015

    Posted 28 Jan in reply to Hristo Link to this post

    Thanks for your code. I got the color I want. :)
  9. Craig
    Craig avatar
    6 posts
    Member since:
    Mar 2014

    Posted 30 Sep in reply to Hristo Link to this post

    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.
  10. Hristo
    Admin
    Hristo avatar
    716 posts

    Posted 03 Oct Link to this post

    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.
Back to Top
UI for WinForms is Visual Studio 2017 Ready