How do I go about saving dynamically added WinForms controls?
My application should be able to do this for the following: TabPage, GroupBox, RadioButton, and CheckBox. The RadioButton and CheckBox both correspond to a block of text that will be outputted when selected. I understand the process would consist of creating the controls first and storing them, but I am unsure whether what I have found so far (listed below) are the most apt ways to go about doing so.
Creating the Controls
The topic I linked suggested the use of objects that inherit from specific controls. This means I should just instantiate such objects as needed and assign the dynamic values accordingly. It appears to be the easiest way to do this step unless someone suggests otherwise.
Storing Controls
For this part, I was thinking of:
A. Database
My application uses SQLite to save the text and the control properties could also be saved into and retrieved from it via a method from the aforementioned classes.
B. XML
Perhaps a configuration file that I will make myself to write and read from? Tutorials seem aplenty on how to process XML, which makes it doable for me. I am unsure, however, whether storing lengthy text is advisable here, and likewise for what follows.
C. Serialization
Researching got me 3 results--JSON, XmlSerializer, and BinaryFormatter--all of which I have not had experience with as far C# is concerned. There seems to be difficulty in deserializing controls, and each control needs to be serialized differently.
D. UserSetting
This appears to be similar to XML, only that it uses StringCollection or Settings.setting instead.
Suggestions, recommendations, or corrections anyone?
Related
I'm totally new to C#, and got an assignment building a WPF form to modify settings in an existing C# program using Visual Studio and WPF.
I have numerous goals:
Get a list of all of the Settings names.
Retrieve all of the user properties.
Add a line for every setting to the form, and allow the user to restore the original settings, use the current ones, or modify them to a new value.
Make sure that the user input is in the correct Type.
Bind between the TextBox and the values in the line, not sure what is the best way to send the details. What object do you recommend to bind to the xaml? The list containing the property lines? Or to bind every line separately?
I think that I need to access the App.config file for 3, so far unsuccessfully. I would have like to get an advice about the architecture, since I'm new to VS, C# and WPF.
I don't think this is an assignment suitable for someone new to wpf let alone c#.
Even experienced wpf developers are likely to find some tricky bits in this task.
app.config will be in the same folder as your exe. If that is in program files then you will not be able to edit and save. Unless your users are win 7 or earlier.
User settings will usually be stored in appdata for this reason. Since the user is expected to likely want to change them.
They go in a user.config file. The location of one off my system is:
C:\Users\Andrew\AppData\Local\MapEditor\MapEditor.exe_Url_aszfdqs5110y44xmg0kfuuqbatf5la5a\1.0.0.0\user.config
I am user Andrew on the machine and these are the user settings for MapEditor.exe.
The file itself is xml.
In there I see
And there's a bunch of stuff inside that.
I would not try and edit xml directly.
And this is your first bit you'll find isn't exactly easy.
Because you need to translate xml.
"All the user properties."
You presumably know what they are.
Because the user can't just add them.
They need to be defined in the app.
I'd pick out the pieces you want them to change and copy data to a viewmodel. Or observable collection of viewmodels presented by a parent viewmodel.
Probably simplest as an observablecollection.
You can then have a different viewmodel class per type of property.
They enter a string in a number then it'll fail to transfer back to the viewmodel and you can trap validation.errors that'll bubble.
To save, translate the viewmodels back into xml and save it.
I am making an application that will generate and SQL scripts from a template and after taking input for different fields from the user.
There are many templates, so the GUI needs to adjust for the fields that the user will be filling out.
In the interest of keeping this scalable, I'd rather not hardcode the GUIs into the program, but would like have it read from an XML file and change based on the template the user has selected.
This is preferred because if a new template were to arise, then all that the program needs is a XML file that corresponds to the template. And the actual code does not need to be changed.
I have my eyes set on using C# for this, as I have good experiences using it.
I am open to suggestions for other languages though.
Edit: This is a project for work, and I wanted to be sure that this is possible with C# before convincing my employers to expand into using C#.
You could do this sort of thing by subclassing Windows.Forms.Form and adding a constructor to accept your XML file as a parameter. Add a parser for your XML file that will interpret instructions for which labels and fields you want to add to a consistent form design (say, two columns with a label for field name on the left and the actual input field on the right, easily achievable by filling the form with a TableLayoutPanel). You just need to lay out your design constraints from the beginning and stick to them.
This is essentially what visual studio does when you create a form through the designer anyway, so I'd suggest you start by creating an example form manually and just looking at the kind of code it places in the form's designer.cs file
Any language can do that. It's more about design patterns than specific technologies.
If you meant writing a GUI only declaratively and with XML, though, then no. You'd have to write your own parsing and GUI assembling code.
If I want to add a string as a setting or an image as a resource – I do it through Visual Studio.
But how do I add something complicated such as a large array or a Form which has to first be computed at runtime (or in case of the Form – populated with controls)?
I thought I could run it and persist it in settings (Properties.Settings.Default.Setting1 =...), and then publish. But that doesn't work (See: How to persist from build programmatically? ).
So how is it done?
Have you read up on serialization? I know that's kind of a generic answer, but I hope it helps.
As for a large array, you need to come up with an approach or mechanism for storing the data.
If it's a simple array that won't change very often, you can store it in the app.config.
If it's a datasource (ie: the data changes often) you could use an XML file and ideally a database.
I'm not really sure what you mean by persisting a Form. A Form should contain all controls compiled within it's own executable or dll. If you are asking how to populate the form, you would do this in the Main() method of the main form.
My goal are to create a module that holds a listview with entries of xml files. The xml files are just serialized arrays of some object. Country{Code,Name,URL} as an example.
When an entry is clicked another control should show all the items in some kind of item control with styling based on the type(The idea are from WPF). I assume its possible to create some kind of style/rules for possible item types, in above case Country. (any ideas/advises on how to achieve this is welcome).
Now in the list of styled items, one should be able to select wanted items, and export this to a new XML file based on a new type SelectedContries{Code,Name,Url, Params}. The user will then need to add in the extra params in the new list and export(serialize).
My question is now, to do such module what would be the proper way. One module with 3 controls. I am new to module development so please explain deeply. Do i create Asxc files within the module as my controls?
Based on my explained goal above, any links to guides that might be good for exactly this would be nice and any general advises are also welcome. Thanks.
if I got you right, that you want to have a grid on your module showing all your country entries read out from a xml file.
The user can edit or add some detail information to those entries and export (serialize) it again into xml.
I would use the implemented Telerik Grid RadGrid (DNN wrapper is called "dnnGrid") to show all xml items in a grid on your module.
Then I would enhance the grid for your special needs adding some new columns or edit the existing (dont know exactly what you want to edit in this grid).
The last step is to serialize the entries again this should be done with a extra button calling the serialize method.
These are rly basic ideas from me. Iam a dnn module developer I think I could help you better if you explain it to me a lil detailed, show me a bit of the xml file and so on.
best regards, noone
If your app has 1 form, that has n group boxes each of which contain any number of controls. How would you save the settings of all these controls?
A few things I am concerned are:
I don't need properties like BackColor to be saved, because they aren't changed by the user.
I need a way so that when I add new properties or remove some properties from these custom controls, they don't have problem loading the settings back.
How to best do this?
I was thinking of using serialization? If that would be better, which serialization is recommended? Binary/Xml/? Examples are welcome.
I would use data binding to bind the content of all relevant controls to a custom (non-ui) class, then I would just serialize (XML or binary) that class. I would choose XML serialization at least while you are developing, since it will help you when debugging (you can take a look at the serialized files at any time).
I would say if you are storing a model that the properties could change, you could take a look at Custom Serialization. It talks at the end of the article about storing versioning information for changes in the model to help with deserialization.