【问题标题】:How to Change button back color in c#如何在c#中更改按钮的背景颜色
【发布时间】:2012-08-20 04:22:06
【问题描述】:

我在 DataGrid 中有很多按钮

我想设置 根据我的 if 条件,按钮的颜色变为绿色,Button.Text 变为白色(并非全部,仅适用于 1 个按钮) 我已经使用 ITextSharp 来创建 PDF 生成,我评论了 iTextSharp 头文件,我得到了结果,但是这次我的代码中必须需要 iTextSharp 出现以下错误。

“无法将 iTextSharp.text.Color 类型隐式转换为 System.Drawing.Color”

这是我的 iTextSharp 头文件

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

这是代码

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

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

                }

请帮帮我

【问题讨论】:

  • 你明白了,因为它们是不同的类型。如果值相同,则可以选择适合您的类型。

标签: c# background-color


【解决方案1】:

要更改背景颜色,请使用:

Button1.BackColor = Color.Red;

要更改前景色,请使用:

Button1.ForeColor = Color.Red;

您可以将它们都用于 MouseMove 事件。

要重置它们,请使用带有以下代码的 MouseLeave 事件:

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

【讨论】:

    【解决方案2】:

    您可以使用 Button.BackColor 属性

    例子:

    btn.BackColor = Color.Green;
    

    更正: OP 的问题标题具有误导性,以上内容基于此 因此,问题正文中解释的答案将与 OP 给出的答案相同

    btn.ForeColor = Color.Red;//看不出这不起作用的原因

    【讨论】:

    • 他们想改变前景色,但是输入不同的类型是个问题
    • 我理解 -1 我的回答是基于 OP 问题的标题,但 op 的问题与他的标题完全不同且具有误导性
    • @HatSoft:我同意,+1,也许编辑你的答案以包括两者
    • 我没有 -1 你,但你是对的标题和细节不匹配
    • 为支持我的家伙干杯
    【解决方案3】:

    您正在 iTextSharp.text 命名空间中引用 Color 类型。尝试明确指定命名空间:

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

    【讨论】:

    • 我使用了您的代码,但我收到此错误“对象引用未设置为对象的实例”。
    • 那么btn 可能是null。检查上面的FindControl 调用。
    • 是的 FindControl 值为空,我采用单个网格视图 FindControl 值很好。在这种情况下,它是一个内部网格视图。我正在使用父 GridView 的 RowDataBound 事件来绑定子 GridView。我的问题是,如何在 Parent gridViews RowDataBound 事件中获取 Child GridView 的 Button findcontrol 值。 @Steve Czetty,如果有人知道,请帮我解决这个问题。提前谢谢。
    猜你喜欢
    • 2018-01-04
    • 2011-06-26
    • 2013-09-15
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 2021-06-22
    • 2015-04-03
    相关资源
    最近更新 更多