C#將部分Controls數(shù)據(jù)導(dǎo)入對(duì)象并存入ini中的操作方法
在日常的Winform設(shè)計(jì)工作中,將控件中的數(shù)據(jù)導(dǎo)出到對(duì)應(yīng)屬性或者字段中,再進(jìn)行保存是經(jīng)常會(huì)用到的技巧;最簡(jiǎn)單的就是同時(shí)遍歷控件和遍歷屬性字段進(jìn)行名稱對(duì)比(需要保證控件的名稱要包含在字段屬性中);
本篇文章主要是在簡(jiǎn)單的基礎(chǔ)上優(yōu)化整體邏輯,不僅僅是只遍歷控件和屬性進(jìn)行名稱對(duì)比(適合類),還包含一些篩選;
1.遍歷控件和屬性得到控件的值
在下面代碼中,控件命名是以textBox_one的形式命名的
///類對(duì)象
class ObjectParm
{
public int one;
public string two;
public int three;
public string four;
public int five;
public string six;
}private void Save(ObjectParm objectParm, Control controls, string TextName = "textBox_", string ComboBoxName = "comboBox_")
{
Type type = objectParm.GetType();
//獲取有關(guān)成員屬性的信息以及提供對(duì)成員數(shù)據(jù)的訪問(wèn)
MemberInfo[] memberInfos = type.GetMembers();//獲取所有公共成員
foreach (Control control in controls.Controls)
{
foreach (MemberInfo item in memberInfos)
{
//這里字段屬性均可以
if (item.MemberType == MemberTypes.Field)
{
if (control is ComboBox)
{
if (control.Name == ComboBoxName + item.Name)
{
string value = control.Text;
//需要篩選對(duì)象屬性的類型
SetMemberValue(objectParm, item.Name, value);
//---------------------------------注意----------------------------------
//SetMemberValue函數(shù)是判斷屬性或者字段的類型,根據(jù)類型進(jìn)行不同的賦值
}
}
else if (control is TextBox)
{
if (control.Name == TextName + item.Name)
{
string value = control.Text;
//需要篩選對(duì)象屬性的類型
SetMemberValue(objectParm,item.Name,value);
}
}
}
}
}
}2.利用FieldInfo的getSet函數(shù)設(shè)置類對(duì)象數(shù)據(jù)
/// <summary>
/// 設(shè)置類對(duì)象成員數(shù)據(jù)
/// </summary>
/// <param name="objectParm"></param>
/// <param name="fileName"></param>
/// <param name="filevalue"></param>
//Istype函數(shù)是對(duì)比類型是否一致
private bool SetMemberValue(ObjectParm objectParm, string fileName, string filevalue)
{
Type type = objectParm.GetType();
//發(fā)現(xiàn)字段屬性并提供訪問(wèn)
FieldInfo fieldInfo = type.GetField(fileName);//搜索字段
bool ConverFlag = true;
//類型匹配
if (Istype(fieldInfo.FieldType, "System.String"))
{
fieldInfo.SetValue(objectParm, filevalue);
}
if (Istype(fieldInfo.FieldType, "System.Double"))
{
//判斷是否可以進(jìn)行轉(zhuǎn)
double result = 0;
if (!double.TryParse(filevalue, out result)) ConverFlag = false;
fieldInfo.SetValue(objectParm, result);
}
if (Istype(fieldInfo.FieldType, "System.Single"))
{
float result = 0;
if (!float.TryParse(fileName, out result))
ConverFlag = false;
fieldInfo.SetValue(objectParm, result);
}
if (Istype(fieldInfo.FieldType, "System.Boolean"))
{
bool flag = false;
if (!Boolean.TryParse(fileName, out flag))
ConverFlag = false;
fieldInfo.SetValue(objectParm, flag);
}
if (Istype(fieldInfo.FieldType, "System.UInt32"))
{
uint value = 0;
if (!uint.TryParse(fileName, out value))
ConverFlag = false;
fieldInfo.SetValue(objectParm, value);
}
if (Istype(fieldInfo.FieldType, "System.UInt16"))
{
UInt16 value = 0;
if (!UInt16.TryParse(fileName, out value))
ConverFlag = false;
fieldInfo.SetValue(objectParm, value);
}
if (Istype(fieldInfo.FieldType, "System.Int32"))
{
int value = 0;
if (!Int32.TryParse(fileName, out value))
ConverFlag = false;
fieldInfo.SetValue(objectParm, value);
}
if (Istype(fieldInfo.FieldType, "System.Decimal"))
{
if (filevalue != "")
fieldInfo.SetValue(objectParm, Decimal.Parse(filevalue));
else
fieldInfo.SetValue(objectParm, new Decimal(0));
}
if (Istype(fieldInfo.FieldType, "System.Nullable`1[System.DateTime]"))
{
if (filevalue != "")
{
try
{
fieldInfo.SetValue(objectParm, (DateTime?)DateTime.ParseExact(filevalue, "yyyy-MM-dd HH:mm:ss", null));
}
catch
{
fieldInfo.SetValue(objectParm, (DateTime?)DateTime.ParseExact(filevalue, "yyyy-MM-dd", null));
}
}
else
fieldInfo.SetValue(objectParm, null);
}
return ConverFlag;
}private bool Istype(Type type, string typeName)
{
if (type.ToString() == typeName)
{ return true; }
if (type.ToString() == "System.Object")
return false;
return Istype(type.BaseType, typeName);
}3.Ini簡(jiǎn)易類庫(kù)編寫(xiě)
class IniClass
{
public static string inipath = Directory.GetCurrentDirectory() + "\\" + "systemset.ini";//這個(gè)地方實(shí)際沒(méi)用到,在另外一個(gè)地方
[System.Runtime.InteropServices.DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[System.Runtime.InteropServices.DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public IniClass(string inipath_)
{
inipath = inipath_;
}
/// ﹤summary﹥ /// 寫(xiě)入INI文件 /// ﹤/summary﹥ /
// ﹤param name="Section"﹥項(xiàng)目名稱(如 [TypeName] )﹤/param﹥
/// ﹤param name="Key"﹥鍵﹤/param﹥
/// ﹤param name="Value"﹥值﹤/param﹥
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, inipath);
}
/// ﹤summary﹥
/// 讀出INI文件
/// ﹤/summary﹥
/// ﹤param name="Section"﹥項(xiàng)目名稱(如 [TypeName] )﹤/param﹥
/// ﹤param name="Key"﹥鍵﹤/param﹥
public string IniReadValue(string Section, string Key, string default_value = "")
{
StringBuilder temp = new StringBuilder(50000);
int i = GetPrivateProfileString(Section, Key, default_value, temp, 50000, inipath);
return temp.ToString();
}4.存入對(duì)象轉(zhuǎn)換為json存入ini
string Path = @"E:\ymx\Test\將部分Controls數(shù)據(jù)導(dǎo)入對(duì)象\sys.ini";
private void button1_Click(object sender, EventArgs e)
{
Save(objectParm,panel1, "textBox_", "comboBox_");
string str = JsonConvert.SerializeObject(objectParm);
//存入ini
IniClass iniClass = new IniClass(Path);
iniClass.IniWriteValue("Parm", "trest", str);
}5.效果展示


到此這篇關(guān)于C#將部分Controls數(shù)據(jù)導(dǎo)入對(duì)象并存入ini中的文章就介紹到這了,更多相關(guān)C#Controls數(shù)據(jù)存入ini中內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#?wpf?Bitmap轉(zhuǎn)換成WriteableBitmap的方法
本文主要介紹了C#?wpf?Bitmap轉(zhuǎn)換成WriteableBitmap的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
C#向線程中傳遞多個(gè)參數(shù)的解決方法(兩種)
這篇文章主要介紹了C#向線程中傳遞多個(gè)參數(shù)的解決方法(兩種)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
C#安裝OpenCvSharp4的實(shí)現(xiàn)步驟
OpenCv是一款開(kāi)源的圖像處理庫(kù),本文就介紹了C#安裝OpenCvSharp4的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2022-05-05

