(De)serialization of controls with all properties

87 views Asked by At

I'm trying to serialize all the attributes of a control, for starters I started with the TextEdit control of Devexpress. However, the same problem occurs with the TextBox control. Serialization passes only for properties that do not have subordinate items or are not enumerators, that is, they can return a string by (deserialization). The question is, has anyone dealt with this and is there an easier way than writing individual rules for all control attributes. I started writing, but it's too difficult, even when I take into account the different controls (from different manufacturers), it's really too difficult a job.


   Public Class ControlProperties
       Public Property Text As String
       Public Property BackColor As String
       Public Property BorderStyle As Integer
       Public Property Size As Size
       Public Property Location As Point
       Public Property ForeColor As String
       Public Property Multiline As Boolean
       Public Property AllowDrop As Boolean
       Public Property Cursor As String
       Public Property RightToLeft As RightToLeft
       Public Property Font As SerializableFont
   End Class

   Public Class SerializableFont
       Public Property Name As String
       Public Property Size As Single
       Public Property Style As FontStyle

       Public Sub New()
       End Sub

       Public Sub New(font As Font)
           Name = font.Name
           Size = font.Size
           Style = font.Style
       End Sub

       Public Function ToFont() As Font
           Return New Font(Name, Size, Style)
       End Function
   End Class

   Public Sub SerializeControlProperties(control As Object, filePath As String)
       Dim properties As New ControlProperties()
       properties.Text = control.Text
       properties.BackColor = ColorTranslator.ToHtml(control.BackColor)
       properties.BorderStyle = control.BorderStyle
       properties.Size = control.Size
       properties.Location = control.Location
       properties.ForeColor = ColorTranslator.ToHtml(control.ForeColor)
       properties.AllowDrop = control.AllowDrop
       properties.Cursor = CursorToString(control.Cursor)
       properties.RightToLeft = control.RightToLeft
       properties.Font = New SerializableFont(control.Font)

       Dim serializer As New XmlSerializer(GetType(ControlProperties))

       Using writer As TextWriter = New StreamWriter(filePath)
           serializer.Serialize(writer, properties)
       End Using
   End Sub
<?xml version="1.0" encoding="utf-8"?>
<ControlProperties xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Text />
  <BackColor>#FFFFFF</BackColor>
  <BorderStyle>7</BorderStyle>
  <Size>
    <Width>100</Width>
    <Height>20</Height>
  </Size>
  <Location>
    <X>221</X>
    <Y>26</Y>
  </Location>
  <ForeColor>#282828</ForeColor>
  <Multiline>false</Multiline>
  <AllowDrop>false</AllowDrop>
  <Cursor>System.Windows.Forms.Cursor</Cursor>
  <RightToLeft>No</RightToLeft>
  <Font>
    <Name>Tahoma</Name>
    <Size>8.25</Size>
    <Style>Regular</Style>
  </Font>
</ControlProperties>
0

There are 0 answers