[Solved] Change component icon based on state change

Curious if anybody knows what to override in C# to explicitly assign the tab icon of my component? So I can have a different icon on the actual component than the one in the tab.
Capture

You must override the entire display of the component to achieve that. Are you really looking to change the icon completely, or do you want to draw something ‘extra’ on top of the icon to signal a specific state?

Are you really looking to change the icon completely, or do you want to draw something ‘extra’ on top of the icon to signal a specific state?

Yea exactly I want to signal a state change, but potentially with the icon (I know I can signal this with component message but I prefer icon change if possible).

What I have is a component that has an Activate(A) input as boolean. I am trying to have a different icon on the component itself depending on if this Boolean is true or false. The variable in the script is declared outside of the solve instance as “private bool A;” (because I also needed it for some other things outside of the solve instance which is all currently working as expected). In the solve instance I have the usual DA.GetData(0, ref A);

I was hoping for the best and thought I might be able to use the variable in an if statement to also change the icon based on this state like:

 protected override System.Drawing.Bitmap Icon
 {
    get
    {
       if(A)
       {
           return MyProject.Properties.Resources.OnIcon;
       }
       else
       {
           return MyProject.Properties.Resources.OffIcon;
       }
    }
 }

But it doesn’t work :smiley: (It doesn’t give an error but just always uses the icon from the else condition)

So I am wondering:
1.How to change the icon on a component from a state change?
2. If so then what place decides the fixed icon in the menu?

That’s because the Icon is only requested once and then cached (and converted into disabled images as well, by applying some bitmap filters).

You’ll need to override the attributes, and then either completely hijack the display (not recommended) or let it draw and then draw some more stuff on top. You can then choose to either draw a signalling icon, or draw an entirely different icon after you mask out the original, or even choose a different approach to signal the active state, such as a symbol along the top edge of the component.

You can of course also set a Message text, which is how most components signal state to the user.

1 Like

Thanks I’ll look into some things. It is not entirely important, more just poking around to try some stuff I am not so familiar with.

It works using this code

 protected override System.Drawing.Bitmap Icon
 {
    get
    {
       if(A)
       {
           return MyProject.Properties.Resources.OnIcon;
       }
       else
       {
           return MyProject.Properties.Resources.OffIcon;
       }
    }
 }

and then putting base.DestroyIconCache(); after my DA.GetData()'s in the SolveInstance.

A is constructed as private bool A; outside of the solve instance.

Thanks to the tip from @dave_stasiuk

3 Likes