Runtime control population using XML in winform application - c#

A thought came in mind, is there any way to populate control in runtime using XML. I mean I will have one Controls.xml file and in that file I can define what control i want to add to a perticular winform and when we run that app it will show exact controls. Has anyone done this? Please guide me on this.

Yes, you can create the controls dynamically: all you need to do is to parse the XML and then create them.
TextBox tb = new TextBox();
tb.Location = new Point(25,25);
tb.Click += textbox_Click;
this.Controls.Add (tb);

You can Serialize a Control (which is an Object) to XML than Deserialize se HOW TO
But the issue would be Event's ,where you should use Reflection and/or Code Injection into the Application because Event's cannot get Deserialized as an Object.

Related

TabPage custom Tag object lifecycle

I am assigning a custom class with some information I need for each TabPage created:
MyClass custom = new MyClass();
TabPage tabPage = new TabPage();
tabPage.Text = ...;
tabPage.Tag = custom;
tabPage.Controls.Add(...);
Unfortunately, the moment I click between tabs, or have a context menu and click something, the Tag object is lost.
From my understanding, you can set and use whatever you want inside Tag objects, so what's the issue?
I need this to be persistent until I want it to.
The issue still persists without explanation. I even tried this post, with the same result.
Apparently, I was using a third party library that has setting "HotTrack" information as boolean (true/false) using the Tag property of the TabPages.
Removing this in the library (as I do not need it) allowed me to store my own information without getting lost or overriden.

Windows form code pass to WPF

i am trying to make a Column header in WPF by using code like this
listview.Columns.Add("Name",100);
listview.View = View.Details;
listview.FullrowSelect = true;
listview.GridLines = True;
and the error is always saying that i need System.windows.Controls.Listview but when i added the class using System.Windows.Controls.Listview; it's the same error
Please have a look here, I think this is something that you are trying to achieve.
Adding Columns programatically to listview in WPF?
Please notice that WPF use Presentation DLL to construct UI while normal windows from use System.Windows.Forms. As a result, you can't use the Win Form' s control in WPF, you can use gridview instead, please refer to Moeen' s link

How to add link Labels at run time in Windows form

I have been making a Windows Form Application in C# using the Visual C# 2008 IDE.
There are basically two forms in my application. One is created at Runtime and it's layout is undefined and the second one's predefined.
Now, I have been adding form elements using the toolbox provided and don't have any idea how to add them using written code(not using toolbox). I want to add n number of Labels to the second form which is undefined. n can be anything(decided at runtime, depending on the user's input). Can anybody tell me what is the efficient way to do this?
Just a quick example of a "dynamic control" being created at run-time and added to the form:
Label lbl = new Label();
lbl.Text = "Hello World!";
lbl.Location = new Point(100, 25);
this.Controls.Add(lbl);
You can replace "this" with the container to add it to, like "panel1" for instance. For containers that have their own layout engine, like a FlowLayoutPanel, then you wouldn't need to specify a Location().
Create a new LinkLabel(), set its properties (in particular, text and position), then add it to the Controls collection of your form or any panel.
You may also want to add event handlers, and store them somewhere (probably in a List<T>) so you can change or remove them later.
Create one in the designer, configure it's properties as you wish. Then go to the designer file, which name is like Form1.Desiner.cs, copy the code related to your LinkLabel (find everything with text search) and paste it where you wish :)

Winforms/C#: Store/Retrieve (Persist) Content of TextBoxes between 2 Runs

Is there an easiest way to simply tell Winforms (mainly TextBoxes) to persist all its content e. g. to a file without me having to loop through all the controls?
I saw this in a WPF application, but am clueless (also, Google did not turn up anything) whether there is an out-of-the-box, easiest approach.
Again, looping through each control would be possible but seems.. too much work?
Would serialization maybe work?
You can set your ApplicationSettings PropertyBinding in the Form Designer, assign it a Field Name then when you close your application you can do this which will save all of you changes to all of the bound textbox's in the applications app.config file.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Save();
}
You can use IsolatedStorageFile to save and load data. If you can bind your textboxes to a class with properties for each textbox, you can serialize the class and store it IsolatedStorageFile.
You can then deserialize from IsolatedFileStorage and the controls should be bound to the values present in the properties of the class.
For details about data-binding to a class refer to answeers to this question: Data Binding to an object in C#
Refer to this for further details about saving and loading to/from IsolatedStorageFile

How to generate WPF controls automatically based on XML file?

I have an Xml file which tells me the controls that I have to add to a form but this Xml changes dynamically and I need to update the form.
Currently, I can read the XML file, but I dont know if it is possible to automatically create forms based on that or not ?
Yes It is possible.
WPF offers several ways of creating controls either in Xaml or in code.
For your case if you need to dynamically create your controls, you'll have to create them in code. You can either create your control directly using their constructors as in:
// Create a button.
Button myButton= new Button();
// Set properties.
myButton.Content = "Click Me!";
// Add created button to a previously created container.
myStackPanel.Children.Add(myButton);
Or you could create your controls as a string containing xaml and use a XamlReader to parse the string and create the desired control:
// Create a stringBuilder
StringBuilder sb = new StringBuilder();
// use xaml to declare a button as string containing xaml
sb.Append(#"<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
sb.Append(#"Content='Click Me!' />");
// Create a button using a XamlReader
Button myButton = (Button)XamlReader.Parse(sb.ToString());
// Add created button to previously created container.
stackPanel.Children.Add(myButton);
Now for which one of the two methods you want to use really depends on you.
Jean-Louis
You can easily add controls via code in wpf, you can follow this article. Another thing worth noting is that XAML is a form of XML so you can you save your XAML to an XML file, that way you wouldn't need to add controls in code, however it depends on the complexity of your application.
I am pretty new to Xaml but to add to Jean-Louis's answer if you do not want to add the namespaces to every element string then you can do something like this using the System.Windows.Markup namespace:
ParserContext context = new ParserContext();
context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
string xaml = String.Format(#"<ListBoxItem Name='Item{0}' Content='{1}' />", itemID, listItems[itemID]);
UIElement element = (UIElement)XamlReader.Parse(xaml, context);
listBoxElement.Items.Add(element);
Adding controls through the Children.Add method is the quickest way i've found, such as for example
this.Grid.Add(new TextBox() { Text = "Babau" });

Categories

Resources