c# - 如何更改 WinForms 面板中几个按钮的 FlatStyle MouseDownBackColor?
问题描述
我需要解析面板中的控件以更新一些按钮。我不明白如何访问它来更改按钮平面样式鼠标向下的颜色
public Color MouseDownBackColor {get; set;}
我知道我可以使用this.button1.FlatAppearance.MouseDownBackColor =
,但在这种情况下,我可以从面板作为 var 访问按钮,并且不能以这种方式访问它。
更新:
foreach (Control control in button_panel.Controls)
{
if (control is Button)
{
var button = (Button)control;
button.FlatAppearance.MouseOverBackColor = Color.FromArgb(0, 0, 0, 0);
}
}
解决方案
按钮的FlatStyle
属性必须设置FlatStyle.Flat
为工作。
您可以在类型检查之后使用类型转换(取消装箱) :Button
foreach (Control control in button_panel.Controls)
{
if (control is Button)
{
var button = (Button)control;
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.MouseDownBackColor = Color.Yellow;
}
}
您也可以使用 Linq 编写此代码:
using System.Linq;
button_panel.Controls.OfType<Button>().ToList().ForEach(button =>
{
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.MouseDownBackColor = Color.Yellow;
});
推荐阅读
- javascript - 如何打印 HTML 表格
- typescript - 如何避免 TS 三斜杠指令中的丑陋导入
- javascript - 在 Node.js 中获取嵌套 SQL 查询的空响应
- azure - 部署后自动删除 Azure 虚拟机
- javascript - 为什么我的数组参数不在 POST 请求中?
- c++ - 如何提高 Prime Generator(SPOJ) 解决方案的效率?
- c++ - “原始计算器” - 创建除法循环的问题
- javascript - 为什么 jQuery 禁用的元素在 jQuery mobile 中看起来是可触摸的?
- c++ - 使用多个 CDHtmlDialogs 保持未释放的内存。(导航相同的 Url)
- reactjs - react-testing-library - 使用 useContext 钩子的测试组件 - 上下文在测试之间持续存在