Title: Display an end user license agreement (EULA) in C#
Many program display an end-user license agreement (EULA) when they are installed or the first time they are run. This example uses a form called EulaForm to display a RichText format EULA file. The form returns DialogResult.OK if the user clicks Accept and DialogResult.Cancel if the user clicks Decline.
The following code shows how the main program displays the form.
private void Form1_Load(object sender, EventArgs e)
{
// See if the user has already accepted the EULA.
if (RegistryTools.GetSetting(
"howto_eula_once", "EulaIsAccepted", "False").ToString()
!= "True")
{
// Display the EULA.
EulaForm frm = new EulaForm();
if (frm.ShowDialog() == DialogResult.OK)
{
// Remember that the user accepted the EULA.
RegistryTools.SaveSetting(
"howto_eula_once", "EulaIsAccepted", "True");
}
else
{
// The user declined. Close the program.
this.Close();
return;
}
}
// Perform other initialization stuff.
...
}
The code first uses the GetSetting method described shortly to see whether the EulaIsAccepted value stored in the registry is true. If the value is not true, then the user has not previously accepted the EULA, so the program displays the EulaForm.
If the user accepts the EULA, the code sets EulaIsAccepted to true in the registry so the program does not show the form in the future. If the user declines the EULA, the program exits.
To make using the EULA form easier to use, its Accept button's DialogResult property is set to OK and its Decline button's DialogResult is set to Cancel. When the user clicks a button, the button sets the form's DialogResult property accordingly and that automatically hides the form, returning the appropriate result to the main program's call to ShowDialog.
The EulaForm uses the following code to load the RichText format file Eula.rtf into its RichTextBox control.
// Load the EULA file.
private void EulaForm_Load(object sender, EventArgs e)
{
rchEula.LoadFile("Eula.rtf");
}
To make it easy for the program to find the EULA file at run time, add it to the project by opening the Project menu and selecting Add Existing Item. Select the file and click Add.
Next select the file in Solution Explorer and use the Properties window set the file's Copy to Output Directory property to Copy if Newer. Now Visual Studio will copy the latest version of the EULA file into the executable directory so the program can find it.
The final pieces of interesting code in this project are the following GetSetting and SaveSetting methods.
// Save a value.
public static void SaveSetting(string app_name, string name,
object value)
{
RegistryKey reg_key =
Registry.CurrentUser.OpenSubKey("Software", true);
RegistryKey sub_key = reg_key.CreateSubKey(app_name);
sub_key.SetValue(name, value);
}
// Get a value.
public static object GetSetting(string app_name, string name,
object default_value)
{
RegistryKey reg_key =
Registry.CurrentUser.OpenSubKey("Software", true);
RegistryKey sub_key = reg_key.CreateSubKey(app_name);
return sub_key.GetValue(name, default_value);
}
These methods open the registry's CURRENT_USER hive, go into the Software key, and create a subkey named after the application. They then get or set a value inside the application's key.
The registry stores separate values for each user's CURRENT_USER hive, so each user must accept the EULA when first running the program.
Download the example to experiment with it and to see additional details.
|