Windows Forms: Save TextBox, Label and CheckBox in User Settings in C#
By FoxLearn 7/4/2017 9:50:33 PM 11.01K
Step 1: Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "UserSettingDemo" and then click OK
Step 2: Design your form as below
Step 3: Right click on your project -> properties -> Settings tab, then add properties as below
The .NET Framework 2.0 allows you to create and access values that are persisted between application execution sessions. These values are called settings. Settings can represent user preferences, or valuable information the application needs to use.
For example, you might create a series of settings that store user preferences for the color scheme of an application. Or you might store the connection string that specifies a database that your application uses. Settings allow you to both persist information that is critical to the application outside of the code, and to create profiles that store the preferences of individual users.
Step 4: Add code to handle your form as below
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace UserSettingDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Load data from user settings Text = Properties.Settings.Default.Title; chkRememberPassword.Checked = Properties.Settings.Default.CheckBox; lblValue.Text = Properties.Settings.Default.Label; txtValue.Text = Properties.Settings.Default.TextBox; this.Location = new Point(Properties.Settings.Default.PX, Properties.Settings.Default.PY); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { //Save data to user settings Properties.Settings.Default.CheckBox = chkRememberPassword.Checked; Properties.Settings.Default.Title = txtTitleForm.Text; Properties.Settings.Default.Label = txtValue.Text; Properties.Settings.Default.TextBox = txtValue.Text; Properties.Settings.Default.PX = this.Location.X; Properties.Settings.Default.PY = this.Location.Y; Properties.Settings.Default.Save(); } private void btnSetTitleForm_Click(object sender, EventArgs e) { this.Text = txtTitleForm.Text; } private void btnSetValueLable_Click(object sender, EventArgs e) { lblValue.Text = txtValue.Text; } } }
VIDEO TUTORIALS
- How to Use Form Load and Button click Event in C#
- How to use Advanced Filter DataGridView in C#
- How to use TagListControl in C#
- How to use Error Provider in C#
- How to Drag and Drop controls in C#
- How to Create a Random Password Generator in C#
- How to make an Application auto run on Windows startup in C#
- How to Create a Wait Form Dialog in C#