How to configure State Persistence for WinForms Radial Gauge control?
The State Persistence support helps end users to save the application / controls customization based on their needs. So, it is a very essential feature and some controls may not have support to Serialize and Deserialize its customization settings by built-in. This article will provide guidance to configure State Persistence for those controls in sample application level.
Here we have considered WinForms RadialGauge control. At present RadialGauge doesn’t have built-in State Persistence support and its property information can be programmatically saved / load from XML by following below steps.
Step 1: Define Class that helps Serialize / De-Serialize RadialGauge property details and mark it as XMLRoot attribute.
Step 2: Implement required properties of RadialGauge that needs to be Serialized / De-Serialized in this class and mark it as XMLElement attribute.
Step 3: Save those properties using XMLTextWriter class, that helps to store information in XML file.
Step 4: Load those properties using XMLTextReader class, that helps to retrieve information from XML file.
Here, we have Serialized / Deserialized the Value and VisualStyle properties of RadialGauge control.
The following code demonstrates the same.
// Code in Form1.cs
// Save RadialGauge state
SaveRadialGaugeProperties("GaugeFile");
// Load RadialGauge state
LoadRadialGaugeProperties("GaugeFile");
// Save method
public void SaveRadialGaugeProperties(string filename)
{
string path = @"\" + filename;
List<RadialGaugeAdvSettings> settingsList = new List<RadialGaugeAdvSettings>();
RadialGaugeAdvSettings s_settings = new RadialGaugeAdvSettings
{
Value = this.radialGauge1.Value,
VisualStyle = this.radialGauge1.VisualStyle
};
settingsList.Add(s_settings);
using (XmlTextWriter writer =
new XmlTextWriter(Environment.CurrentDirectory + path, Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
XmlSerializer serializer =
new XmlSerializer(typeof(List<RadialGaugeAdvSettings>));
serializer.Serialize(writer, settingsList);
}
}
// Load method
public void LoadRadialGaugeProperties(string filename)
{
string path = @"\" + filename;
string fullPath = Environment.CurrentDirectory + path;
if (File.Exists(fullPath))
{
using (FileStream stream = new FileStream(fullPath, FileMode.Open))
{
XmlTextReader reader = new XmlTextReader(stream);
XmlSerializer serializer =
new XmlSerializer(typeof(List<RadialGaugeAdvSettings>));
List<RadialGaugeAdvSettings> savedSettings =
(List<RadialGaugeAdvSettings>)serializer.Deserialize(reader);
foreach (var settings in savedSettings)
{
this.radialGauge1.Value = settings.Value;
this.radialGauge1.VisualStyle = settings.VisualStyle;
}
}
}
}
// Code in RadialGaugeAdv.cs
[XmlRoot("Root")]
public class RadialGaugeAdvSettings
{
[XmlElement("Value")]
public float Value { get; set; }
[XmlElement("VisualStyle")]
public ThemeStyle VisualStyle { get; set; }
}' Code in Form1.vb
' Save RadialGauge state
SaveRadialGaugeProperties("GaugeFile")
' Load RadialGauge state
LoadRadialGaugeProperties("GaugeFile")
' Save method
Public Sub SaveRadialGaugeProperties(ByVal filename As String)
Dim path As String = "\" & filename
Dim settingsList As New List(Of RadialGaugeAdvSettings)()
Dim s_settings As New RadialGaugeAdvSettings()
s_settings.Value = Me.radialGauge1.Value
s_settings.VisualStyle = Me.radialGauge1.VisualStyle
settingsList.Add(s_settings)
Using writer As New XmlTextWriter(Environment.CurrentDirectory & path, Encoding.UTF8)
writer.Formatting = Formatting.Indented
Dim serializer As New XmlSerializer(GetType(List(Of RadialGaugeAdvSettings)))
serializer.Serialize(writer, settingsList)
End Using
End Sub
' Load method
Public Sub LoadRadialGaugeProperties(ByVal filename As String)
Dim path As String = "\" & filename
Dim fullPath As String = Environment.CurrentDirectory & path
If File.Exists(fullPath) Then
Using stream As New FileStream(fullPath, FileMode.Open)
Dim reader As New XmlTextReader(stream)
Dim serializer As New XmlSerializer(GetType(List(Of RadialGaugeAdvSettings)))
Dim savedSettings As List(Of RadialGaugeAdvSettings) =
DirectCast(serializer.Deserialize(reader), List(Of RadialGaugeAdvSettings))
For Each settings In savedSettings
Me.radialGauge1.Value = settings.Value
Me.radialGauge1.VisualStyle = settings.VisualStyle
Next
End Using
End If
End Sub
' Code in RadialGaugeAdv.vb
<XmlRoot("Root")>
Public Class RadialGaugeAdvSettings
<XmlElement("Value")>
Public Property Value As Single
<XmlElement("VisualStyle")>
Public Property VisualStyle As ThemeStyle
End ClassOutput:
Conclusion
I hope you enjoyed learning about how to configure State Persistence for WinForms Radial Gauge control.
You can refer to our WinForms Radial Gauge feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms Radial Gauge examples to understand how to present and manipulate data.
For current customers, you can check out our WPF Controls from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WPF Charts and other WPF controls.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!