C# 儲存所有TextBox,下次全部裝入儲存介面的輸入框內容

來源:互聯網
上載者:User
  1.   private void SaveXml(XmlTextWriter tw, Control objs) //儲存by yanleigis email:landgis@126.com
  2.         {
  3.             if (objs.Controls.Count < 2) return;
  4.             foreach (object obj in objs.Controls)
  5.             {
  6.                 TextBox myextBox;
  7.                 if (obj is TextBox)
  8.                 {
  9.                     myextBox = (TextBox)obj;
  10.                     tw.WriteElementString(myextBox.Name, myextBox.Text);
  11.                 }
  12.                 else
  13.                 {
  14.                     if (obj is Control) SaveXml(tw, (Control)obj);
  15.                 }
  16.             }
  17.         }
  18.         private void SaveXml(Form myForm) //儲存by yanleigis email:landgis@126.com
  19.         {
  20.             string FileName = Application.ExecutablePath;
  21.             string xmlFileName = Path.ChangeExtension(FileName, ".xml");
  22.             if (File.Exists(xmlFileName))
  23.                 File.Delete(xmlFileName);
  24.             XmlTextWriter tw = new XmlTextWriter(xmlFileName, null);
  25.             tw.Formatting = Formatting.Indented;
  26.             tw.WriteStartDocument();
  27.             tw.WriteStartElement("savetextconent");
  28.             tw.WriteAttributeString("Text", "SaveText");
  29.             foreach (object obj in myForm.Controls)
  30.             {
  31.                 TextBox myextBox;
  32.                 if (obj is TextBox)
  33.                 {
  34.                     myextBox = (TextBox)obj;
  35.                     tw.WriteElementString(myextBox.Name, myextBox.Text);
  36.                 }
  37.                 else
  38.                 {
  39.                     if (obj is Control) SaveXml(tw, (Control)obj);
  40.                 }
  41.             }
  42.             tw.WriteEndElement();
  43.             tw.WriteEndDocument();
  44.             tw.Flush();
  45.             tw.Close();
  46.         }
  47.         private void loadXml(Form myForm) //載入by yanleigis email:landgis@126.com
  48.         {
  49.             string FileName = Application.ExecutablePath;
  50.             string xmlFileName = Path.ChangeExtension(FileName, ".xml");
  51.             if (!File.Exists(xmlFileName)) return;
  52.             XmlDocument xmldoc = new XmlDocument();
  53.             xmldoc.Load(xmlFileName);
  54.             XmlElement root = xmldoc.DocumentElement;
  55.             XmlNodeList xnl = root.ChildNodes;
  56.             for (int i = 0; i < xnl.Count; i++)
  57.             {
  58.                 object textobj = myForm.GetType().GetField(xnl.Item(i).Name,
  59.                 System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(myForm);
  60.                 if (textobj != null)
  61.                 {
  62.                     TextBox textBox = textobj as TextBox;
  63.                     textBox.Text = xnl.Item(i).InnerText;
  64.                 }
  65.             }
  66.         }
  67.        //測試 by yanleigis
  68.         private void frmdel_Load(object sender, EventArgs e)
  69.         {
  70.             loadXml(this);
  71.         }
  72.         private void frmdel_FormClosed(object sender, FormClosedEventArgs e)
  73.         {
  74.             SaveXml(this);
  75.         }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.