【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《C#系统编程代码集合》,欢迎阅读!
C#系统编程代码集合
1. 一个界面(Form1)获取另一个界面(Form2)中控件参数(一般指Text)值
private void getnumber_Click(object sender, EventArgs e)
{
Form2 fo2 = new Form2(); string k1 = "tb"; string k2 = "m512";
string k = k1 + k2; //控件名,实现控件名称动态化
object i = fo2.GetType().GetField(k, System.Reflection.BindingFlags.NonPublic | //反向映射System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(fo2); //反向映射 TextBox tb = new TextBox(); tb = i as TextBox; tb1.Text = tb.Text; }
2. 多线程参数传递,使用异步委托
public delegate int threadasy(string parm); //在Form下声明委托
threadasy asy = new threadasy(threadtestpart); //线程,asy线程对象,threadtestpart线程名
IAsyncResult asyresult = asy.BeginInvoke(parm, null, null);//委托开始
int res = asy.EndInvoke(asyresult); //委托结束,int参数传递
3. XML文件创建与添加元素
private void pcbnewbulid_Click(object sender, EventArgs e)
{
string pcbname = pcbstype.SelectedItem.ToString(); XmlDocument xd = new XmlDocument(); //声明XML文件性质
XmlDeclaration xmlDec = xd.CreateXmlDeclaration("1.0", "utf-8", "yes"); xd.AppendChild(xmlDec); //加入一个根元素
XmlElement xmlelem = xd.CreateElement(pcbname); xd.AppendChild(xmlelem);
XmlNode root = xd.SelectSingleNode(pcbname); xd.Save(@"XML//pcbname.xml"); }
//添加元素
XmlElement lsffc = xmldoc.CreateElement("立式FFC排线"); addelement(xmldoc, lsffc, "xspos", xsp);
//添加元素方法,可以减少工作量
private XmlElement addelement(XmlDocument doc, XmlElement ele, string tar, string value) {
XmlElement nodeelem = doc.CreateElement(tar);
XmlText nodetext = doc.CreateTextNode(value); ele.AppendChild(nodeelem); nodeelem.AppendChild(nodetext); return nodeelem; }
本文来源:https://www.wddqxz.cn/46fe2c18905f804d2b160b4e767f5acfa1c7833c.html