Problem:
WPF application, no database.
It has main window, ribbon and there a button which opens new window, which has few checkboxes, textboxes which allows to set up parameteres how the job is gonna be done.
Now, how is the best way/best practice to store/save those parameteres and use them?
Settings class with properties and then creating global object when application starts? Then I could access this object in child window, save settings, then I could use such setting in MainWindow?
I guess it's a problem with OOP understanding.
Once I create Settings setting = new Settings(); in ChildWindow it is not accessible once window is closed.
But then, I've read somewhere to never set global objects and share them between windows.
Should I create Settings setting = new Settings() in MainWindow and then pass it in the constructor of ChildWindow? It could be like:
Settings setting = new Settings();
ChildWindow child = newChildWindow(setting);
Then in the new ChildWindow I could set up setting properties. As far as I know if you pass an object as a parameter it has reference to original object, so setting up properties in ChildWindow would affect MainWindow Settings object?
I know, the question is a bit messy, not sure if it's right place to ask such questions
If you just have a small number of fields to store, then using the built in User scoped Settings would be the easiest way. They are stored in XML files and each user will have their own settings file in a hidden data folder. You can set them up in a dialog window in Visual Studio and then refer to them simply in your code like this:
this.BackColor = Properties.Settings.Default.myColor;
Saving them is just as easy:
Properties.Settings.Default.Save();
These examples are from the following linked page... to find out full details, take a look at the Using Settings in C# page on MSDN.
Related
Currently, I am using WPF. I have two windows I-e window1 and window2. window1 has a button to navigate to window2; window2 has also a button to go back to window1. window2 has many controls also user-controls. The case is whenever I navigate window1 to window2, I have to create new object of window2 due to some reasons. I know singleton pattern but cant apply here.
Now, on unload event of window2 I saved window2's object in a static class(I save "this" to static class). when user will again navigate here, I want to load saved object rather than constructing new object.
Is there any method to load saved object in current object that's going to construct ? I-e somewhere in constructor or load I may load saved object?
Thanks in advance
As it is tagged wpf, you definitely should take a look at mvvm. Instead of passing data between forms you will have two ViewModels (for each form) to hold all associated with UI data and some Models to hold the rest. And those view models (and models) will persist between forms closing/opening, means no need to pass anything or save/load.
If you need to actually save data (to example, different sets of states or for the data to survive exit/start application), then have a look at serialization. My favorite is XmlSerializer/XmlDeserializer, as it's easy to control, easy to change (edit data) and easy to support versioning. There are dozens of tutorials around, simply search for "c# serialization" with optional words "xml", or "binary" or even "protobuf".
So I'm making a plugin for Visual Studio that opens Internet Explorer and goes to a specified website.
How can I make it so that you can right click the button or something and bring up options and enter in a new URL that is then saved? Not sure how I can do this!
Thanks for any help
You can create a new setting at design time by using the Settings designer. The Settings designer is a grid-style interface that allows you to create new settings and specify properties for those settings. You must specify Name, Value, Type and Scope for your new settings. Once a setting is created, it is accessible in code.
In Solution Explorer, expand the Properties node of your project.
Double-click the .settings file in which you want to add a new setting. The default name for this file is Settings.settings. In the Settings designer, set the Name, Value, Type, and Scope for your setting. Each row represents a single setting.
How to read settings
Via the Properties.Settings.Default member.
The following example shows how to assign myName setting to a string Name property.
this.Name = Properties.Settings.Default.myName;
How to write settings
Properties.Settings.Default.myName = "Robert"
If you want to persist the changes to the settings between application sessions, call the Save method, as shown below:
Properties.Settings.Default.Save();
I have a C# Dialog based app. I want to save the preferences/settings the user choose, so that I could reload them in the next run.
I am new to C#, may be this is something quite basic but I do not know.
Do I have to explicitly write them to a file like ini or something ? or is there a built in way to do that.
The kind of config data is like checkboxes selelected, numericUpDOwn, checkedListbox - checked items etc
Select the control in the designer. Scroll all the way up in the Properties window and expand (ApplicationSettings). Click the indicated button to open a dialog. Select the property whose value should be persisted (like Checked for a check box) and click New in the dropdown.
Be a bit careful, not all properties are suitable to be persisted like this. An example is the form's Size. You don't want to store the size when the form is minimized or maximized, that size won't restore well. You need to do this by adding the setting in the Settings designer an only write it when the control is in the right state. In the case of Size, that's when the Resize event runs and the WindowState is Normal.
After you create the application settings as the other answers suggest, make sure you don't forget to call Properties.Settings.Default.Save(), for example:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Properties.Settings.Default.Save()
}
To Create a New Setting at Design Time
In Solution Explorer, expand the Properties node of your project.
In Solution Explorer, double-click the .settings file in which you want to add a new setting. The default name for this file is Settings.settings.
In the Settings designer, set the Name, Type, Scope, and Value for your setting. Each row represents a single settings
For more info you can refer here
You should use application settings. These will persist their values after you close your application, and you will be able to read from them when the program starts back up again.
I looking for a simple example using c# to call the SetSelectionContainer method on the EnvDTE.Window class.
I would like to know what the classes should look like that I should pass to the method.
I have really searched the web now for days and could not even find anything remotely helpful.
Window window = _applicationObject.ItemOperations.NewFile(#"General\XML File", "", "");
TextSelection selection = (TextSelection) window.Document.Selection;
selection.SelectAll();
selection.Insert("<xml>some xml<xml>", (int)vsInsertFlags.vsInsertFlagsContainNewText);
object[] container = new[] {"Test1", "Test2"};
window.SetSelectionContainer(ref container);
Sets the objects that should be passed to the Properties window
whenever the window has focus. SetSelectionContainer works only on
windows created with the CreateToolWindow method. Other tool windows,
such as Solution Explorer and Task List, already have code for setting
what is displayed in the Properties window.
SetSelectionContainer
allows you to associate objects with the window so that whenever the
window has focus, the Properties window displays properties for those
objects. For example, you would use this property if you have a custom
tool window that displays a chart and you want to display properties
in the Properties window in order to change characteristics of the
chart.
If SetSelectionContainer is passed an empty Variant value, it
removes the displayed object. The object displays when the tool window
is active and the objects are available from the selection container.
Ref.
Have you downloaded the samples from here?: Automation and Extensibility for Visual Studio
From what I have discovered, you can pass simple properties, such as strings and ints in the object array parameters for this method.
It appears that the real trick to making this work is generating the notification (through ITrackSelection.OnSelectChange) that the properties have changed.
There is some working code for this here.
I am writing a Settings form for one of my applications. Once the user clicks Tool > Settings a new form comes up with the settings that can be changed.
The TopMost property is set the True and working properly.
What I can't seem to find how to do is to keep the Form on focus. I do not want the use to be able to leave the form. The user has to close the form to continue using the application.
Thank you...
You are probably using Show() to show the settings form. Instead, use ShowDialog().
http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx
Display it like this:
FormChild1 Child = new FormChild1();
Child.ShowDialog(this);