/// <summary>
/// 遍历窗体子控件和孙控件,将所有控件置于allCtrls
/// </summary>
private static Queue<Control> allCtrls = new Queue<Control>();
private static void CheckAllCtrls(Control item)
{
for (int i = 0; i < item.Controls.Count; i++)
{
if (item.Controls[i].HasChildren)
{
CheckAllCtrls(item.Controls[i]);
}
else
{
allCtrls.Enqueue(item.Controls[i]);
}
}
}
/*然后一定注意在窗体的构造函数中写!!!*/
public SysSetting()
{
InitializeComponent();
CheckAllCtrls(this);//表示生成子窗体的时候已经遍历窗体的所有控件和子控件
}
/*使用举例:想要找到设置窗体中的文本框并保存其中的数据*/
public void saveSet(string filePath)
{
//string newset = "#系统设置";
StreamWriter sw = new StreamWriter(filePath,false);
sw.Write("");
string newset = "#系统设置";
foreach (Control c in allCtrls)
{
if ((c is TextBox) && (c.Text != null))
{
newset += "\r\n" + c.Name + "=" + c.Text;
//File.AppendText(newset);
}
if (c is CheckBox && (((CheckBox)c).Checked == true))
{
newset += "\r\n" + c.Name + "=" + "1";
// File.AppendText(newset);
}
if (c is CheckBox && (((CheckBox)c).Checked == false))
{
newset += "\r\n" + c.Name + "=" + "0";
//File.AppendText(newset);
}
//File.AppendText(newset);
}
sw.Write(newset);
sw.Flush();
sw.Close();
}