Neeraj
Neeraj

Reputation: 497

How to Change button back color in c#

I have many Buttons in DataGrid

I want to set Button's color become Green and the Button.Text become white ( not for all, only for 1 button) in the basis of my if condition I alredy used ITextSharp for creating PDF generation,i commented the iTextSharp header files i get the result but i must need iTextSharp in my code this time bellowing error is occured.

"Cannot implicitly convert type iTextSharp.text.Color to System.Drawing.Color"

This is my iTextSharp header file

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;

This is the code

            if (dsRecAdj.Tables[2].Rows.Count > 0)
                {

                    Button btn = (Button)e.Row.FindControl("btnSalvage");
                    btn.ForeColor = Color.Red;

                }

Some Body please help me

Upvotes: 0

Views: 23732

Answers (3)

Ady
Ady

Reputation: 61

To change the BackColor use:

Button1.BackColor = Color.Red;

To change the ForeColor use:

Button1.ForeColor = Color.Red;

You can use both of them for the MouseMove event.

To reset both of them use the MouseLeave event with this code:

Buttton1.BackColor = SystemColors.ButtonFace;
Button1.ForeColor = default(Color);
Button1.UseVisualStyleBackColor = true;

Upvotes: 1

HatSoft
HatSoft

Reputation: 11191

You can use the Button.BackColor property

Example :

btn.BackColor = Color.Green;

Correction : OP's question title is misleading and the above is based on that So the answer for the explanation in body of the question will be same as what OP have given

btn.ForeColor = Color.Red;//don't see the reason why this should not work

Upvotes: 1

Steve Czetty
Steve Czetty

Reputation: 6228

You are referencing the Color type in the iTextSharp.text namespace. Try specifying the namespace explicitly:

btn.ForeColor = System.Drawing.Color.Red;

Upvotes: 5

Related Questions