Solved

change the background color opacity of a button - c#

Posted on 2014-07-29
6
1,257 Views
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.
0
Comment
Question by:Mike Eghtebas
  • 3
  • 2
6 Comments
 
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%
0
 
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%

Mike

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

Expert Comment

by:käµfm³d 👽
ID: 40231863
What kind of project is this? Web forms? Win Forms? WPF?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Author Comment

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

Accepted Solution

by:
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


your.css
.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.
0
 
LVL 33

Author Closing Comment

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

Mike
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

912 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now