Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


change the background color opacity of a button - c#

Posted on 2014-07-29
Medium Priority
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
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
  • 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?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 34

Author Comment

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

Accepted Solution

käµfm³d   👽 earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

715 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