Articles in this section
Category / Section

How to configure State Persistence for WinForms Radial Gauge control?

5 mins read

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. 

 

For example

 

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.

 

For example

 

Here, we have Serialized / Deserialized the “Value” and “VisualStyle” properties of RadialGauge control.

 

The following code demonstrates the same.

 

Code Example: [C#]

 

 
//codes in form1.cs
 
 
//Save RadialGauge state
 
SaveRadialGaugeProperties("GaugeFile");
 
//load
LoadRadialGaugeProperties("GaugeFile");
 
//For Save
 
public void SaveRadialGaugeProperties(string filename)
{
 
string path = @"\" + filename;
 
List<RadialGaugeAdvSettings> settingsList = new List<RadialGaugeAdvSettings>();
 
RadialGaugeAdvSettings s_settings = new RadialGaugeAdvSettings();
 
s_settings.Value = this.radialGauge1.Value;
 
s_settings.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);
 
}
}
 
//For load
public void LoadRadialGaugeProperties(string filename)
{
 
string path = @"\" + filename;
 
if (File.Exists(filename))
{
 
FileStream stream = new FileStream((Environment.CurrentDirectory + path), 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;
 
}
}
}
 
//Codes in RadialGaugeAdv.cs
 
[XmlRoot("Root")]
 
public class RadialGaugeAdvSettings
{
 
[XmlElement("Value")]
 
public float Value
{
 
get;
 
set;
 
}
[XmlElement("VisualStyle")]
 
public ThemeStyle VisualStyle
{
 
get;
 
set;
 
}
 
}
 

 

Code Example: [VB]

 

'Codes in Form1.vb
 
'Save RadialGauge state
 
SaveRadialGaugeProperties("GaugeFile")
 
'load
 
LoadRadialGaugeProperties("GaugeFile")
 
'For Save
 
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
 
'For load
 
Public Sub LoadRadialGaugeProperties(ByVal filename As String)
 
Dim path As String = "\" & filename
 
If File.Exists(filename) Then
 
Dim stream As New FileStream((Environment.CurrentDirectory & path), 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 settings
 
End If
 
End Sub
 
'Codes in RadialGaugeAdv.vb
 
<XmlRoot("Root")>
 
Public Class RadialGaugeAdvSettings
 
<XmlElement("Value")>
 
Public Property Value() As Single
 
<XmlElement("VisualStyle")>
 
Public Property VisualStyle() As ThemeStyle
 
End Class

 

Sample

 

C#:  RadialGaugeSerializationSample

 

VB:  RadialGaugeSerializationSample

 

 

 

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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied