Another way to achieve this is to create an Application Setting [
^] of Type Bool. In this example, the FormClosing EventHandler compares the CheckBox state to the Setting, and then adds, or deletes, the Registry Key if necessary.
Set the 'Scope parameter for your 'bool Setting to 'User, not 'Application.
using System;
using System.Windows.Forms;
namespace AutoOpen
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool isAutoStartNow, isChecked;
private void Form1_Load(object sender, EventArgs e)
{
isAutoStartNow = (bool)Properties.Settings.Default["AutoStart"];
checkBox1.Checked = isAutoStartNow;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
isChecked = checkBox1.Checked;
isAutoStartNow = (bool)Properties.Settings.Default["AutoStart"];
if (isAutoStartNow == isChecked) return;
Properties.Settings.Default["AutoStart"] = isChecked;
if (isChecked)
{
}
else
{
}
Properties.Settings.Default.Save();
}
}
}