简体   繁体   English

如何在C#中更改按钮的后颜色

[英]How to Change button back color in c#

I have many Buttons in DataGrid 我在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. 我想将Button的颜色设置为Green,将Button.Text设置为白色(不是全部用于1个按钮),前提是我已经使用ITextSharp创建PDF生成了,我评论了iTextSharp头文件结果,但这次必须出现以下错误,因此我必须在代码中使用iTextSharp。

"Cannot implicitly convert type iTextSharp.text.Color to System.Drawing.Color" “无法将类型iTextSharp.text.Color隐式转换为System.Drawing.Color”

This is my iTextSharp header file 这是我的iTextSharp头文件

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. 您正在iTextSharp.text命名空间中引用Color类型。 Try specifying the namespace explicitly: 尝试显式指定名称空间:

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

You can use the Button.BackColor property 您可以使用Button.BackColor属性

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 更正: OP的问题标题具有误导性,并且以上内容基于此,因此问题正文中的解释答案将与OP给出的答案相同

btn.ForeColor = Color.Red; //don't see the reason why this should not work //看不到为什么它不起作用的原因

To change the BackColor use: 要更改BackColor,请使用:

Button1.BackColor = Color.Red;

To change the ForeColor use: 要更改ForeColor,请使用:

Button1.ForeColor = Color.Red;

You can use both of them for the MouseMove event. 您可以将它们都用于MouseMove事件。

To reset both of them use the MouseLeave event with this code: 若要重置它们两者,请使用带有以下代码的MouseLeave事件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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