Author Topic: Storing Dialog or Forms settings with UserConfigurationManager  (Read 2165 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
Sorry if this has been covered and I have not used it enough to know if it is a good long term solution, but here is one way to store settings that will remain between opening and closing and between sessions.
 
Say you have a form with a Checkbox and TextBox

 
The methods of IConfigurationSection  to retrieve and store the settings are
Code - C#: [Select]
  1.     object ReadProperty(string name, object defaultValue);
  2.     void WriteProperty(string name, object value);
  3.  
ReadProperty takes the a name it was stored with and a default property if it is not found.
WriteProperty takes the name to store it with and the value to store.
Here is one way to use it
Code - C#: [Select]
  1.     public partial class Form1 : Form
  2.     {
  3.         public Form1()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.         private void Form1_Load(object sender, EventArgs e)
  8.         {
  9.             UserConfigurationManager mng = Autodesk.AutoCAD.ApplicationServices.Application.UserConfigurationManager;
  10.             using (IConfigurationSection sesction = mng.OpenDialogSection(this))
  11.             {
  12.                 if (sesction != null)
  13.                 {
  14.                     checkBox1.Checked = (bool)sesction.ReadProperty("IsChecked", false);
  15.                     textBox1.Text = (string)sesction.ReadProperty("SearchText", "");
  16.                 }
  17.             }
  18.         }
  19.         private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  20.         {
  21.             UserConfigurationManager mng = Autodesk.AutoCAD.ApplicationServices.Application.UserConfigurationManager;
  22.             using (IConfigurationSection sesction = mng.OpenDialogSection(this))
  23.             {
  24.                 sesction.WriteProperty("IsChecked", checkBox1.Checked);
  25.                 sesction.WriteProperty("SearchText", textBox1.Text);
  26.             }
  27.         }
  28.     }
  29.  
*************************Edit***********************
Of course needs to be opened through Application object
Code - C#: [Select]
  1.         [CommandMethod("TestForm", CommandFlags.Modal)]
  2.         public void TestForm() // This method can have any name
  3.         {
  4.             Form1 frm = new Form1();
  5.             Application.ShowModalDialog(frm);
  6.         }
  7.  
« Last Edit: April 07, 2013, 05:32:17 PM by Jeff H »

TheMaster

  • Guest
Re: Storing Dialog or Forms settings with UserConfigurationManager
« Reply #1 on: April 08, 2013, 02:57:25 AM »
You can do that without having to write any code.  :laugh:

Select a control on your form (or with nothing selected to create a setting for the form itself), and in the Properties tool window, expand the first item (ApplicationSettings). Then select (PropertyBindings), and click the button to the right.  In the dialog, enter a name for the setting, and the value.

In the case of a standalone executable, you're done. In the case of a form used in AutoCAD, you have to manually save the settings when the form is closing, which you can do using the Properties.Settings class created by the designer:

Code - C#: [Select]
  1.  
  2.     Properties.Settings.Default.Save();
  3.  
  4.  

The Settings class can also be manipulated manually, or modified to have additional settings that don't correspond to a Component. You'll find it in the Properties node of your project in Solution Explorer.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Storing Dialog or Forms settings with UserConfigurationManager
« Reply #2 on: April 08, 2013, 04:15:31 AM »
Thanks Tony,
That is how I normally do it, but would add a Settings File and did not know about adding the properties through the properties window.