On request of a reader, I’ll show you today some C# basics. After I had posted a little snippet for dealing with the AppSettings, I was asked how we could now actually save user input on a GUI with help of this snippet. Here comes the solution.
For clarification, I have a created a little C# WinForms project in Visual Studio. The WinForm got a label and a textbox on it. Also the above mentioned functions for loading and storing values in the AppSettings will be used.
If you have created the project and added to the controls, your Application should look similar to the left-hand picture.
Next, you two need to add event handlers. The easiest way is via the “Events” window. (This is the one that is marked red in the alongside picture.)
There you create an event handler for the Load, and one for the Form-Closing event. Finally, we need to animate the two methods. Because the required code is not too long, it is explained by the comments.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Configuration; namespace SaveUserInput { public partial class FormMain : Form { public FormMain() { InitializeComponent(); } public string getAppSetting(string key) { //Load the AppSettings Configuration config = ConfigurationManager. OpenExeConfiguration( System.Reflection.Assembly. GetExecutingAssembly().Location); //Return value which matches the given key return config.AppSettings.Settings[key].Value; } public void setAppSetting(string key, string value) { //Load AppSettingss Configuration config = ConfigurationManager. OpenExeConfiguration( System.Reflection.Assembly. GetExecutingAssembly().Location); //Check if entry (which matches the key) exists if (config.AppSettings.Settings[key] != null) { //Delete it for "overwrite" process config.AppSettings.Settings.Remove(key); } //Create new KeyValue pair config.AppSettings.Settings.Add(key, value); //Save the changed settings config.Save(ConfigurationSaveMode.Modified); } private void FormMain_Load(object sender, EventArgs e) { try { //Load settings into the textbox textBoxInput.Text = getAppSetting("user_setting"); } catch (Exception ee) { //Handly possible errors textBoxInput.Text = ee.Message; } } private void FormMain_FormClosing(object sender, FormClosingEventArgs e) { //Save settings when application is closing setAppSetting("user_setting", textBoxInput.Text); } } }
That’s quite simple, isn’t it? If you have any questions, just write me a comment. You might want to download the entire Visual Studio project, if so, use this downloadlink.
Best regards,
Raffi
This works great! But I do have a question for you (I am new to C# and app development)…
Why, when I make this component a separate function, does it no longer work correctly?
YOUR CODE:
public void setAppSetting(string key, string value)
{
//Load AppSettingss
Configuration config = ConfigurationManager.
OpenExeConfiguration(
System.Reflection.Assembly.
GetExecutingAssembly().Location);
//Check if entry (which matches the key) exists
if (config.AppSettings.Settings[key] != null)
{
//Delete it for “overwrite” process
config.AppSettings.Settings.Remove(key);
}
//Create new KeyValue pair
config.AppSettings.Settings.Add(key, value);
//Save the changed settings
config.Save(ConfigurationSaveMode.Modified);
}
MY NEW CODE:
public void setAppSetting(string key, string value)
{
//Load AppSettingss
Configuration config = ConfigurationManager.
OpenExeConfiguration(
System.Reflection.Assembly.
GetExecutingAssembly().Location);
deleteAppSettings(key);
//Create new KeyValue pair
config.AppSettings.Settings.Add(key, value);
//Save the changed settings
config.Save(ConfigurationSaveMode.Modified);
}
public delete deleteAppSettings(string key)
{
//Check if entry (which matches the key) exists
if (config.AppSettings.Settings[key] != null)
{
//Delete it for “overwrite” process
config.AppSettings.Settings.Remove(key);
}
}
Sorry I ment
public void deleteAppSettings(string key)
not
public delete deleteAppSettings(string key)
public void deleteAppSetting(string key)
{
//Load AppSettings
Configuration config = ConfigurationManager.
OpenExeConfiguration(
System.Reflection.Assembly.
GetExecutingAssembly().Location);
//Delete key
if (config.AppSettings.Settings[key] != null)
{ config.AppSettings.Settings.Remove(key); }
}
I don’t see, why your code shouldn’t run. What error code do you get?
It displays all the past values for each field on the UI as a continuous string. I am not sure why it would do that though… the only difference is it is in a different module…
I haven’t tested it, but you presented it clearly and I understand what you are doing. Thank you for sharing.