Click here to Skip to main content
15,893,904 members

Comments by mar_morsh (Top 1 by date)

mar_morsh 4-Feb-13 11:44am View    
//this my code for get controls from xml file it's work good ,now I want save //controls Event (ex. button has event click) to xml file and get them to new //controls.
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;

namespace WindowsFormsApplication_DynamicGUIFromXML
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

XDocument guiConfig = XDocument.Load(@"../../Gui.xml");

foreach (XElement item in from y in guiConfig.Descendants("Item") select y)
{
Control tmp = new Control();
switch (item.Attribute("type").Value)
{
case "Button":
tmp = new Button();
break;
case "TextBox":
tmp = new TextBox();
break;
}

tmp.Name = item.Attribute("name").Value;
tmp.Text = item.Attribute("text").Value;
tmp.Location = new Point(Int32.Parse(item.Attribute("x").Value), Int32.Parse(item.Attribute("y").Value));
Controls.Add(tmp);
}

}
}
}

// ***********************************************
// Contents of Gui.xml
// ***********************************************
//
//<gui>
// <item type="Button" name="foo" text="bar" x="100" y="100">
// <item type="TextBox" name="foo2" text="bar2" x="200" y="200">
//