This is a class helper that will make it easier to save and load settings.
The class gathers information from any given control, creates a key, type, and value, and writes the information to a Json file.
This is an example of the json file content after saving.
{
"textBoxLensSize": "1000",
"checkBoxDrawBorder": false,
"checkBoxSaveCroppedImages": true,
"checkBoxClearShapes": true
}
The key names are the control names given to the designer by the user. And on the right, it's the values and states.
This is the class code.
using Images_Cropping.Properties;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace Images_Cropping
{
public class MySettings
{
private Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
private string jsonFilePath;
public MySettings(string filePath)
{
jsonFilePath = filePath;
}
public void LoadSettings()
{
if (File.Exists(jsonFilePath))
{
string json = File.ReadAllText(jsonFilePath);
keyValuePairs = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
ApplySettingsToControls();
}
}
public void SaveSettings(Control control)
{
// Store the control information in the dictionary
string controlName = control.Name; // Store the control name separately
keyValuePairs[controlName] = GetControlValue(control); // Use controlName as the key
// Save settings to the JSON file
SaveSettingsToFile();
}
public void SaveSettingsToFile()
{
string json = JsonConvert.SerializeObject(keyValuePairs, Formatting.Indented);
File.WriteAllText(jsonFilePath, json);
}
private void ApplySettingsToControls()
{
List<string> keysToRemove = new List<string>();
foreach (var key in keyValuePairs.Keys.ToList()) // Create a copy of keys
{
string controlName = key; // Get the control name directly
Control control = FindControlByNameRecursive(controlName);
if (control == null)
{
keysToRemove.Add(controlName); // Add to remove list
continue; // Skip controls not found
}
// Determine the property type and set the value accordingly
if (keyValuePairs[key] is bool boolValue)
{
if (control is CheckBox checkBox)
{
checkBox.Checked = boolValue;
}
}
else if (keyValuePairs[key] is string textValue)
{
if (control is TextBox textBox)
{
textBox.Text = textValue;
}
}
// Add more control types and properties as needed
}
// Remove keys for controls that were not found
foreach (string keyToRemove in keysToRemove)
{
keyValuePairs.Remove(keyToRemove);
}
}
private Control FindControlByNameRecursive(string name)
{
return FindControlByNameRecursive(Application.OpenForms[0], name);
}
private Control FindControlByNameRecursive(Control parentControl, string name)
{
if (parentControl.Name == name)
{
return parentControl;
}
foreach (Control control in parentControl.Controls)
{
Control foundControl = FindControlByNameRecursive(control, name);
if (foundControl != null)
{
return foundControl;
}
}
return null;
}
private object GetControlValue(Control control)
{
if (control is CheckBox checkBox)
{
return checkBox.Checked;
}
else if (control is TextBox textBox)
{
return textBox.Text;
}
// Handle other control types as needed
return null;
}
}
}
Join the conversation! Your thoughts help the community grow.