Results 1 to 19 of 19

Thread: Prpertygrid Color value save to Xml

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    10

    Prpertygrid Color value save to Xml

    Does anyone have a working example to write out 'Color' and 'Font' values from a 'Propertygrid' to an XML file.
    All other values are written out correctly. I have already tried several type converters, but none bring a solution.
    To remain compatible with already previously written away files, I should also be able to retrieve these values correctly:

    HTML Code:
    <?xml version="1.0" encoding="utf-8"?>
    <XmlModeG xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Randkleur Web="White" />
      <Lettertype>[Font: Name=Arial, Size=24, Units=3, GdiCharSet=0, GdiVerticalFont=False]</Lettertype>
    </XmlModeG>
    Last edited by dday9; Mar 4th, 2024 at 09:22 AM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: Prpertygrid Color value save to Xml

    The basic issue is that color is a complex type that cannot be serialized easily. You will need to use a class that contains the property that is serializable. Take a look at this example:
    Code:
    <Serializable>
    Public Class SerializableButton
        Inherits Control ' change me
    
        Public Property BackgroundColorString As String
            Get
                Return BackgroundColor.ToArgb().ToString()
            End Get
            Set(value As String)
                BackgroundColor = Color.FromArgb(CInt(value))
            End Set
        End Property
    End Class
    What this does is create a class that stores the BackgroundColor as a String by grabbing the current value, converting it to ARGB, and then converting the resulting Integer into a String. Then when the property value is set, it sets the BackgroundColor to the result of Color.FromArgb.

    This is a straightforward property because there is a straightforward way to convert colors to primitive types; fonts will be a little bit different. One thing that you can do is convert the various parts of a font to a CSV String literal and then convert them back. Just keep in mind that this approach is very fragile and if anyone touches the resulting XML it could corrupt the font serialization:
    Code:
    Public Property FontString As String
        Get
            Dim fontName = font.FontFamily.Name
            Dim fontSize = font.Size.ToString()
            Dim fontStyle = font.Style.ToString()
    
            Return String.Join(",", {fontName, fontSize, fontStyle})
        End Get
        Set(value As String)
            Dim fontParts = value.Split(","c)
            Dim fontName = fontParts(0)
            Dim fontSize = fontParts(1)
            Dim fontStyle = DirectCast([Enum].Parse(GetType(FontStyle), fontParts(2)), FontStyle)
    
            Font = New Font(fontName, fontSize, fontStyle)
        End Set
    End Property
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    10

    Re: Prpertygrid Color value save to Xml

    When I apply the color example, the color picker in the propertygrid disappeared.

    For choosing the Font, I have several errors:
    Name:  Font type.jpg
