Microsoft Visual C# 2010 Express
I've been looking all day for a way to somehow save a TabPage containing runtime-added images and controls (the user can move them in the TabPage) so that they can be used later. I tried using serialization but it seams that you can't serialize interfaces. I'm using a SqlCe database but I couldn't find a way to store it there either.
Can someone please point me in the right direction?
Thanks in advance.
[Later edit]
Thank you for your answers. I've managed to do it following your advice, saving each controller's proprieties into the database. Basically, I'm not saving the controller, I'm recreating it every time I need it. It's a little messy but it solves my problem.
If you want to save in a database you need to create a table where to store all the information needed to restore the state of the interface.
Most likely, you will need to store some key data for each image you save:
Position, Size, Image source (possibly a file name or another resource), etc, depending on your needs.
You don't get a ready to use solution for that.
As Adi said, you have to do that manually by saving the key properties in database, have a look at Create And Host Custom Designers With The .NET Framework 2.0. It will help you with using the Visual Studio designer itself.
Note, you can download a sample of the designer and the source code exists as well.
Good luck!
Thank you for your answers. I've managed to do it following your advice, saving each controller's proprieties into the database. Basically, I'm not saving the controller, I'm recreating it every time I need it. It's a little messy but it solves my problem.
Related
To make things a tad simpler in my C# application, I decided to save all user style changes using the Properties.Settings properties, which works as expected.
But I liked the style change aspect of my app so much, and it is so important to the usage, that I have now decided to have savable and loadable style files(MyStyle.xxx) for the user to save and reload. But as I have never used these user settings before, I am unsure of how to duplicate and save this object. Can it be done simply, or can I maybe write these objects to serialized XML? What is the common approach?
P.S. I tried researching this topic but because it is so closely related to just saving user settings, all I could find was questions about saving user settings.
This is sort of sketchy but it has worked for me in the past and seems to be a pretty decent solution..
Firstly, create a list of settings you want and some default values.
Then create some JSON-esque format for your data and save in the setting as a string. From there you can jsut copy paste this template and make say 3 more string in your settings file for a total of 4 strings with the same JSON stuff in it.
It's a bit of a fuss parsing that info in and out but if you do set it up in JSON format then it shouldn't be too hard.
I am a total beginner of ASP.NET. I have created a webform in Visual Studio and now I need to figure out how to make the webform submit the data to a record in SQL. I assume there are thousands of tutorials out there for this but I can't seem to find any. Everything I find seems wildly over-complicated to just simply write data to a record. (or maybe its just my lack of understanding .NET that makes it seem over-complicated.)
I dont need to exchange data, or update live data on a page form the db, I just need to write the data to a record.
Can anyone point me in the right direction?
You could use the scaffolding feature to generate your application.
All you need to do is setup your connection string (pointing to your database) and follow this tutorial (http://www.c-sharpcorner.com/UploadFile/4b0136/getting-started-with-web-forms-scaffolding-in-Asp-Net/).
As simple as this!
Follow this link: http://mrbool.com/inserting-data-into-sql-server-database-using-csharp-and-asp-net/25091
or this link
http://www.c-sharpcorner.com/Blogs/12611/easy-steps-to-insert-data-into-sql-server-using-Asp-NetC-Sharp.aspx
I'm currently working on a Microsft Word Application-Level Add-in using C#. My application contains an app.config file. In this file I save user-settings (userSettings-Section) and some data defined by a custom ConfigurationSection. The data stored inside of custom ConfigurationSection is also user-specific.
I access the user-settings as follows:
Properties.Settings.Default.MyUserSetting
The custom ConfigurationSection I'm using like:
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal)
.GetSection("MyCustomConfigurationSection")
My questions are:
Do I even use the app.config the "correct" way? (I know that there is probably not only one correct way, but is "my way" one of these?)
Which ConfigurationUserLevel does Propeties.Settings.Default use? Is there a way of setting/changing it?
And Most importantly: Is there a way to automatically keep the user-specific settings during an upgrade of my application, but at the same time add newly created settings?
I deploy my Add-in using an installer built with WiX. At the moment I use CustomActions, which manually insert code for each added/removed/changed setting. But I was wondering whether there is a built-in way of doing this.
I did some researches the whole day now and I think I can answer my questions for myself:
Q: Which ConfigurationUserLevel does Propeties.Settings.Default use? Is there a way of setting/changing it?
A: It seems that Propeties.Settings.Default use ConfigurationUserLevel.PerUserRoamingAndLocal, what sounds logical as they only contain userSettings... I don't know whether it is possible to change it, but for me it is not necessary.
Q: Is there a way to automatically keep the user-specific settings during an upgrade of my application, but at the same time add newly created settings?
A: I found out that there is an Upgrade()-Method in Settings which should take care of copying user settings between an older version of the product and the new one. For more information see http://ngpixel.com/2011/05/05/c-keep-user-settings-between-versions/. I must confess that I haven't had occasion to test it, since I have no permissions to build the installer, but I will rely on it. I will come back to this post after I tested my solution.
Furthermore I am not using a custom ConfigurationSection any more. Instead I derive from System.Configuration.ApplicationSettingsBase, which means that I am able to handle the custom data exactly like the Properties.Settings, including the Upgrade()-Method. See this post for more information about how to use it.
Hope this helps somebody.
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'm building a generic survey engine where end users can generate and design their own surveys (including UI design). I'm interested in building (or perhaps even buying) a custom WinForms designer which would be able to output design in XML format. I'd then save the XML to database and when users load a survey, XML would be parsed and the form would be dynamically generated based on the XML data.
Building my own C# to XML converter is an option, but I'd like to avoid it since users would have to use Visual Studio to get ahold of WinForms code and then run a custom tool to generate XML output, which is a pretty clumsy solution (besides I can't force customers to buy VS).
I'd appreciate it if anyone could point me in the right direction.
Thanks!
The bits of the WinForms Designer are exposed by the .NET framework: Design surface, toolbox, property grids,...
This MSDN article and this CodeProject article should help you get started.
Of course, this is only the start of the story.
Your second problem is to make the designer emit XML rather than C#/VB code. Last but not least, you'll have to create the form at runtime, which is usually done by compiling the code generated by the designer. Needless to say that compiling XML is another story.
It would certainly be easier to store the C# code in your DB rather than some XML model. You can then compile it programmatically at runtime (no need to have VS installed on the client's computer. The .NET runtime is enough) and instantiate the form class dynamically. BTW, whether you complie the code in the designer or in runtime is up to you.
However, other routes may be easier, such as Hans' suggestion: InfoPath.
The Mono project has a Winforms Designer. I don't know how easy it would be to re-use as a componant. Here it is in action:
I don't know how you would go about serializing the output to XML and deserializing it again. Maybe you could even store the C# in your database?
Check this post on how to create xml files in C#: Working with XML
Then use sp_xml_preparedocument/sp_xml_removedocument in SQL to create/parse xml files (use stored procedures to do this)
Traverse and use the generated xml in your winform (I suggest you use LINQ to XML to do this)
Probably the hardest thing you'll encounter here is the creating xml part and traversing the xml nodes, but aside from that everything will be a breeze.
We're already implementing this structure/architecture at work but on webforms not winforms. But I think the flow/logic is the same. HTH.
Edit: This post might help you. Cheers.