Windows Forms Layout using Xml
-
Hi All, How can I control the layout of Windows forms using Xml file. Thank you, Devaang
I'am guessing you don't wanna use WPF. Use the classes in the System.xml namespace to parse the xml, and then dynamicly create and add controls to the form, setting the position, size, color, text and so on according to the data in the xml. (I am asuming you are using C#/.net). If you use c++/MFC you can do it with parsing the xml with a library like TinyXML and then create the controls dynamicly based on the data in the xml.
-
I'am guessing you don't wanna use WPF. Use the classes in the System.xml namespace to parse the xml, and then dynamicly create and add controls to the form, setting the position, size, color, text and so on according to the data in the xml. (I am asuming you are using C#/.net). If you use c++/MFC you can do it with parsing the xml with a library like TinyXML and then create the controls dynamicly based on the data in the xml.
-
Hi, yes I am using .Net/C# I need something like this. Suppose I have Xml like ; Now When I render "Windows Form" it has lable like in given xml. Thank you, Devaang
well, lets asume you have created a class that contain the data parsed from the xml (lets call it LabelData) and that you have a list with objects of that type containing the data for all the lables you wants to add to the form (lets call the list LabelDataList). the code will look something like this.
public class LabelData
{
public string Name;
public string Text;
public int x,y;
public int width,height;
....
// you should use get/set methodes insted of public variables
};List LabelDataList;
LabelDataList=new List();
//parse the xml
....
....
if (Node.NodeType == XmlNodeType.Element)
{
if (Node.Name == "Label")
{
XmlElement NodeElement = Node as XmlElement;
LabelData tmpLD=new LabelData();
//sett the variables in the tmpLD object to the values in the xml using the XmlElement methode GetAttribute
LabelDataList.add(tmpLD);
}
}
....
....//loop through the parsed data
foreach(LabelData ld in LabelDataList)
{
Label newLabel=new Label();
newLabel.Name=ld.Name;
newLabel.Text=ld.Text;
newLabel.Location=new Point(ld.x, ld.y);
newLabel.Size=new Size(ld.width, ld.height);
....
....
this.Controls.Add(newLabel);
}ofcourse you can create the labels and add them to the form directly in the loop you use to parse the xml too, when it comes to parsing the XML there is many ways to do it, maybe the easiest one is to load the data into a DataTable and loop through the rows in the DataTable.
modified on Tuesday, August 11, 2009 4:11 AM
-
well, lets asume you have created a class that contain the data parsed from the xml (lets call it LabelData) and that you have a list with objects of that type containing the data for all the lables you wants to add to the form (lets call the list LabelDataList). the code will look something like this.
public class LabelData
{
public string Name;
public string Text;
public int x,y;
public int width,height;
....
// you should use get/set methodes insted of public variables
};List LabelDataList;
LabelDataList=new List();
//parse the xml
....
....
if (Node.NodeType == XmlNodeType.Element)
{
if (Node.Name == "Label")
{
XmlElement NodeElement = Node as XmlElement;
LabelData tmpLD=new LabelData();
//sett the variables in the tmpLD object to the values in the xml using the XmlElement methode GetAttribute
LabelDataList.add(tmpLD);
}
}
....
....//loop through the parsed data
foreach(LabelData ld in LabelDataList)
{
Label newLabel=new Label();
newLabel.Name=ld.Name;
newLabel.Text=ld.Text;
newLabel.Location=new Point(ld.x, ld.y);
newLabel.Size=new Size(ld.width, ld.height);
....
....
this.Controls.Add(newLabel);
}ofcourse you can create the labels and add them to the form directly in the loop you use to parse the xml too, when it comes to parsing the XML there is many ways to do it, maybe the easiest one is to load the data into a DataTable and loop through the rows in the DataTable.
modified on Tuesday, August 11, 2009 4:11 AM
Hi Annathor Thanks for your interest... As I have to generate class from xml right for it Fist I have to create schema from xml and then have to generate class from it. Can I use xml directly?? And second question is : I want to bind control from xml file. means suppose my xml like So it means "address" columns of database will be bind to textbox
-
Hi Annathor Thanks for your interest... As I have to generate class from xml right for it Fist I have to create schema from xml and then have to generate class from it. Can I use xml directly?? And second question is : I want to bind control from xml file. means suppose my xml like So it means "address" columns of database will be bind to textbox
English isn't my first language, so I don't understand your question. Do you want to store the column name in a text box? if so you could make a control that inherit from the textbox class like this: public class ExtendedTextbox:Textbox { public string ColumnName; } and use the ExtendedTextbox insted of the normal Textbox class. and save the column name in the ColumnName varible like this: ExtendedTextbox etb=new ExtendedTextbox (); etb.Name="Something"; etb.ColumnName="Something"; ... ... this.Controls.add(etb);