Save WinForm or Controls to File

后端 未结 6 1987
旧巷少年郎
旧巷少年郎 2021-01-24 09:52

I have been working on an application which allows the user to make a label template for printing purposes by adding label controls to a panel(which I use as a container). I ha

6条回答
  •  悲&欢浪女
    2021-01-24 10:31

    This isn't trivial, but personally I would set up a function that can be called recursively that would add nodes to an XML file.

    I don't have actual code, but pseudo-code looks like this: (you will need to do some clean-up, because I'm doing this off the top of my head without the aid of Intellisense.)

    XmlDocument doc;
    
    function SaveForm()
    {
       doc = new XmlDocument("FormInfo");
       foreach(Control ctrl in this.Controls)
       {
          AddControlToXml(ctrl, doc.Documentelement);
       }
    }
    
    function AddControlToXml(Control ctrl, XmlNode currentNode)
    {
       XmlNode n = new XmlNode;
       Node.InnerText = ctrl.Name;
       foreach(Control ctrl2 in ctrl.Controls)
       {
          AddControlToXml(ctrl2);
       }
    
    }
    

提交回复
热议问题