Home > Plug-in Development > How to implement the Options page logic for storing settings

How to implement the Options page logic for storing settings

February 25th, 2011

Here’s the second part of the post about adding an options page into your DXCore plug-in. See the other parts, to learn more:

  1. Adding and designing an options page
  2. Implementing the options page settings storing logic (this post)
  3. Using settings from an options page inside a plug-in

Here are the steps we need to accomplish, to implement a logic for your options page, and make it load and save its settings correctly:

  1. Prepare the options page (set values for the controls based on the settings in storage)
  2. Optionally specify default values (enables the “Defaults Settings…” button)
  3. Commit changes (transfer values on the controls to storage)

Preparing the options page

Handle the PreparePage event of your new options page. This event is triggered just before your options page is first shown to the user. Use the ea.Storage parameter to read the settings from the decoupled storage and assign them to your controls, using code like this:

private void OptMySettingsPage_PreparePage(object sender, DevExpress.CodeRush.Core.OptionsPageStorageEventArgs ea)
{
  chkEnabled.Checked = ea.Storage.ReadBoolean("Preferences", "Enabled", true);
  // ...
}

Show Visual Basic code »

Private Sub OptMySettingsPage_PreparePage(sender As System.Object, ea As DevExpress.CodeRush.Core.OptionsPageStorageEventArgs) Handles MyBase.PreparePage
  chkEnabled.Checked = ea.Storage.ReadBoolean("Preferences", "Enabled", true)
  ' ...
End Sub

Replace the “// …” in the sample code above with similar calls to transfer the loaded data to other controls on the form as needed.

Specifying default values

It is recommended to provide default settings for your options page. To do this, handle the RestoreDefaults event. If you handle this event, the “Default Settings…” button will be enabled on the Options Dialog when your page is displayed:

IDETools OptionsDialog buttons bar

Use code like this, to initialize the controls to their default values:

private void OptMySettingsPage_RestoreDefaults(object sender, DevExpress.CodeRush.Core.OptionsPageEventArgs ea)
{
  chkEnabled.Checked = true;
  // ...
}

Show Visual Basic code »

Private Sub OptMySettingsPage_RestoreDefaults(sender As System.Object, ea As DevExpress.CodeRush.Core.OptionsPageEventArgs) Handles MyBase.RestoreDefaults
  chkEnabled.Checked = true
  ' ...
End Sub

Clicking on the “Default Settings…” button will fire this event. Here, you need to adjust properties of the controls on the options page. This event is not supposed to store settings – just change the controls.

Commiting changes

Handle the CommitChanges event of your new options page. This event is triggered when the user clicks either the OK or the Apply button on the IDE tools Options Dialog. Use the ea.Storage parameter to write the settings to the decoupled storage, like this:

private void OptMySettingsPage_CommitChanges(object sender, DevExpress.CodeRush.Core.OptionsPageStorageEventArgs ea)
{
  ea.Storage.WriteBoolean("Preferences", "Enabled", chkEnabled.Checked);
  // ...
}

Show Visual Basic code »

Private Sub OptMySettingsPage_CommitChanges(sender As System.Object, ea As DevExpress.CodeRush.Core.CommitChangesEventArgs) Handles MyBase.CommitChanges
  ea.Storage.WriteBoolean("Preferences", "Enabled", chkEnabled.Checked)
  ' ...
End Sub

These three events take care of saving and loading settings for the options page. The next step is to ensure that your plug-in is notified whenever users make a change on your new options page.

—–
Products: DXCore
Versions: 10.2 and up
VS IDEs: any
Updated: Nov/26/2011
ID: D066

Similar Posts: