How to save Application theme name in WPF - c#

I am using Devexpress and WPF. There are different themes user can apply provided by devexpress.
ThemeManager.ApplicationThemeName = Theme.MetropolisDarkName; //MetropolisDarkName is name of a Theme.
In my application user can select any theme to apply. But if he closes application and opens it again, Themes changes doesn't same. I want that these changes should be saved so if user after applying theme will open it again, changes should be saved and apply.
Should i have to save name of Theme in database or is there any other way to solve this. I need your suggestions.
Thank you.

The super easy way to do this in Visual Studio is to add a new .settings file to your project, and define a setting of ThemeName. You can find the settings template under General in the C# project Templates. The settings file itself is just a designer with an underlying class of type System.Configuration.ApplicationSettingsBase.
The class created saves setting values to the app.config. The neat thing is you can define settings as application or user, so different users using the application on the same machine can have their own custom settings.
The following assumes that you created the file Settings.settings with an entry called ThemeName of type string.
Get the theme from Settings
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ThemeManager.ApplicationThemeName = Properties.Settings.Default.ThemeName;
}
Save the Theme
public void SetTheme(string themeName) {
ThemeManager.ApplicationThemeName = themeName;
Properties.Settings.Default.ThemeName = themeName;
Properties.Settings.Default.Save();
}
Settings on MSDN

Related

Set the Button Text according to Resource File in Visual Studio 2013

I am an Amateur Visual C# Programmer.
I want to implement the concept of I18N in my project.
(Similar to strings.xml in Android)
I have added 2 String Resources in my Resource File.
And I have added 2 Buttons in my form.
Now, I want the text of the Button to be the string value from the Resource file.
Please help.
Adhish
If you are using windows form, you can use this procedure.
Open the properties menu of the form in the design. Select the option "Language" and choose the language that you want.
Selecting a different language will create automatically a new file resource with the label for the language selected.
The file .resx will the same name of the form plus the initial of the language used.
It really depends on your UI framework. The following solution is for WinForms, but you can implement it (without MVVM) in WPF as well.
Create a button in the designer.
Register to the Load event of the form by double clicking it. (Right click on the form => properties => click the events button => search for the Load event)
In the load event, initialize the text of the button with the string from the resources.
Your code should look something like this:
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = MyResources.ButtonText;
}

How can I change the RequestedTheme of an element from a control in a settings flyout?

I'm trying to set an option in a settings flyout in my WinRT app for the user to change the theme of the app.
In the App.xaml file, I've set up different ResourceDictionaries for the light and dark themes. I can set the desired theme of my application from it's settings file with the following code (RootPage is the name of the grid the contains all of the elements):
if ((string)Windows.Storage.ApplicationData.Current.LocalSettings.Values["Theme"] == "0")
{
RootPage.RequestedTheme = Windows.UI.Xaml.ElementTheme.Light;
}
if ((string)Windows.Storage.ApplicationData.Current.LocalSettings.Values["Theme"] == "1")
{
RootPage.RequestedTheme = Windows.UI.Xaml.ElementTheme.Dark;
}
However, I would like to be able to change the theme dynamically. Basically, I want the theme to change immediately when the user selects the desired theme from the settings flyout, rather than having to wait for the application to be restarted.
I've tried using the following command:
App.Current.RequestedTheme = Windows.UI.Xaml.ApplicationTheme.Light;
But this crashes the app with a System.NotSupportedException.
I've also tried setting the theme only to the RootPage element with the following:
var MainPage = Windows.UI.Xaml.Window.Current.Content as MainPage;
MainPage.RootPage.RequestedTheme = Windows.UI.Xaml.ElementTheme.Light;
But I've done this wrong because it raises a System.NullReferenceException.
Could anyone help me work out how can I change the RequestedTheme of an element from a control in a settings flyout?
Windows 8 theme resources are provided using StaticResource and so can’t be updated at runtime.
To somehow do what you want I'd recommend you to read this blog post.
Try setting the app requested theme within the App constructor in the App.xaml.cs.
Like this
public App()
{
Current.RequestedTheme = ApplicationTheme.Light;
this.InitializeComponent();
...
...
}

C# Plugin options

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();

Ideas on how to remember previous user selections on .NET WinForm

I am currently looking at a better approach to remember/persist previous state of the controls on a .NET Windows Form.
For example, there are 5 drop down list menu controls on a windows form. And user previously selected some items in these drop down menu. What I'd like to do here is: when this WinForm is loaded again, user's previous selections shall be recovered and remain the same.
For now, I can kinda think of a solution: store each selected value/index in a text file or registry key or something. And then read them every time the From is loaded.
But the thing is this approach would become inefficient to deal with a large number of controls and maintain their states.
So can anyone give me some thoughts or suggestions? What'd be the best way to do achieve it?
EDIT:
I just had a read about this article on MSDN, and this concerns me because I am doing the add-in project at the moment:
You cannot use application settings in an unmanaged application that
hosts the .NET Framework. Settings will not work in such environments
as Visual Studio add-ins, C++ for Microsoft Office, control hosting in
Internet Explorer, or Microsoft Outlook add-ins and projects.
You might want to look at Settings files
Saving User Settings
private void UserSettingsDemo_Load(object sender, EventArgs e)
{
txtServer.Text = Settings.Default.ServerNameSetting;
txtDatabase.Text = Settings.Default.DBNameSetting;
txtPassword.Text = Settings.Default.PasswordSetting;
txtUserId.Text = Settings.Default.UserIdSetting;
}
private void UserSettingsDemo_FormClosing(object sender, FormClosingEventArgs e)
{
Settings.Default.DBNameSetting = txtDatabase.Text;
Settings.Default.UserIdSetting = txtUserId.Text;
Settings.Default.PasswordSetting = txtPassword.Text;
Settings.Default.ServerNameSetting = txtServer.Text;
Settings.Default.Save();
}
Are you talking about keeping the previous state of a control after restarting the application? In that case, you probably won't have no choice than writing all the changes down in an configuration file for example.
If you are trying to navigate between different winforms and want to persist the changes, you could implement an history of controls. A datastructur like a stack should do the trick, with the following methods:
AddToHistory(Control control)
RetrieveLastOpen()
As far as you know what exactly you want to save it is ok to use smth like
(foreach var child in MainForm.Children.OfType<ComboBox>)
{
// Save properties of child into Dictionary<string, ComboBoxProperties>
}
and for loading you will do smth like this
(foreach var child in MainForm.Children.OfType<ComboBox>)
{
// Load properties of child from dictionary[child.Name]
}

Saving and restoring app settings in C# Forms

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.

Categories

Resources