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.
Related
I'm currently working on a winforms application that gets information from a user via a textbox. Since this information is not likely to change very often, I would like my application to save the input and load it into the appropriate text box the next time it is run.
I feel confident in my ability to set this up on the form side of things, my question has to do with how to store the values outside of the program. My first instinct is to just use a text file, but the overhead needed to handle IO and reading in the values seems a bit much. I also would rather not have the user editing it outside of the textbox in the program. I tried using string resources, but those are read only. Is there a more elegant solution available?
You need at least a mediocre database client whether it be sql server, mdf, MS Access, SharePoint, Oracle etc. Sounds like a vis studio prog. (winforms). They are designed for IO procedures but requires data architecture knowledge or relational dbs. (RMDBS). Try a lightswitch project (if a VS proj.) to get the basics of db and form data relationships.
I know it's not outside Your program, but you could try settings, it's easy, kinda like the resources ownly in the settings you can change it.
To change settings goto:
Properties and then double click Settings, here you van add strings, bools etc. Make sure you set them to user
To access them through code use:
Properties.Settings.Default.
To save the settings use:
Properties.Settings.Default.Save()
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?
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.
I have around 60 controls and I want to show a tooltip for each of them.
I could do this manually but I want something that would allow me to make changes to the tooltip messages or the control they are bound to without modyfing the code (I don't know what this would be called).
I thought about XML, but is there a better to store simple data like this?
I would recommend a custom configuration section. http://msdn.microsoft.com/en-us/library/2tw134k3.aspx. Then all you have to change are the values in the config file. You could even reference it as a separate file as to not clutter up your web.config.
If you wanted them to be user-configurable through an admin section I'd say put the text in a database.
Otherwise, I'd suggest putting it in resource files.