如何更改Windows中所有按钮的属性? [英] How to change property of all buttons in windows from?
问题描述
我在 Form1
上有一些按钮.我想将其 FlatStyle
属性设置为 FlatStyle.Popup
.我搜索并编写了一些代码,如下所示:
I have some buttons on Form1
. I want to set their FlatStyle
property to FlatStyle.Popup
.
I searched and wrote some code as below:
// List<Control> ButtonsList = new List<Control>();
List<Button> ButtonsList = new List<Button>();
public Form1()
{
InitializeComponent();
this.Icon = Properties.Resources.autorun; //Project->Properties->Resources->
ButtonsList = GetAccessToAllButtons(this).OfType<Button>.ToList(); //*** hot line ***
foreach(Button btn in ButtonList)
{
btn.FlatStyle = FlatStyle.Popup;
}
}
public IEnumerable<Control> GetAccessToAllButtons(Control thisClass)
{
List<Control> ControlsList = new List<Control>();
foreach (Control child in thisClass.Controls)
{
ControlsList.AddRange(GetAccessToAllButtons(child));
}
ControlsList.Add(thisClass);
return ControlsList;
}
但是当我在代码的热线中使用 GetAccessToAllButtons()
时,VS会产生此错误:
But when I use GetAccessToAllButtons()
in hot line of my code, VS generates this error:
'System.Linq.Queryable.OfType(Query.Linq.IQueryable)'是一个'方法',在给定的上下文中无效
'System.Linq.Queryable.OfType(Query.Linq.IQueryable)' is a 'method', which is not valid in the given context
我怎么了?
我在此处的参考信息错过了()
.这是一个可以接受的答案!我可以参考其他情况吗?还是只是一个错字?
My reference at here missed the ()
. It is an accepted answer! Do we have a different situation at my reference? or it is only a typo?
推荐答案
OfType
是通用方法,您必须将其用作方法.只需将该行替换为以下内容:
OfType
is Generic Method and you must use it as a method.
Just replace that line to the following:
ButtonsList = GetAccessToAllButtons(this).OfType<Button>().ToList();
我也建议您编写如下方法:
Also I will recommend you to write method as below:
public List<Button> GetAllButtons(Form f)
{
List<Button> resultList = new List<Button>();
foreach(Control a in f.Controls)
{
if(a is Button)
{
resultList.Add((Button)a);
}
}
return resultList;
}
并以这种方式使用它:
var myBtns = GetAllButtons(yourForm);
foreach (var btn in myBtns)
{
btn.FlatStyle = FlatStyle.Popup;
}
这篇关于如何更改Windows中所有按钮的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!