C# Winform获取常规控件&容器控件&特殊容器控件 及其子容器属性代码
```
using System.Windows.Forms;
namespace DianZan.sOptions
{
class SaveOptions
{
public SaveOptions(Form wform)
{
Forms = wform;
}
private Form Forms { get; set; }
public string ASaveOption(string filePath, FileOptType fileOptType)
{
string rStr = null;
OptionData.LangList.Clear();
OptionData.CtrList.Clear();
GetAllControls(Forms); //调用查找控件方法
rStr = SaveFile(filePath, fileOptType);//保存配置文件
OptionData.LangList.Clear();
OptionData.CtrList.Clear();
if (rStr != null)
{
return rStr; //回显提示信息
}
return "UnKnowError";
}
private void GetAllControls(Control FormControl)
{
foreach (Control OneControl in FormControl.Controls)
{
#if DEBUG
int listNum = FormControl.Controls.Count;
string contName = OneControl.Name;
string contText = OneControl.Text;
int countNum = OneControl.Controls.Count;
#endif
string typeName = OneControl.GetType().Name; //获取控件类型
switch (typeName)
{
case "StatusStrip":
GetUnpackStatusStrip(OneControl); //针对StatusStrip特殊控件
break;
default:
GetUnpackStandControl(OneControl);
break;
}
}
}
private void GetUnpackCollectControl(Control TwoControl)
{
int countNum = TwoControl.Controls.Count;
if (countNum > 0)
{
foreach (Control ThreeControl in TwoControl.Controls)
{
GetUnpackStandControl(ThreeControl);
}
}
}
private void GetUnpackStandControl(Control OneControl)
{
string typeName = OneControl.GetType().Name; //获取控件类型
#if DEBUG
string contName = OneControl.Name;
string contText = OneControl.Text;
#endif
switch(typeName)
{
case "Button":
AddOptionInfo.colButton((Button)OneControl);
break;
case "CheckBox":
AddOptionInfo.colCheckBoxx((CheckBox)OneControl);
break;
case "ComboBox":
AddOptionInfo.colComboBox((ComboBox)OneControl);
break;
case "Label":
AddOptionInfo.colLabel((Label)OneControl);
break;
case "TextBox":
AddOptionInfo.colTextBox((TextBox)OneControl);
break;
case "GroupBoxExt":
AddOptionInfo.colGroupBoxExt((GroupBoxExt)OneControl);
GetUnpackCollectControl(OneControl);
break;
}
}
private void GetUnpackStatusStrip(Control control)
{
#if DEBUG
string typeName = control.GetType().Name; //获取控件类型
string contName = control.Name;
string contText = control.Text;
#endif
StatusStrip statusStrip = (StatusStrip)control; //强转类型,目的是获取数据
AddOptionInfo.colStatusStrip(statusStrip); //获取必要数据
ToolStripItemCollection toolStripItemCollection = statusStrip.Items; //强转类型,目的是获取数据
int countNum = toolStripItemCollection.Count; //判断是否需要拆包(方便调试)
if (countNum > 0)
{
foreach (ToolStripItem tsi in toolStripItemCollection)
{
GetUnpackToolStripItem(tsi); //调用拆包方法
}
}
}
private void GetUnpackToolStripItem(ToolStripItem tst)
{
string typeName = tst.GetType().Name; //获取控件类型
switch(typeName)
{
case "ToolStripStatusLabel":
AddOptionInfo.colToolStripStatusLabel((ToolStripStatusLabel)tst);
return; //1级 无需拆包,文本框
case "ToolStripProgressBar":
AddOptionInfo.colToolStripProgressBar((ToolStripProgressBar)tst);
return; //1级 无需拆包,进度条
case "ToolStripSplitButton":
GetUnpackToolStripSplitButton(tst);
return; //1级 拆包,图片+文本菜单按钮 包内容ToolStripDropDownButton
case "ToolStripDropDownButton":
GetUnpackToolStripDropDownButton(tst);
return; //1级-多级 需要递归查找 拆包,单文本菜单按钮 包内容ToolStripDropDownButton
}
}
private void GetUnpackToolStripDropDownButton(ToolStripItem dropDownButton)
{
#if DEBUG
string typeName = dropDownButton.GetType().Name; //获取控件类型
string contName = dropDownButton.Name;
string contText = dropDownButton.Text;
#endif
ToolStripDropDownButton toolStripDropDownButton = (ToolStripDropDownButton)dropDownButton; //强转类型,目的是获取数据
AddOptionInfo.colToolStripDropDownButton(toolStripDropDownButton); //获取必要数据
ToolStripItemCollection toolStripItemCollection = toolStripDropDownButton.DropDownItems; //获取集合
int countNum = toolStripItemCollection.Count; //判断是否需要拆包(方便调试)
if (countNum > 0)
{
foreach (ToolStripItem toolStripItem in toolStripItemCollection)
{
GetUnpackToolStripMenuItem(toolStripItem); //调用拆包方法
}
}
}
private void GetUnpackToolStripMenuItem(ToolStripItem menuItem)
{
#if DEBUG
string typeName = menuItem.GetType().Name; //获取控件类型
string contName = menuItem.Name;
string contText = menuItem.Text;
#endif
ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem)menuItem; //强转类型,目的是获取数据
AddOptionInfo.colToolStripMenuItem(toolStripMenuItem); //获取必要数据
ToolStripItemCollection toolStripItemCollection = toolStripMenuItem.DropDownItems; //获取集合
int countNum = toolStripItemCollection.Count; //判断是否需要拆包(方便调试)
if (countNum > 0)
{
foreach (ToolStripItem toolStripItem in toolStripItemCollection)
{
GetUnpackToolStripMenuItem(toolStripItem); //调用拆包方法
}
}
}
private void GetUnpackToolStripSplitButton(ToolStripItem splitButton)
{
#if DEBUG
string typeName = splitButton.GetType().Name; //获取控件类型
string contName = splitButton.Name;
string contText = splitButton.Text;
#endif
ToolStripSplitButton toolStripSplitButton = (ToolStripSplitButton)splitButton; //强转类型,目的是获取数据
AddOptionInfo.colToolStripSplitButton(toolStripSplitButton); //获取必要数据
ToolStripItemCollection toolStripItemCollection = toolStripSplitButton.DropDownItems; //获取集合
int countNum = toolStripItemCollection.Count; //判断是否需要拆包(方便调试)
if(countNum>0)
{
foreach (ToolStripItem toolStripItem in toolStripItemCollection)
{
GetUnpackToolStripMenuItem(toolStripItem); //调用拆包方法
}
}
}
private string SaveFile(string filePath, FileOptType fileOptType)
{
string rStr = null;
switch (fileOptType)
{
case FileOptType.WriteDefault:
rStr = OptionFile.aOptFile(fileOptType);
break;
case FileOptType.WriteLang:
rStr = OptionFile.aOptFile(fileOptType);
break;
case FileOptType.WriteOption:
rStr = OptionFile.aOptFile(filePath, fileOptType);
break;
}
return rStr;
}
}
}
```
```
using System.Windows.Forms;
namespace DianZan.sOptions
{
class AddOptionInfo
{
public static void colButton(Button cl)
{
object cTag = cl.Tag;
string cName = cl.Name;
string cText = cl.Text;
object cValue = cl.Text;
AddData(cTag, cName, cText, cValue);
}
public static void colCheckBoxx(CheckBox cl)
{
object cTag = cl.Tag;
string cName = cl.Name;
string cText = cl.Text;
object cValue = cl.Checked;
AddData(cTag, cName, cText, cValue);
}
public static void colComboBox(ComboBox cl)
{
object cTag = cl.Tag;
string cName = cl.Name;
string cText = cl.Text;
object cValue = cl.Text;
AddData(cTag, cName, cText, cValue);
}
public static void colGroupBoxExt(GroupBoxExt cl)
{
object cTag = cl.Tag;
string cName = cl.Name;
string cText = cl.Text;
object cValue = cl.Text;
AddData(cTag, cName, cText, cValue);
}
public static void colLabel(Label cl)
{
object cTag = cl.Tag;
string cName = cl.Name;
string cText = cl.Text;
object cValue = cl.Text;
AddData(cTag, cName, cText, cValue);
}
public static void colStatusStrip(StatusStrip cl)
{
object cTag = cl.Tag;
string cName = cl.Name;
string cText = cl.Text;
object cValue = cl.Text;
AddData(cTag, cName, cText, cValue);
}
public static void colTextBox(TextBox cl)
{
object cTag = cl.Tag;
string cName = cl.Name;
string cText = cl.Text;
object cValue = cl.Text;
AddData(cTag, cName, cText, cValue);
}
public static void colToolStripItem(ToolStripItem cl)
{
object cTag = cl.Tag;
string cName = cl.Name;
string cText = cl.Text;
object cValue = cl.Text;
AddData(cTag, cName, cText, cValue);
}
public static void colToolStripDropDownButton(ToolStripDropDownButton cl)
{
object cTag = cl.Tag;
string cName = cl.Name;
string cText = cl.Text;
object cValue = cl.Text;
AddData(cTag, cName, cText, cValue);
}
public static void colToolStripMenuItem(ToolStripMenuItem cl)
{
object cTag = cl.Tag;
string cName = cl.Name;
string cText = cl.Text;
object cValue = cl.Text;
AddData(cTag, cName, cText, cValue);
}
public static void colToolStripStatusLabel(ToolStripStatusLabel cl)
{
object cTag = cl.Tag;
string cName = cl.Name;
string cText = cl.Text;
object cValue = cl.Text;
AddData(cTag, cName, cText, cValue);
}
public static void colToolStripProgressBar(ToolStripProgressBar cl)
{
object cTag = cl.Tag;
string cName = cl.Name;
string cText = cl.Text;
object cValue = cl.Text;
AddData(cTag, cName, cText, cValue);
}
public static void colToolStripSplitButton(ToolStripSplitButton cl)
{
object cTag = cl.Tag;
string cName = cl.Name;
string cText = cl.Text;
object cValue = cl.Text;
AddData(cTag, cName, cText, cValue);
}
private static void AddData(object cTag,string cName,string cText,object cValue)
{
//Type typec = con.GetType();
bool LangSave = false;
bool OptSave = false;
if (cTag != null && cTag.Equals("Lang"))
{
LangSave = true; //是否包含保存配置参数标志
}
if (cTag != null && cTag.Equals("Opt"))
{
OptSave = true; //是否包含保存配置参数标志
}
if (cTag != null && cTag.Equals("LangOpt"))
{
LangSave = true;
OptSave = true;
}
LangOption langOption = new LangOption();
CtrOption ctrOption = new CtrOption();
if (LangSave)
{
langOption.Name = cName;
langOption.Text = cText;
OptionData.LangList.Add(langOption); //添加语言文件
}
if (OptSave)
{
ctrOption.Name = cName;
ctrOption.Value = cValue;
OptionData.CtrList.Add(ctrOption); //添加语言文件
}
}
}
}
```
虽然方法比较笨,但是最终还是搞定了 fih 发表于 2023-10-1 20:37
可以的,通用的Winform遍历窗体控件,加载和保存配置文件。
这是保存,加载的我还没搞定呢
保存相对简单,只要根据调试信息里面的数据内容写对应代码就行了.
保存用3天,我估计加载的代码要300天,太难了{:1_923:} 虽然看不懂,但是依旧觉得很腻害。 可以的,通用的Winform遍历窗体控件,加载和保存配置文件。 这是啥意思,没怎么看懂,可以详细说一下嘛 厉害,先mark C#比C++简单,using自动就可以引用了
页:
[1]