C# 按钮背景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11055414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:18:48  来源:igfitidea点击:

Button background colour

c#asp.netvisual-studio-2008

提问by Anna T

I need to change the backgroundcolour of a button using C# code (Visual Studio 2008).

我需要background使用 C# 代码(Visual Studio 2008)更改按钮的颜色。

I saw some people recommending the inclusion of a directive: using System.Windows.Media; - I tried it and it triggered this error: Windows does not exist in namespace System. I tried several combinations like:

我看到有些人推荐一个指令包含:using System.Windows.Media; - 我试过了,它触发了这个错误:Windows 不存在于namespace System. 我尝试了几种组合,例如:

 btn.BackColor = Color.Red;
 btn.Background = Brushes.Green;

And neither is working. Is a special directive needed for using colour? What code do you suggest. Thanks a lot.

两者都不起作用。使用颜色是否需要特殊指令?你建议什么代码。非常感谢。

采纳答案by Shyju

This should change your button background color to Red

这应该将您的按钮背景颜色更改为红色

yourButtonName.BackColor = Color.Red;

You need to include System.Drawingnamespace as Color class belongs to that. Like this

您需要包含System.Drawing命名空间,因为 Color 类属于该命名空间。像这样

using System.Drawing;

And ofcourse you need to add the reference to System.DrawingDLL in your project to use this namespace and Color class.

当然,您需要System.Drawing在项目中添加对DLL的引用才能使用此命名空间和 Color 类。

enter image description here

在此处输入图片说明

回答by Limey

using System.Drawing;

allows you to use

允许你使用

Color.Red;

回答by MMK

Button1.BackColor = System.Drawing.Color.Red; 

回答by MMK

try this

尝试这个

button.BackColor = Color.Red

回答by Jeremy

Although that property is technically available to all WebControls, it maybe ignored for buttons (I haven't confirmed). This, of course, is assuming your project is ASP.NET. Having said that, if it DOES happen to work for you, I highly advise that you test this in other browsers as it may be MS specific.

尽管该属性在技术上适用于所有 WebControl,但按钮可能会被忽略(我尚未确认)。当然,这是假设您的项目是 ASP.NET。话虽如此,如果它碰巧适合您,我强烈建议您在其他浏览器中测试它,因为它可能是特定于 MS 的。

From MSDN:

来自 MSDN:

This property will render for only certain controls. For example, Table, Panel, DataGrid, Calendar, and ValidationSummary will render this property. It will also work for CheckBoxList, RadioButtonList and DataList if their RepeatLayout property is RepeatLayout.Table, not RepeatLayout.Flow.

此属性将仅呈现某些控件。例如,Table、Panel、DataGrid、Calendar 和 ValidationSummary 将呈现此属性。如果 CheckBoxList、RadioButtonList 和 DataList 的RepeatLayout 属性是RepeatLayout.Table,而不是RepeatLayout.Flow,则它们也适用。

Source: WebControl.BackColor Property

来源:WebControl.BackColor 属性

However, a more flexible and widely practiced method of achieving this is to use CSS.

然而,实现这一点的更灵活和广泛实践的方法是使用 CSS。

回答by Viraj Hasith

On PageLoad try this,

在 PageLoad 上试试这个,

Button1.Style.Add("background-color", "green");

Button1.Style.Add("background-color", "green");

Use a method to write a condition when you want to change the color of the button. if condition is true use above code to change the color of the button as you prefer.

当你想改变按钮的颜色时,使用一种方法来写一个条件。如果条件为真,请使用上面的代码根据需要更改按钮的颜色。