Remembering the state of a property.
-
Hello, within a plugin dll I instantiate an object as a property. The properties of this object are visible in the UI of the application that uses the plugin dll. I do not have acces to the code of the application. If I use the following code:
[IsExpanded()]
[Display(Name = "Direction")]
public FilterManager Direction { get; set; }the properties of object "Direction" are visible in the UI. If I use the following code:
[Display(Name = "Direction")]
public FilterManager Direction { get; set; }the properties ob object Direction are no visible in the UI and the user has to expand it manually first. Question: Is there a way to save the last state of object "Drection", i.e. "expended by the user" or "not expended by the user", so that next time the user opens the UI he can start to work where he stopped working last time (otherwise the user must click himself trogh many levels) ?
-
I do a similar thing for forms: each "generic" form remembers it's own position on screen, and will open there regardless of which application caused it to be created (so a "move files" project displays a status screen which appears in the same place from multiple apps).
The way I do it is to create an ini file under the %Appdata% folder, and read that in my Load handler, then when teh form closes I check and if neccessary update the ini file:
/// <summary> /// Restore size and location (if the user doesn't /// override it) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void frmMain_Load(object sender, EventArgs e) { //TODO: Remove this code to not save and restore your form location and size if ((ModifierKeys & Keys.Shift) == 0) { this.LoadLocation(); } } /// <summary> /// Save the size and location (if the used doesn't /// override it) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void frmMain_FormClosing(object sender, FormClosingEventArgs e) { //TODO: Remove this code to not save and restore your form location and size if ((ModifierKeys & Keys.Shift) == 0) { this.SaveLocation(); } }
/// <summary> /// Get the current user data folder /// </summary> public static string UserDataFolder { get { Guid appGuid = AppGuid; string folderBase = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); string dir = string.Format(@"{0}\{1}\", folderBase, appGuid.ToString("B").ToUpper()); return CheckDir(dir); } } /// <summary> /// Fill out the static properties /// </summary> static Storage() { LocationsStorageFile = UserDataFolder + "control.storage.locations"; }
/// <summary> /// Save the control location /// (Because a Form is derived from Control, it works /// for them as well) /// </summary> /// <param name="control">Form to save location of</param> /// <param name="instance">Instance identifier (for use with multiple instances)</param> public static void SaveLocation(this Control control, string instance = null) { string controlName = control.GetType().Name; if (!File.Exists(LocationsStorageFile)) { CreateBlankLocationFile(); } DataTable dt = ReadXML(LocationsStorageFile); if (dt.Columns.Count >= 6) { bool ignoreInstance = string.IsNullOrWhiteSpace(instance); DataRow current = dt.NewRow(); // Assume new row or instance. current["ControlName"] = controlName; current["Instance"] = instance ?? ""; foreach (DataRow row in dt.Rows) { if (row["ControlName"] as string == controlName && (ignoreInstance || row["Instance"] as string == instance)) { // Found it! //current = row; dt.Rows.Remove(row); break; } } current["LocationX"] = control.Location.X; current["LocationY"] = control.Location.Y; current["SizeW"] = control.Size.Width; current["SizeH"] = control.Size.Height; dt.Rows.Add(current); WriteXML(dt, LocationsStorageFile); } } /// <summary> /// Load the Control location /// (Because a Form is derived from Control, it works /// for them as well) /// </summary> /// <param name="control">Form to save location of</param> /// <param name="instance">Instance identifier (for use with multiple instances)</param> public static void LoadLocation(this Control control, string instance = null) { string controlName = control.GetType().Name; if (!File.Exists(LocationsStorageFile)) { CreateBlankLocationFile(); } DataTable dt = ReadXML(LocationsStorageFile); if (dt.Columns.Count >= 6) { bool ignoreInstance = string.IsNullOrWhiteSpace(instance); DataRow current = dt.NewRow(); // Assume new row or instance. current["ControlName"] = controlName; current["Instance"] = instance ?? ""; current["LocationX"] = control.Location.X; current["LocationY"] = control.Location.Y; current["SizeW"] = control.Size.Width; current["SizeH"] = control.Size.Height; foreach (DataRow row in dt.Rows) { if (row["ControlName"] as string == controlName && (ignoreInstance || row["Instance"] as string == instance)) { // Found it! current = row; int x, y, w, h; if (int.TryParse(current["LocationX"].ToString(), out x) && int.TryParse(current["LocationY"].ToString(), out y) && int.TryParse(current["SizeW"].ToString(), out w) && int.TryParse(current["SizeH"].ToString(), out h)) { control.Location = new Point(x, y); control.Size = new Size(w, h); } break; } } } }