Views: 125
Size:  25.9 KB

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: Prpertygrid Color value save to Xml

    Can you provide me with the entire class definition?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    10

    Re: Prpertygrid Color value save to Xml

    Quote Originally Posted by dday9 View Post
    Can you provide me with the entire class definition?
    I sent you a PM with the full Class

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: Prpertygrid Color value save to Xml

    I didn't get a private message nor would I want one. If you want help then I encourage you to post your code here to allow the community to help you out.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    10

    Re: Prpertygrid Color value save to Xml

    The full Class is too large to post here, hence. I tried to include the 2 elements only in the example below.

    Code:
    Imports System.ComponentModel
    Imports System.Globalization
    Imports System.IO
    Imports System.Xml.Serialization
    
    
    <Serializable()>
    Public Class XmlModeT
      
      Dim _Lettertype As Font
      Dim _Kleur As Color
     
      <CategoryAttribute("Tekst"),
           DefaultValueAttribute(""),
           DescriptionAttribute("Lettertype en grootte")>
      Public Property Lettertype() As String
        Get
          Dim fontName = Font.FontFamily.Name
          Dim fontSize = Font.Size.ToString()
          Dim fontStyle = Font.Style.ToString()
    
         Return String.Join(",", {fontName, fontSize, fontStyle})
        End Get
        Set(ByVal Value As String)
          Dim fontParts = Value.Split(","c)
          Dim fontName = fontParts(0)
          Dim fontSize = fontParts(1)
          Dim fontStyle = DirectCast([Enum].Parse(GetType(FontStyle), fontParts(2)), FontStyle)
    
          Font = New Font(fontName, fontSize, fontStyle)
        End Set
      End Property
    
      <CategoryAttribute("Tekst"),
           Browsable(True),
           [ReadOnly](False),
           BindableAttribute(False),
           DefaultValueAttribute(""),
           DesignOnly(False),
           DescriptionAttribute("Kleur van de rand")>
      Public Property kleur() As String
        Get
          Return _Kleur.ToArgb().ToString
        End Get
        Set(ByVal Value As String)
          _Kleur = Color.FromArgb(CInt(Value))
        End Set
      End Property
    
      Public Shared Function Load(NaamBestand As String) As XmlModeT
        Dim serializer As XmlSerializer = New XmlSerializer(GetType(XmlModeT))
        Dim retVal As XmlModeT
        Dim reader As TextReader
        Dim fileNotFound As Boolean
    
        Try
          reader = New StreamReader(NaamBestand)
        Catch ex As FileNotFoundException
          ' Take the defaults
          fileNotFound = True
        End Try
    
        If fileNotFound Then
          retVal = New XmlModeT()
        Else
          'Read it from the file
          retVal = serializer.Deserialize(reader)
          reader.Close()
        End If
    
        Return retVal
      End Function
    
      Public Sub Save(NaamBestand As String)
        Dim serializer As New XmlSerializer(GetType(XmlModeT))
        Dim writer As TextWriter = New StreamWriter(NaamBestand)
        serializer.Serialize(writer, Me)
        writer.Close()
      End Sub

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: Prpertygrid Color value save to Xml

    The idea behind the two properties I gave you is that they act as wrappers to handle only the serialization. If you look my examples, the property setters are not setting the value of the property that the setter is tied to. For example, the setter for BackgroundColorString sets the value of BackgroundColor and the setter for FontString sets the value of Font.

    So you will need to have those underlying properties, which it appears as though you've removed.

    Try this instead:
    Code:
    Imports System.ComponentModel
    Imports System.Globalization
    Imports System.IO
    Imports System.Xml.Serialization
    
    
    <Serializable()>
    Public Class XmlModeT
    
        <NonSerialized()>
        Public Property Kleur As Color
    
        <Browsable(False)>
        Public Property KleurString As String
            Get
                Return Kleur.ToArgb().ToString()
            End Get
            Set(value As String)
                Kleur = Color.FromArgb(CInt(value))
            End Set
        End Property
    
        <NonSerialized()>
        Public Property Lettertype() As Font
    
        <Browsable(False)>
        Public Property LettertypeString As String
            Get
                Dim fontName = Lettertype.FontFamily.Name
                Dim fontSize = Lettertype.Size.ToString()
                Dim fontStyle = Lettertype.Style.ToString()
    
                Return String.Join(",", {fontName, fontSize, fontStyle})
            End Get
            Set(value As String)
                Dim fontParts = value.Split(","c)
                Dim fontName = fontParts(0)
                Dim fontSize = fontParts(1)
                Dim fontStyle = DirectCast([Enum].Parse(GetType(FontStyle), fontParts(2)), FontStyle)
    
                Lettertype = New Font(fontName, fontSize, fontStyle)
            End Set
        End Property
    
        ' save/load methods...
    
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    10

    Re: Prpertygrid Color value save to Xml

    I followed your example completely, but errors remain in the code, <NonSerialized()> mark them as errors (indicates that a field of a serializable class schould not be serialized. This class cannot inherited)


    Code:
    Imports System.ComponentModel
    Imports System.IO
    Imports System.Xml.Serialization
    
    <Serializable()>
    Public Class XmlModeT2
    
      <NonSerialized()>
      Public Property Kleur As Color
    
      <Browsable(False)>
      Public Property KleurString As String
        Get
          Return Kleur.ToArgb().ToString()
        End Get
        Set(value As String)
          Kleur = Color.FromArgb(CInt(value))
        End Set
      End Property
    
      <NonSerialized()>
      Public Property Lettertype() As Font
    
      <Browsable(False)>
      Public Property LettertypeString As String
        Get
          Dim fontName = Lettertype.FontFamily.Name
          Dim fontSize = Lettertype.Size.ToString()
          Dim fontStyle = Lettertype.Style.ToString()
    
          Return String.Join(",", {fontName, fontSize, fontStyle})
        End Get
        Set(value As String)
          Dim fontParts = value.Split(","c)
          Dim fontName = fontParts(0)
          Dim fontSize = fontParts(1)
          Dim fontStyle = DirectCast([Enum].Parse(GetType(FontStyle), fontParts(2)), FontStyle)
    
          Lettertype = New Font(fontName, fontSize, fontStyle)
        End Set
      End Property
    
      Public Shared Function Load(NaamBestand As String) As XmlModeT2
        Dim serializer As XmlSerializer = New XmlSerializer(GetType(XmlModeT2))
        Dim retVal As XmlModeT2
        Dim reader As TextReader = Nothing
        Dim fileNotFound As Boolean
    
        Try
          reader = New StreamReader(NaamBestand)
        Catch ex As FileNotFoundException
          ' Take the defaults
          fileNotFound = True
        End Try
    
        If fileNotFound Then
          retVal = New XmlModeT2()
        Else
          'Read it from the file
          retVal = serializer.Deserialize(reader)
          reader.Close()
        End If
    
        Return retVal
      End Function
    
      Public Sub Save(NaamBestand As String)
        Dim serializer As New XmlSerializer(GetType(XmlModeT))
        Dim writer As TextWriter = New StreamWriter(NaamBestand)
        serializer.Serialize(writer, Me)
        writer.Close()
      End Sub
    End Class

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: Prpertygrid Color value save to Xml

    I did free type this example and I used the wrong attribute. Use XmlIgnore (documentation) instead of NonSerialized (documentation).
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  11. #11

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    10

    Re: Prpertygrid Color value save to Xml

    With XmlIgnore added, there are 2 fewer errors. Now this error remains:
    Name:  fonterror.jpg
