I'm trying to design my C# winform application with a very generalized function to automatically go through all of the form elements and save their states/values in a text file so that I can load it later. I want it to be very generalized so that it'll be a cinch to reuse this code in future projects, as it wouldn't be heavily tied down to the specifics.
The form elements I want to save are text boxes, combo boxes, data grid views, list boxes and that's about it. I want to save their values and everything about them.
One way that I was going about it was to go through every possible form element and then detect eachs type, and then create the corresponding c# code to re-create its value ('tboxmine.value="blue elephant"'), and then writing the code to a file, so that I could load the code from the file and execute it using the CSCcompiler. My code so far doesn't seem to be working correctly and I'm having my doubts that this compiler is actually running the code inside my application (I think it's possibly creating a new thread?), and it just seems like there's probably a far more straightforward relatively standard way of doing this.
This seems a bit like the reverse "best practice" approach. If you dont't know about databinding I suggest you look into that.
Basically you create classes to represent your data and use databinding to associate controls with your objects. The controls will automatically show the right value and allow the user to change it. If the user has changed the value, your object gets automatically updated.
To save the data, you would use some kind of serialization to store your objects in a file. When loading, you let the Serializer rebuilt your class structure and (best case) you are good to go.
This is not exactly what you asked for, but I think it is something you could use well ;-)
N.B.: Not the complete state of the control is saved. e.g. in a Textbox your text would be saved but the BackColor won't.
To get you started look into this tutorial: http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial
Related
I have been looking for a while now for a way to do this but nothing seems to be what I am looking for, but all I want is to enter in some data into a DataGridView, save it and then if I want, later load it back up. All the tutorial seem to focus on log in data from some database or something like that when all I want is a grid where I can put things in and add a search filter tool so I can look things up. It is for a project I am doing and I was wanting a way to save and organise data efficiently like how you can as if it was a text document but in a grid with a search filter as well as the way you can send file over to someone else who has the application and read the data. Sorry if I have put it in a confusing way.
I am doing this in Visual Studio 2019 in C#
You could use this one as a staring point. Although such approaches seem to be outdated. (Using datatables etc) Your best bet is using classes for serializing the data to json and sending the json file
I have a questions that is more theory of "best practices" than actual technical programming.
I am trying to think of the best way to handle editing an element after it is placed by my program. Specifically I have my program set up as follows:
User clicks a tool that essentially "adds some families into a project" in Revit. It reads a sketch created by user and places items based on the sketch.
Sketch
Element creation
I want to give the user the ability to "edit the sketch" of those elements similar to say a floor slab. I don't believe the Revit API exposes the ability to sketch using "sketch mode". I am trying to mimic this very useful capability in my program.
So, what I have done is used extensible storage and store an UniqueId into all elements created using my tool. What the program does is when the user clicks "edit elements" tool, the program asks for the new sketch, asks to click on existing element, reads the UniqueId of the existing element and calls all elements with that UniqueId and deletes them, then the program adds new elements into the project again using the users "new sketch".
The problem is, if the user deletes the original elements that were added to the project and then tries to edit them, how do I guarantee the user is not going to delete those original elements that have the UniqueId? I think the way to go is to use the Dynamic Model Update functionality in the Revit API.
How are most of these algorithms written? Am I on the right track here? Do I just assign UniqueIds to elements and store them on the elements themselves so I can call them up later? Maybe there is a basic theoretical piece of the puzzle I am missing. Data structures?
I think you are essentially on the right track.
Using extensible storage to store the UniqueIds is definitely the way to go, and also using the DMU dynamic model updater functionality to react to changes sounds good.
The one thing that seems to be unclear is the fact the Revit will automatically assign the unique ids to the elements when they are created, and there is nothing you can do to affect that. The unique id is unique and immutable, and you have no control over it.
Therefore, the easiest approach is probably to delete all the previous sketch elements and recreate the entire sketch and all elements defining it from scratch whenever the sketch needs to be modified in any way.
You should use ElementId instead of UniqueId for structured storage, because an ElementId is automatically remapped to the new ElementId when a work sharing update occurs. The ElementId is also set To ElementId.InvalidElementId when the element is deleted.
I want to create a program that will enable user to open a file. That would create a new tab and assign a class to that tab that contains over 200 fields of various types (which I have already designed, along with read and write code of the program).
Then I would create an interface that would enable user to change any of that 200 fields.
Does that mean I have to create a property for each and every single of 200 fields that I have in order to bind them with fields?
Is there another way? Is this kind of program more viable for WindowsForms?
It seems a lot easier to do this programmatically instead of using XAML.
Is there a 3rd solution?
Maybe you are looking for the PropertyGrid. It may be not as beautiful as you'd expect, but it would cover major of your needs. There're some customizations examples, but it still looks ugly (IMO).
Otherwise you could create a control by yourself. Define deferent templates for various types of your properties. It shouldn't be difficult: some reflection, create bindings via C#, some templates.
I want to save state of lot of CheckBoxes (checked/unchecked) and some TextBoxes on a WPF screen in .settings file (As I do not want to use database/filing).
Whether I have to save all these checkboxes and textboxes in setings file or there is a better way to do it?
If this is to remember the state of controls in an application, I would definitely use user settings to store this. Creating a separate setting for each control can be time consuming initially, but it makes life a little easier later. However, if you don't want to do this, you can simply create a custom settings class with a public property for each control and then simply use an instance of that class as your one savable setting.
If it's possible it would be a great thing.
So is there a way how to achieve somehow a ViewState behavior into a WinForm's Application.
Like if i have a Bunch of TextBoxes ,than uses types text into them ,TextBoxes should store Their Text ,Location ,Size etc. and on Next Application Startup they should Load their Content Text ,Location ,Size etc.
Further...every Control into every Form ,should store Their State somehow ,and on Load ,they should Load their Content and Other Properties like they where last time Application was running.
PS : If there is not any Library or already made Solution ,than please don't try to spend time writing Code ,because it's not Fear to ask you guy's for Code ,i need just Direction's and Ideas because i plan to achieve it by my-self.Down-Voter's please consider that!!!
Using my google-fu I found this on code project.
The guy's written some code that effectively serializes a form out to XML so you can load it back in again. Don't know if it would cope with forms with dynamic controls that perhaps aren't on the form when you load it back in again - but I'm sure you can tweak the code to do what you want.