如何使用c#更改窗口窗体上所有控件的字体系列 [英] how to change font family of all control on window form using c#
本文介绍了如何使用c#更改窗口窗体上所有控件的字体系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
先生,我有问题的解决方案
解决方案
试试这样..
private void Form1_Load( object sender,EventArgs e)
{
List< Control> allControls = GetAllControls( this );
allControls.ForEach(k => k.Font = new System.Drawing.Font( Verdana, 12 ));
}
private 列表< Control> GetAllControls(控件容器,列表<控制>列表)
{
foreach (控制c in container.Controls)
{
if (c.Controls.Count > 0 )
list = GetAllControls(c,list);
else
list.Add(c);
}
return list;
}
私人列表<控制> GetAllControls(控件容器)
{
return GetAllControls(容器,新列表<控制>());
}
对于通常的替代方法,让我们使用堆栈抓取所有控件...不按常规方式进行递归: pre lang =cs> 使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Windows.Forms;
public static class SpecialMethods
{
public static IEnumerable< Control> GetAllControls(控制aControl)
{
Stack< Control> stack = new Stack< Control>();
stack.Push(aControl);
while (stack.Any())
{
var nextControl = stack.Pop();
foreach (控制childControl nextControl.Controls)
{
stack.Push(childControl);
}
yield return nextControl;
}
}
}如果我们想为表单上的所有控件设置一个特定的字体,我们可以在表单的加载事件中使用这样的东西:
Font theFont = new 字体( Arial, 9 .0F,FontStyle.Bold);
foreach (在中控制theControl (SpecialMethods.GetAllControls( this )))
{
theControl.Font = theFont;
}如果我们只想设置按钮控件的字体:
foreach(在(SpecialMethods.GetAllControls(this)中控制theControl)。OfType < 按钮 > ()。ToList() )
{
theControl.Font = theFont;
}确保表格引用Linq。
来源:这篇文章,2009年,StackOverFlow提醒我的可能性使用堆栈在.NET中进行递归遍历更有效:[ ^ ]。
请务必查看mrydengren的回复:[ ^ ]和Eric Lippert的回复:[ ^ ]。
这是一个解决方案:
check check [ ^ ]
sir, i have solution of question
解决方案
Try like this..
private void Form1_Load(object sender, EventArgs e) { List<Control> allControls = GetAllControls(this); allControls.ForEach(k=>k.Font = new System.Drawing.Font ("Verdana",12)); } private List<Control> GetAllControls(Control container, List<Control> list) { foreach (Control c in container.Controls) { if (c.Controls.Count > 0) list = GetAllControls(c, list); else list.Add(c); } return list; } private List<Control> GetAllControls(Control container) { return GetAllControls(container, new List<Control>()); }
For an alternative to the usual, let's grab all the Controls using a Stack ... not doing recursion in the usual manner:using System.Collections.Generic; using System.Linq; using System.Windows.Forms; public static class SpecialMethods { public static IEnumerable<Control> GetAllControls(Control aControl) { Stack<Control> stack = new Stack<Control>(); stack.Push(aControl); while (stack.Any()) { var nextControl = stack.Pop(); foreach (Control childControl in nextControl.Controls) { stack.Push(childControl); } yield return nextControl; } } }If we wanted to set a specific Font for all Controls on a Form, we could use something like this in the Load Event of a Form:
Font theFont = new Font("Arial", 9.0F, FontStyle.Bold); foreach(Control theControl in (SpecialMethods.GetAllControls(this))) { theControl.Font = theFont; }If we wanted to set only the Font of Button Controls:
foreach(Control theControl in (SpecialMethods.GetAllControls(this)).OfType<Button>().ToList()) { theControl.Font = theFont; }Make sure the Form references Linq.
Sources: this post, in 2009, on StackOverFlow alerted me to the possibilities of using a Stack to make recursive traverses in .NET more efficient: [^].
Be sure and see mrydengren's response: [^], and Eric Lippert's response: [^].
Here is one solution:
check here[^]
这篇关于如何使用c#更改窗口窗体上所有控件的字体系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文