C#遍历界面控件名称和值,本来是想遍历所有控件名称用来切换语言用的,后来感觉也可以整体保存界面参数,于是就写到xml里面导入导出算了。
主要代码类:
public class XmlHelp
{
Dictionary<string, string> control = new Dictionary<string, string>();//界面控件键值对(控件名,值)
Dictionary<string, string> xmlInfro = new Dictionary<string, string>();//导出xml文件的键值对集合(控件名,值)
List<Control> list = new List<Control>();//界面控件集合(包含在其他布局空间内的控件)
/// <summary>
/// 获取界面内所有控件,包括布局内的控件
/// </summary>
/// <param name="objControl"></param>
/// <param name="list"></param>
private void InitialControl(Control objControl, List<Control> list)
{
foreach (System.Windows.Forms.Control contro in objControl.Controls)
{
list.Add(contro);
if (contro.Controls.Count > 0)
{
InitialControl(contro, list);
}
}
}
/// <summary>
/// 遍历所有控件的键值对(目前适用:CheckBox、RadioButton、ComboBox、Button及textbox系列)
/// </summary>
/// <param name="control">控件界面(如:主画面this)</param>
/// <param name="controlList">键值对集合</param>
private void getControls(List<Control> control, Dictionary<string, string> controlList)
{
foreach (Control singlecontrol in control)
{
if (!controlList.ContainsKey(singlecontrol.Name))//判断有没有重复的键值对
{
if (singlecontrol is CheckBox)
{
CheckBox cb = (CheckBox)singlecontrol;
controlList.Add(singlecontrol.Name, cb.Checked.ToString());
}
else if (singlecontrol is RadioButton)
{
RadioButton ra = (RadioButton)singlecontrol;
controlList.Add(singlecontrol.Name, ra.Checked.ToString());
}
else if (singlecontrol is ComboBox)
{
ComboBox cb = (ComboBox)singlecontrol;
if (cb.SelectedItem == null)
{
controlList.Add(singlecontrol.Name, "");
}
else
{
controlList.Add(singlecontrol.Name, cb.SelectedItem.ToString());
}
}
else if (singlecontrol is Button)
{
Button bt = (Button)singlecontrol;
controlList.Add(singlecontrol.Name, bt.Enabled.ToString());
}
else
{
controlList.Add(singlecontrol.Name, singlecontrol.Text);
}
if (singlecontrol.Controls.Count > 0)
{
getControls(control, controlList);
}
}
}
}
public void saveXml(List<Control> contro, string rootName, string path)
{
XmlDocument xml = new XmlDocument();
XmlDeclaration xmldec = xml.CreateXmlDeclaration("1.0", "utf-8", "yes");
xml.AppendChild(xmldec);
XmlElement rootElement = xml.CreateElement(rootName);
xml.AppendChild(rootElement);
XmlAttribute root = xml.CreateAttribute(rootName);
root.Value = "1.0.1";
rootElement.Attributes.Append(root);
for (int i = 0; i < contro.Count; i++)
{
if (contro[i]!=null)
{
exportXml(contro[i], xml, rootElement);
}
}
xml.Save(path);
}
/// <summary>
/// 导出xml文件,并保存
/// </summary>
/// <param name="xml">xml文件对象</param>
/// <param name="path">文件存储路径</param>
///<param name="rootName">根节点名称</param>
public void exportXml(Control contro, XmlDocument xml, XmlElement rootElement)
{
InitialControl(contro, list);
getControls(list, control);
list.Clear();
XmlElement xmlClassElement = xml.CreateElement(contro.Name);
rootElement.AppendChild(xmlClassElement);
XmlAttribute attr2 = xml.CreateAttribute("name");
attr2.Value = contro.Name;
xmlClassElement.Attributes.Append(attr2);
for (int i = 0; i < control.Count; i++)
{
XmlElement a = xml.CreateElement(control.Keys.ElementAt(i));
xmlClassElement.AppendChild(a);
XmlAttribute va = xml.CreateAttribute("name");
va.Value = control.Keys.ElementAt(i);
a.Attributes.Append(va);
XmlAttribute val = xml.CreateAttribute("value");
val.Value = control.Values.ElementAt(i);
a.Attributes.Append(val);
}
control.Clear();
}
/// <summary>
/// 从xml文件加载指定界面控件的键值对
/// </summary>
/// <param name="xml">xml文件对象</param>
/// <param name="form"></param>
private void loadXml(XmlDocument xml, string form)
{
XmlNode xmlnode = xml.SelectSingleNode(form);
if (form == null)
{
return;
}
XmlNodeList xmlnodelist = xmlnode.ChildNodes;
if (xmlnodelist.Count == 0)
{
return;
}
foreach (XmlNode item in xmlnodelist)
{
string a = item.Attributes["name"].Value;
string b = item.Attributes["value"].Value;
xmlInfro.Add(a, b);
}
}
/// <summary>
/// 将界面控件的Name和值存入键值对集合(目前是textbox、label、checkbox、RadioButton可以使用)
/// </summary>
/// <param name="control">输入控件所在界面</param>
/// <param name="dic">键值对集合</param>
private void switchParams(Control control, Dictionary<string, string> dic)
{
if (dic.Count <= 0)
{
return;
}
foreach (Control item in control.Controls)
{
for (int i = 0; i < dic.Count; i++)
{
if (dic.Keys.ElementAt(i) == item.Name)
{
if (item is CheckBox)
{
CheckBox cb = (CheckBox)item;
cb.Checked = Convert.ToBoolean(dic.Values.ElementAt(i)) || false;
}
else if (item is RadioButton)
{
RadioButton ra = (RadioButton)item;
ra.Checked = Convert.ToBoolean(dic.Values.ElementAt(i)) || false;
}
else if (item is ComboBox)
{
ComboBox cb = (ComboBox)item;
cb.SelectedItem = dic.Values.ElementAt(i);
}
else if (item is Button)
{
Button bt = (Button)item;
bt.Enabled = Convert.ToBoolean(dic.Values.ElementAt(i)) || false;
}
else
{
item.Text = dic.Values.ElementAt(i);
}
}
}
if (item.Controls.Count > 0)
{
switchParams(item, dic);
}
}
}
/// <summary>
/// 从xml文件加载控件值,目前可以用在textbox、label、checkbox类型的控件
/// </summary>
/// <param name="control">控件界面(如:this)</param>
/// <param name="rootName">根节点名称</param>
/// <param name="path">xml文件路径</param>
public void loadParamsFromXml(Control control, string rootName, string path)
{
XmlDocument xml = new XmlDocument();
try
{
xml.Load(path);
loadXml(xml, rootName);
switchParams(control, xmlInfro);
xmlInfro.Clear();
}
catch (Exception)
{
throw;
}
}
}
实现效果:可以加载textbox、label、CheckBox、RadioButton及 ComboBox控件的名称、值,如需要加载其他控件的值可自行添加。
实现效果: