Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

change the background color opacity of a button - c#

Posted on 2014-07-29
Last Modified: 2014-07-31
On a page I have bt1, btn2, bt3.

Via code I want to add opacity of .5 to btn1 and remove it from other two. Depending on the logic of the code, I may need to have btn2 with .5 opacity and the rest at 0.

Question: How is this done in code window?

Thank you.
Question by:Mike Eghtebas
  • 3
  • 2
LVL 21

Expert Comment

by:Randy Poole
ID: 40227882
bt1.BackColor = System.Drawing.Color.FromArgb(128,bt1.BackColor);

Open in new window

this will set the alpha to 50%
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40227945
How would one know this is 50% opacity? Knowing this will help to change to 25% or 75% later on when a need arises?

I do not see number 0.0 in your code.

Is FromArgb(64,bt1.BackColor); then 25%


BTW, after I tried this code, I don't see any opacity change.
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40231863
What kind of project is this? Web forms? Win Forms? WPF?
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

LVL 34

Author Comment

by:Mike Eghtebas
ID: 40231983
Web form.
LVL 75

Accepted Solution

käµfm³d   👽 earned 500 total points
ID: 40232016
You can add a style to your buttons in the code behind:

this.Button1.Style.Add("opacity", "1");
this.Button2.Style.Add("opacity", ".5");
this.Button3.Style.Add("opacity", "1");

Open in new window

The more preferable approach would be to use CSS classes:

this.Button1.CssClass = "opacity-100";
this.Button2.CssClass = "opacity-100";
this.Button3.CssClass = "opacity-50";

Open in new window

.opacity-50 {
    opacity: .5;

.opacity-100 {
    opacity: 1;

Open in new window

One problem you will have, though, is that the CssClass property is of type string, so if you have other CSS classes to add to your buttons, then you will need to do string manipulation in order to set the classes properly in your HTML.
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 40232194
Thank you for good solution and etc.


Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question