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 33

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?
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

LVL 33

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 33

Author Closing Comment

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


Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 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