简体   繁体   中英

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

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

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

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM