|
Hi All, How can I control the layout of windows forms 2.0 using xml file. I mean suppose I have 5 textboxes or labels on the form I need to control the layout of form using xml file. Thank you, Devaang | | Devaang25 Tuesday, August 11, 2009 6:44 AM | Hi Devaang25,
Write the following code on form load that will load the XML , and through reflection load the Correponding control class, and the property values dynamically.
try
{
//Insert the file in bin folder
XmlDocument doc = new XmlDocument();
doc.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + "\\XMLFileWindow.xml");
XmlNodeList nodes = doc.SelectNodes("/Label");
//Create Dynamic Assembly
Assembly myAssembly = null;
myAssembly = Assembly.GetCallingAssembly();
//Loading
foreach (XmlNode node in nodes)
{
try
{
//Loading Control Class specific to Label
Type tControl = myAssembly.GetType("System.Windows.Forms." + node.Name);
object objControl = Activator.CreateInstance(tControl, null);
foreach (XmlAttribute at in node.Attributes)
{
//Get Property Info
PropertyInfo p = tControl.GetProperty(at.Name);
if (p.Name == "Location")
{
string[] chLocation = at.Value.Split(',');
p.SetValue(objControl, new Point(Convert.ToInt16(chLocation[0].ToString()), Convert.ToInt16(chLocation[1].ToString())), null);
}
else if (p.Name == "Size")
{
string[] chLocation = at.Value.Split(',');
p.SetValue(objControl, new Size(Convert.ToInt16(chLocation[0].ToString()), Convert.ToInt16(chLocation[1].ToString())), null);
}
else if (p.Name == "TextAlign")
p.SetValue(objControl, HorizontalAlignment.Center, null);
else
p.SetValue(objControl, at.Value, null);
}
this.Controls.Add((Control)objControl);
}
catch (Exception exx)
{
Console.WriteLine(exx.Message);
return;
}
Good luck if it is helpfull. - Marked As Answer byKira QianMSFT, ModeratorMonday, August 17, 2009 8:36 AM
-
| | Malik M.Shahid Tuesday, August 11, 2009 10:53 AM |
private void Form1_Load(object sender, EventArgs e)
{
SetAttributes();
}
private void SetAttributes()
{
XmlDocument doc = new XmlDocument();
doc.Load("Test.xml");
//Display all the book titles.
XmlNodeList elemList = doc.GetElementsByTagName("Label");
for (int i = 0; i < elemList.Count; i++)
{
label1.Name = elemList[i].Attributes["Name"].Value.ToString();
label1.Text = elemList[i].Attributes["Text"].Value.ToString();
}
}
Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
- Marked As Answer byKira QianMSFT, ModeratorMonday, August 17, 2009 8:36 AM
- Proposed As Answer by_SuDhiR_ Tuesday, August 11, 2009 10:01 AM
-
| | _SuDhiR_ Tuesday, August 11, 2009 8:56 AM | Read XML file in Form_load for Layout details and Set them for controls. Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
| | _SuDhiR_ Tuesday, August 11, 2009 6:48 AM | Hi, Suppose I have xml like <Root> <Label Name="balance_label" Text="DETAILS" Location="325,177" Size="360,20" TextAlign="MiddleCenter" /> </Root> Now I want to generate form output from this xml means form has lable as per the xml file.
| | Devaang25 Tuesday, August 11, 2009 7:22 AM |
private void Form1_Load(object sender, EventArgs e)
{
SetAttributes();
}
private void SetAttributes()
{
XmlDocument doc = new XmlDocument();
doc.Load("Test.xml");
//Display all the book titles.
XmlNodeList elemList = doc.GetElementsByTagName("Label");
for (int i = 0; i < elemList.Count; i++)
{
label1.Name = elemList[i].Attributes["Name"].Value.ToString();
label1.Text = elemList[i].Attributes["Text"].Value.ToString();
}
}
Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
- Marked As Answer byKira QianMSFT, ModeratorMonday, August 17, 2009 8:36 AM
- Proposed As Answer by_SuDhiR_ Tuesday, August 11, 2009 10:01 AM
-
| | _SuDhiR_ Tuesday, August 11, 2009 8:56 AM | Hi Devaang25,
Write the following code on form load that will load the XML , and through reflection load the Correponding control class, and the property values dynamically.
try
{
//Insert the file in bin folder
XmlDocument doc = new XmlDocument();
doc.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + "\\XMLFileWindow.xml");
XmlNodeList nodes = doc.SelectNodes("/Label");
//Create Dynamic Assembly
Assembly myAssembly = null;
myAssembly = Assembly.GetCallingAssembly();
//Loading
foreach (XmlNode node in nodes)
{
try
{
//Loading Control Class specific to Label
Type tControl = myAssembly.GetType("System.Windows.Forms." + node.Name);
object objControl = Activator.CreateInstance(tControl, null);
foreach (XmlAttribute at in node.Attributes)
{
//Get Property Info
PropertyInfo p = tControl.GetProperty(at.Name);
if (p.Name == "Location")
{
string[] chLocation = at.Value.Split(',');
p.SetValue(objControl, new Point(Convert.ToInt16(chLocation[0].ToString()), Convert.ToInt16(chLocation[1].ToString())), null);
}
else if (p.Name == "Size")
{
string[] chLocation = at.Value.Split(',');
p.SetValue(objControl, new Size(Convert.ToInt16(chLocation[0].ToString()), Convert.ToInt16(chLocation[1].ToString())), null);
}
else if (p.Name == "TextAlign")
p.SetValue(objControl, HorizontalAlignment.Center, null);
else
p.SetValue(objControl, at.Value, null);
}
this.Controls.Add((Control)objControl);
}
catch (Exception exx)
{
Console.WriteLine(exx.Message);
return;
}
Good luck if it is helpfull. - Marked As Answer byKira QianMSFT, ModeratorMonday, August 17, 2009 8:36 AM
-
| | Malik M.Shahid Tuesday, August 11, 2009 10:53 AM |
|