Views: 83
Size:  23.3 KB
    Attached Images Attached Images  

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: Prpertygrid Color value save to Xml

    The constructor is wrong because each variable is currently defined as a string. Take a look at the relevant documentation here: https://learn.microsoft.com/en-us/do...ing.font.-ctor

    You will need to convert fontName to a FontFamily and fontStyle to a FontStyle:
    Code:
    Lettertype = New Font(New FontFamily(fontName), fontSize, Dim val = DirectCast([Enum].Parse(GetType(FontStyle), fontStyle), FontStyle))
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  13. #13

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    10

    Re: Prpertygrid Color value save to Xml

    Your modified line now causes another error:
    Attachment 190672

  14. #14
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: Prpertygrid Color value save to Xml

    I’m not able to see the image. Can you please copy/paste the error message?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  15. #15

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    10

    Re: Prpertygrid Color value save to Xml

    Name:  fonterror2.jpg
Views: 67
Size:  16.0 KB

    Expression expected

  16. #16
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: Prpertygrid Color value save to Xml

    I will let you take a stab in the dark at how to resolve that one. It’s super easy.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  17. #17

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    10

    Re: Prpertygrid Color value save to Xml

    I'm just following your examples. If I adjust one error another error comes in its place.

    Name:  fonterror3.jpg
Views: 48
Size:  4.3 KB

    Initializes a new font using a specified size and style

  18. #18
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: Prpertygrid Color value save to Xml

    OK, I plugged it into Visual Studio and there were a few errors.

    1. Regarding the incorrect initialization of Font. The error message you are getting is:
    Overload resolution failed because no accessible 'New' can be called with these arguments...
    The issue with this is that we are currently passing it a FontFamily, a String, and a FontStyle. But if you look at the documentation I provided you you will see that the correct overload is FontFamily, Single, FontStyle. So all you need to do is convert the second argument from a String to a Single:
    Code:
    <Browsable(False)>
    Public Property LettertypeString As String
        Get
            Dim fontName = Lettertype.FontFamily.Name
            Dim fontSize = Lettertype.Size.ToString()
            Dim fontStyle = Lettertype.Style.ToString()
    
            Return String.Join(",", {fontName, fontSize, fontStyle})
        End Get
        Set(value As String)
            Dim fontParts = value.Split(","c)
            Dim fontName = fontParts(0)
            Dim fontNameAsFamily = New FontFamily(fontName)
            Dim fontSize = fontParts(1)
            Dim fontSizeAsSingle = Convert.ToSingle(fontSize)
            Dim fontStyle = DirectCast([Enum].Parse(GetType(FontStyle), fontParts(2)), FontStyle)
    
            Lettertype = New Font(fontNameAsFamily, fontSizeAsSingle, fontStyle)
        End Set
    End Property
    2. Regarding the error in the Load method. The error message you are getting is:
    Option Strict On disallows implicit conversions from 'Object' to 'XmlModeT2'.
    You will need to cast the deserialized value to a strongly typed object:
    Code:
    Public Shared Function Load(NaamBestand As String) As XmlModeT2
        Dim serializer = New XmlSerializer(GetType(XmlModeT2))
        Dim retVal As XmlModeT2
        Dim reader As TextReader = Nothing
        Dim fileNotFound As Boolean
    
        Try
            reader = New StreamReader(NaamBestand)
        Catch ex As FileNotFoundException
            ' Take the defaults
            fileNotFound = True
        End Try
    
        If fileNotFound Then
            retVal = New XmlModeT2()
        Else
            'Read it from the file
            retVal = DirectCast(serializer.Deserialize(reader), XmlModeT2)
            reader.Close()
        End If
        Return retVal
    End Function
    2. Regarding the error in the Save method. The error message you are getting is:
    Type 'XmlModeT' is not defined.
    This is a typo, simply correct the strongly type name to be XmlModeT2:
    Code:
    Public Sub Save(NaamBestand As String)
        Dim serializer = New XmlSerializer(GetType(XmlModeT2))
        Dim writer = New StreamWriter(NaamBestand)
        serializer.Serialize(writer, Me)
        writer.Close()
    End Sub
    No offence, but these are all simple things that you should be able to infer as a developer. By me providing copy/paste solutions for you, it essentially handicaps you from learning how to resolve these things on your own. My hope is that next time you get an answer that fails when you to copy/paste the code, you take the time to understand what is failing.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  19. #19

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    10

    Re: Prpertygrid Color value save to Xml

    I got the example working with this. Now I can start modifying the actual application.
    Thanks for helping.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width