Page 1 of 2 12 LastLast
Results 1 to 40 of 45

Thread: Read and write color to xml file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Read and write color to xml file

    Hi All,

    I have an xml file for creating buttons so I can call them back to my form. Is there a parameter to allow for color? I cannot seem to find one.

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <buttoncollection>
      <button text="L100" width="55" height="73" x="246" y="79" />
      <button text="U500" width="39" height="28" x="337" y="361" />
      <button text="T101" width="110" height="93" x="430" y="169" />
      <button text="R520" width="1" height="1" x="432" y="170" />
      <button text="T100" width="108" height="90" x="432" y="76" />
    </buttoncollection>

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Read and write color to xml file

    FWIW - gets all properties of all buttons on a form not in a container.

    Code:
            Dim buttons As XElement = <buttons>
                                      </buttons>
            Dim buttonProto As XElement = <button></button>
            For Each btn As Button In Me.Controls.OfType(Of Button)().OrderBy(Function(cb) cb.Name)
                Dim b As New XElement(buttonProto)
                b.Add(<name><%= btn.Name %></name>)
                buttons.Add(b)
                Dim Btyp As Type = btn.GetType()
                Dim props As List(Of PropertyInfo) = Btyp.GetProperties().OrderBy(Function(pi) pi.Name).ToList
                For Each prop As PropertyInfo In props
                    Dim PropNM As String = prop.Name
                    Dim pInfo As System.Reflection.PropertyInfo = Btyp.GetProperty(PropNM)
                    Dim PropValue As Object = pInfo.GetValue(btn, Reflection.BindingFlags.GetProperty, Nothing, Nothing, Nothing)
                    If PropValue IsNot Nothing Then
                        Dim propXE As XElement
                        ' Dim vtyp As Integer = Type.GetTypeCode(PropValue.GetType())
                        Dim vtyp As String = PropValue.GetType().UnderlyingSystemType.FullName
                        propXE = <prop>
                                     <name><%= PropNM %></name>
                                     <val><%= PropValue %></val>
                                     <typ><%= vtyp %></typ>
                                 </prop>
                        b.Add(propXE)
                    End If
                Next
            Next
    Last edited by dbasnett; Dec 2nd, 2021 at 11:35 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    Thanks dbasnett... wow, that is a lot of information that gets returned. Since I am creating dynamic buttons at runtime, I am not certain how I could use this to set different colors for these buttons.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Read and write color to xml file

    Quote Originally Posted by dbasnett View Post
    FWIW - gets all properties of all buttons on a form not in a container.

    Code:
            Dim buttons As XElement = <buttons>
                                      </buttons>
            Dim buttonProto As XElement = <button></button>
            For Each btn As Button In Me.Controls.OfType(Of Button)().OrderBy(Function(cb) cb.Name)
                Dim b As New XElement(buttonProto)
                b.Add(<name><%= btn.Name %></name>)
                buttons.Add(b)
                Dim Btyp As Type = btn.GetType()
                Dim props As List(Of PropertyInfo) = Btyp.GetProperties().OrderBy(Function(pi) pi.Name).ToList
                For Each prop As PropertyInfo In props
                    Dim PropNM As String = prop.Name
                    Dim pInfo As System.Reflection.PropertyInfo = Btyp.GetProperty(PropNM)
                    Dim PropValue As Object = pInfo.GetValue(btn, Reflection.BindingFlags.GetProperty, Nothing, Nothing, Nothing)
                    If PropValue IsNot Nothing Then
                        Dim propXE As XElement
                        ' Dim vtyp As Integer = Type.GetTypeCode(PropValue.GetType())
                        Dim vtyp As String = PropValue.GetType().UnderlyingSystemType.FullName
                        propXE = <prop>
                                     <name><%= PropNM %></name>
                                     <val><%= PropValue %></val>
                                     <typ><%= vtyp %></typ>
                                 </prop>
                        b.Add(propXE)
                    End If
                Next
            Next
    You could actually make this a lot neater and more readable:-
    Code:
            Dim xml = <Buttons>
                          <%= From b In Me.Controls.OfType(Of Button)() Order By b.Name
                              Select <Button>
                                         <Name><%= b.Name %></Name>
    
                                         <%= From prop In b.GetType.GetProperties() Order By prop.Name
                                             Let propVal = prop.GetValue(b)
                                             Where propVal IsNot Nothing
                                             Select <Properties>
                                                        <Name><%= prop.Name %></Name>
                                                        <Value><%= propVal.ToString %></Value>
                                                        <Type><%= prop.PropertyType.UnderlyingSystemType.FullName %></Type>
                                                    </Properties> %>
    
    
    
                                     </Button>
                          %>
                      </Buttons>
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Read and write color to xml file

    Quote Originally Posted by mikeg71 View Post
    Thanks dbasnett... wow, that is a lot of information that gets returned. Since I am creating dynamic buttons at runtime, I am not certain how I could use this to set different colors for these buttons.
    Do you want to know how to use XML data like that to generate Buttons at runtime?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    Thanks for the reply Niya... I am looking for a way to change the button color when they are created at runtime from my xml file. But it seems everything I have searched does not show a color parameter in an xml file. My xml file contents are above, this is all I have for creating my buttons right now.
    Last edited by mikeg71; Dec 7th, 2021 at 02:43 PM.

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Read and write color to xml file

    So add one... it's just a value. There's nothing magical about colors.
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <buttoncollection>
      <button text="L100" width="55" height="73" x="246" y="79" color="red" />
      <button text="U500" width="39" height="28" x="337" y="361" color="9c9c9c" />
      <button text="T101" width="110" height="93" x="430" y="169" color="0000ff" />
      <button text="R520" width="1" height="1" x="432" y="170" />
      <button text="T100" width="108" height="90" x="432" y="76" />
    </buttoncollection>
    You can (or should be able to) use named colors, or use RGB values, maybe even both with a little juggling.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    Thanks techgnome, here is what I have right now when calling buttons from my xml file. Please ignore the reference to "labels" as I am doing buttons. I have tried a couple of things here to add color, but have had no success.

    Code:
    Private Sub LoadButtons(ByVal container As Form1, ByVal filename As String)
            filename = "C:\Temp\Buttons.xml"
            Dim xmlfile As New XmlDocument
            xmlfile.Load(filename)
    
            Dim labelsroot As XmlElement = DirectCast(xmlfile.SelectSingleNode("buttoncollection"), XmlElement)
    
            For Each labelnode As XmlElement In labelsroot.ChildNodes.OfType(Of XmlElement)()
                Dim location As New Point(CInt(labelnode.Attributes("x").Value), CInt(labelnode.Attributes("y").Value))
                Dim size As New Size(CInt(labelnode.Attributes("width").Value), CInt(labelnode.Attributes("height").Value))
    
                Dim lbl As New Label With {
                    .Text = labelnode.Attributes("text").Value,
                    .ForeColor = Color.White,
                    .BackColor = Color.FromArgb(50, 127, 120, 127),     '<<<<< this is what I want to get from the xml file
                    .Location = location,
                    .TextAlign = ContentAlignment.MiddleCenter,
                    .Size = size,
                    .Font = New Font("Agency FB", 8, FontStyle.Regular)}
                container.Controls.Add(lbl)
                lbl.BringToFront()
            Next
        End Sub

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Read and write color to xml file

    Use a different overload of the FromARGB method - one that only needs a single parameter. You can use Color.ToArgb (or use a hex to decimal converter) to get the single integer value you can then put into your xml file.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    Hmmm I wish I could say I totally understand that. Do you have an example?

  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Read and write color to xml file

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <buttoncollection>
      <button text="L100" width="55" height="73" x="246" y="79" color="255" /> 
      <button text="U500" width="39" height="28" x="337" y="361" color="10263708" />
      <button text="T101" width="110" height="93" x="430" y="169" color="255" />
      <button text="R520" width="1" height="1" x="432" y="170" color="65280 />
      <button text="T100" width="108" height="90" x="432" y="76" color="10263708 />
    </buttoncollection>
    255 = ff = red
    10263708 = 9c9c9c = grey
    65280 = ff00 = green

    Code:
    Private Sub LoadButtons(ByVal container As Form1, ByVal filename As String)
            filename = "C:\Temp\Buttons.xml"
            Dim xmlfile As New XmlDocument
            xmlfile.Load(filename)
    
            Dim labelsroot As XmlElement = DirectCast(xmlfile.SelectSingleNode("buttoncollection"), XmlElement)
    
            For Each labelnode As XmlElement In labelsroot.ChildNodes.OfType(Of XmlElement)()
                Dim location As New Point(CInt(labelnode.Attributes("x").Value), CInt(labelnode.Attributes("y").Value))
                Dim size As New Size(CInt(labelnode.Attributes("width").Value), CInt(labelnode.Attributes("height").Value))
                Dim newColor as Color = Color.FromArgb(CInt(labelNode.Attributes("color").value)) ' << get the value from the xml and pass it to the FromArgb method....
    
                Dim lbl As New Label With {
                    .Text = labelnode.Attributes("text").Value,
                    .ForeColor = Color.White,
                    .BackColor = newColor,     '<<use the color here
                    .Location = location,
                    .TextAlign = ContentAlignment.MiddleCenter,
                    .Size = size,
                    .Font = New Font("Agency FB", 8, FontStyle.Regular)}
                container.Controls.Add(lbl)
                lbl.BringToFront()
            Next
        End Sub
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Read and write color to xml file

    Addendum, - although, I'm not sure Cint handles ints of that size ... you may be better off using Integer.TryParse or Integer.Parse ... but that's just me.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    Thanks techgnome, here is what I have right now. The color does not seem to be coming in.

    Code:
        Private Sub LoadButtons(ByVal container As Form1, ByVal filename As String)
    
            filename = "C:\Temp\MyButtons.xml"
            Dim xmlfile As New XmlDocument
            xmlfile.Load(filename)
    
            Dim buttonsroot As XmlElement = DirectCast(xmlfile.SelectSingleNode("buttoncollection"), XmlElement)
    
            For Each buttonnode As XmlElement In buttonsroot.ChildNodes.OfType(Of XmlElement)()
                Dim location As New Point(CInt(buttonnode.Attributes("x").Value), CInt(buttonnode.Attributes("y").Value))
                Dim size As New Size(CInt(buttonnode.Attributes("width").Value), CInt(buttonnode.Attributes("height").Value))
                Dim newColor As Color = Color.FromArgb(Integer.Parse(buttonnode.Attributes("color").Value)) ' << get the value from the xml and pass it to the FromArgb method....
    
                Dim btn As New Button With {
                    .Text = buttonnode.Attributes("text").Value,
                    .ForeColor = Color.White,
                    .BackColor = newColor,
                    .Location = location,
                    .TextAlign = ContentAlignment.MiddleCenter,
                    .Size = size,
                    .Font = New Font("Agency FB", 8, FontStyle.Regular)}
                container.Controls.Add(btn)
                btn.BringToFront()
            Next
        End Sub

  14. #14
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Read and write color to xml file

    Post the contents of the XML file.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    I am using the one that techgnome posted with the updated colors. Just added the missing quotes. I get no errors from it, but no colors seem to be changing.

  16. #16
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Read and write color to xml file

    The problem with that code are the actual integers representing the colours. I don't know how those values were derived but all of them have their alpha values set to 0 which means the colours are completely transparent. The solution to this is simple however, you just set the most significant byte of the Integer to 255 so when Color.FromArgb converts it, it has an alpha value of 255 which means the colour is completely opaque:-
    Code:
            Dim doc As XDocument = XDocument.Load("d:\buttons.txt")
    
            For Each xb In doc.Root.Elements("button")
    
                Me.Controls.Add(New Button With {
                    .Text = xb.Attribute("text").Value,
                    .Width = CInt(xb.Attribute("width").Value),
                    .Height = CInt(xb.Attribute("height").Value),
                    .Left = CInt(xb.Attribute("x").Value),
                    .Top = CInt(xb.Attribute("y").Value),
                    .BackColor = Color.FromArgb(CInt(xb.Attribute("color").Value) Or &HFF000000I) 'Set alpha to 255
                })
    
            Next
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    Ahh yes...thank you, Niya. This seems to be working well now. If I wanted to have a button color semi-transparent, how can I do that using this current method?

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

    Re: Read and write color to xml file

    I'm not sure how you're creating the XML to begin with, but you can use a PropertyGrid control (documentation), bind it to a Button, customize the Button at runtime, and then serialize the control.

    Once the control is serialized, when you can simply deserialize it to create a new instance.

    That away you don't have to worry about properly setting property values.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  19. #19
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Read and write color to xml file

    Quote Originally Posted by mikeg71 View Post
    Ahh yes...thank you, Niya. This seems to be working well now. If I wanted to have a button color semi-transparent, how can I do that using this current method?
    Then you must use a non zero alpha value. The closer to 0 it is, the more transparent, the closer to 255, the more opague:-
    Code:
            Dim doc As XDocument = XDocument.Load("d:\buttons.txt")
            Dim alpha As Byte = 150 'Use alpha of 150 which is semi-transparent
    
            For Each xb In doc.Root.Elements("button")
    
                Me.Controls.Add(New Button With {
                    .Text = xb.Attribute("text").Value,
                    .Width = CInt(xb.Attribute("width").Value),
                    .Height = CInt(xb.Attribute("height").Value),
                    .Left = CInt(xb.Attribute("x").Value),
                    .Top = CInt(xb.Attribute("y").Value),
                    .BackColor = Color.FromArgb(CInt(xb.Attribute("color").Value) Or (CInt(alpha) << 24)) 'Set alpha of colour
                })
    
            Next
    Last edited by Niya; Dec 8th, 2021 at 10:35 AM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  20. #20
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Read and write color to xml file

    Quote Originally Posted by dday9 View Post
    I'm not sure how you're creating the XML to begin with, but you can use a PropertyGrid control (documentation), bind it to a Button, customize the Button at runtime, and then serialize the control.

    Once the control is serialized, when you can simply deserialize it to create a new instance.

    That away you don't have to worry about properly setting property values.
    I get the impression he is actually doing some kind of pseudo-WPF XAML thing where he is allowing his interfaces to be customizable through the use of XML files.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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

    Re: Read and write color to xml file

    I've done something similar in a separate project where I had a "settings" form. In the form I had a UI Customization tab that allowed the user to customize certain properties of a subset of controls.

    In the UI Customization tab, when the user would click save, it would serialize the properties and store the value in My.Settings. Then when the user would restart the application it would redraw the controls using their customized property values.

    I don't remember why the client needed this, but I remember thinking that it was a fun little feature. But you're right, if the OP really wants a pseudo XAML thing going on where anyone can customize it with a little bit of XML knowledge, then my suggestion is moot.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    dday9 and Niya... thanks for the help on this, very much appreciated. Here is how I am saving the buttons. To give a little insight on what I am doing... I am creating buttons of different sizes using the drag method and assigning the text. What might become of this is, the GUI is going to be used for installing parts. Each user will have their own color, when finished installing they will click the part and I will record some information along with that.

    Code:
    Private Sub SaveLabels(ByVal container As Form1, ByVal filename As String)
    
            filename = "C:\Temp\MyButtons.xml"
    
            Dim xmlfile As New Xml.XmlDocument
            Dim decl As XmlDeclaration = xmlfile.CreateXmlDeclaration("1.0", "UTF-8", "yes")
    
            xmlfile.AppendChild(decl)
            Dim labelsroot As XmlElement = xmlfile.CreateElement("buttoncollection")
            xmlfile.AppendChild(labelsroot)
    
            For Each lbl As Label In container.Controls.OfType(Of Label)()
                Dim labelnode As XmlElement = xmlfile.CreateElement("button")
                Dim textattr As XmlAttribute = xmlfile.CreateAttribute("text")
                Dim brderattr As XmlAttribute = xmlfile.CreateAttribute("border")
                Dim widthattr As XmlAttribute = xmlfile.CreateAttribute("width")
                Dim heightattr As XmlAttribute = xmlfile.CreateAttribute("height")
                Dim xpos As XmlAttribute = xmlfile.CreateAttribute("x")
                Dim ypos As XmlAttribute = xmlfile.CreateAttribute("y")
                Dim kolor As XmlAttribute = xmlfile.CreateAttribute("color")
    
    
                textattr.Value = lbl.Text
                widthattr.Value = lbl.Width.ToString()
                heightattr.Value = lbl.Height.ToString()
                xpos.Value = lbl.Location.X.ToString()
                ypos.Value = lbl.Location.Y.ToString()
                kolor.Value = InputBox("Enter color value: ")
    
    
                With labelnode.Attributes
                    .Append(textattr)
                    .Append(widthattr)
                    .Append(heightattr)
                    .Append(xpos)
                    .Append(ypos)
                    .Append(kolor)
                End With
    
                labelsroot.AppendChild(labelnode)
            Next
    
            xmlfile.Save(filename)
        End Sub

  23. #23
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Read and write color to xml file

    I haven't went back and read all the discussion so this may be irrelevant.

    But have you consider how your going to retrieve existing user data and edit existing data. You might want to consider putting xml data into a datatable. It's easy, someDatatable.ReadXML and someDatatable.WriteXML.

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    wes4dbt... thank you for popping in on this. No doubt I am open to all suggestions as I am in the early stages of this. using xml is quite new to me, so plenty to learn here. Is using a datatable much different from using the xml file format below? Thanks

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <buttoncollection>
      <button text="L100" width="55" height="73" x="246" y="79" color="255" /> 
      <button text="U500" width="39" height="28" x="337" y="361" color="10263708" />
      <button text="T101" width="110" height="93" x="430" y="169" color="255" />
      <button text="R520" width="1" height="1" x="432" y="170" color="65280 />
      <button text="T100" width="108" height="90" x="432" y="76" color="10263708 />
    </buttoncollection>

  25. #25
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Read and write color to xml file

    Once the xml file is in the datatable then you can use the datatable just like any other datatable. All the data could easily be display/edited in a DataGridview if you want. I don't know what your experience is with datatables. But the xml file would have to be formatted differently and would also need to include the datatable schema.

  26. #26
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Read and write color to xml file

    Given the nature of this, I don't think it's necessary to involve the use of Datatables.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  27. #27

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    I suppose I am a little bit stuck right now trying to assign a color to each button that I am creating. When I use the drag method to create my button, I am then using the ColorDialog to choose the color, but then when I use the for loop above, all of the created buttons in my xml file color attribute get overwritten by my last chosen color. So I am curious if I am using the correct method for saving my buttons to xml.

  28. #28
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Read and write color to xml file

    Quote Originally Posted by mikeg71 View Post
    I suppose I am a little bit stuck right now trying to assign a color to each button that I am creating. When I use the drag method to create my button, I am then using the ColorDialog to choose the color, but then when I use the for loop above, all of the created buttons in my xml file color attribute get overwritten by my last chosen color. So I am curious if I am using the correct method for saving my buttons to xml.
    Is this a different problem?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  29. #29

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    Not really different, it still has to with reading/writing the color. Here is the code I have right now for saving to the xml. I am not understanding how nothing else in my xml gets overwritten except for color. I know I am missing some key element here.

    XML File:

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <buttoncollection>
      <button text="U300" width="38" height="31" x="460" y="357" color="-32704" />
      <button text="L1" width="60" height="87" x="247" y="407" color="-32704" />
      <button text="L100" width="53" height="72" x="244" y="79" color="-32704" />
      <button text="T100" width="69" height="80" x="450" y="81" color="-32704" />
      <button text="L300" width="51" height="40" x="462" y="463" color="-32704" />
      <button text="L301" width="50" height="41" x="463" y="533" color="-32704" />
    </buttoncollection>
    Save to XML:
    Code:
    Private Sub SaveLabels(ByVal container As Form1, ByVal filename As String)
    
            filename = "C:\Temp\MyButtons.xml"
    
            Dim xmlfile As New Xml.XmlDocument
            Dim decl As XmlDeclaration = xmlfile.CreateXmlDeclaration("1.0", "UTF-8", "yes")
    
            xmlfile.AppendChild(decl)
            Dim labelsroot As XmlElement = xmlfile.CreateElement("buttoncollection")
            xmlfile.AppendChild(labelsroot)
    
    
            For Each lbl As Button In container.Controls.OfType(Of Button)()
                Dim labelnode As XmlElement = xmlfile.CreateElement("button")
                Dim textattr As XmlAttribute = xmlfile.CreateAttribute("text")
                Dim brderattr As XmlAttribute = xmlfile.CreateAttribute("border")
                Dim widthattr As XmlAttribute = xmlfile.CreateAttribute("width")
                Dim heightattr As XmlAttribute = xmlfile.CreateAttribute("height")
                Dim xpos As XmlAttribute = xmlfile.CreateAttribute("x")
                Dim ypos As XmlAttribute = xmlfile.CreateAttribute("y")
                Dim kolor As XmlAttribute = xmlfile.CreateAttribute("color")  '<<<<<<<<<<<<<<<<<
    
                textattr.Value = lbl.Text
                widthattr.Value = lbl.Width.ToString()
                heightattr.Value = lbl.Height.ToString()
                xpos.Value = lbl.Location.X.ToString()
                ypos.Value = lbl.Location.Y.ToString()
                kolor.Value = newBtnColor.ToArgb   '<<<<<<<<<<<<<<<<<< New Color
    
                With labelnode.Attributes
                    .Append(textattr)
                    .Append(widthattr)
                    .Append(heightattr)
                    .Append(xpos)
                    .Append(ypos)
                    .Append(kolor) '<<<<<<<<<<<<<<<<<<<<<<<<<
                End With
    
                labelsroot.AppendChild(labelnode)
            Next
    
            xmlfile.Save(filename)
        End Sub
    Create the button:

    Code:
        Private Sub CreateButton(ByVal ptA As Point, ByVal ptB As Point)
    
            Dim cDialog As New ColorDialog()
    
            If CheckBox1.CheckState = CheckState.Checked Then
                'don't allow creating new buttons
            Else
                Dim btn As New Button
    
                If (cDialog.ShowDialog() = DialogResult.OK) Then
                    btn.BackColor = cDialog.Color
                End If
    
                newBtnColor = cDialog.Color
    
    
                btn.Bounds = RectangleFromPoints(ptA, ptB)
                Me.Controls.Add(btn)
                btn.BringToFront()
                myButtonName = InputBox("Reference Designator...")
                btn.Name = myButtonName
                btn.Text = myButtonName
                btn.Font = New Font("Agency FB", 12, FontStyle.Regular)
            End If
    
        End Sub
    In my Form Class:
    Code:
    Public Class Form1
        Dim newBtnColor As Color

  30. #30
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Read and write color to xml file

    Sigh... so you created a form-level variable... and set it when you create a button.... and use that same variable when saving.... and then wonder why all your buttons are the same color as the last button you created when reading it back...
    1) don't use a form-level variable for the color. There is ZERO reason to do so. It should be confined to the sub where you use it - when creating the button. No more, no less.
    2) When saving the button information, save the BUTTON INFORMATION, not some extraneous information from another variable that's been declared at the form level -- unless you want that same value to APPLY TO ALL BUTTONS...

    see the problem now?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  31. #31

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    Ok, I am seeing some of this now. I removed the form level variable, but then when choosing the color at "CreateButton", how do I capture the color I picked and apply it to "kolor.value" for my xml file save?

  32. #32
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Read and write color to xml file

    you already are.... it's in your button:
    Code:
                If (cDialog.ShowDialog() = DialogResult.OK) Then
                    btn.BackColor = cDialog.Color
                End If
    In your save, you're already pulling all of the other data points from the button... sooo......... pull the .BackColor as well.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  33. #33

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    OMG! I can't even blame lack of coffee here...

    Code:
    kolor.Value = lbl.BackColor.ToArgb
    Thanks techgnome.... I think I need to call it a day

  34. #34

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    Hi Niya... I am using your two added lines to try and create my buttons with semi-transparent color from my xml file. It seems to ignore the "or (CInt(alpha) << 24))". I also don't understand how this works.

    Code:
            Dim doc As XDocument = XDocument.Load("d:\buttons.txt")
            Dim alpha As Byte = 150 'Use alpha of 150 which is semi-transparent
    
            For Each xb In doc.Root.Elements("button")
    
                Me.Controls.Add(New Button With {
                    .Text = xb.Attribute("text").Value,
                    .Width = CInt(xb.Attribute("width").Value),
                    .Height = CInt(xb.Attribute("height").Value),
                    .Left = CInt(xb.Attribute("x").Value),
                    .Top = CInt(xb.Attribute("y").Value),
                    .BackColor = Color.FromArgb(CInt(xb.Attribute("color").Value) Or (CInt(alpha) << 24)) 'Set alpha of colour
                })
    
            Next

  35. #35
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Read and write color to xml file

    Quote Originally Posted by mikeg71 View Post
    It seems to ignore the "or (CInt(alpha) << 24))"
    What makes you think this is being ignored? I tested it before I posted it and it worked as expected. What happened to make you think it's not working?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  36. #36

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    When I enter those values, my button back color does not change. The opacity stays 100%

    This is what I have right now:

    Code:
            Dim buttonsroot As XmlElement = DirectCast(xmlfile.SelectSingleNode("buttoncollection"), XmlElement)
            Dim alpha As Byte = 150 'Use alpha of 150 which is semi-transparent
    
            For Each buttonnode As XmlElement In buttonsroot.ChildNodes.OfType(Of XmlElement)()
                Dim location As New Point(CInt(buttonnode.Attributes("x").Value), CInt(buttonnode.Attributes("y").Value))
                Dim size As New Size(CInt(buttonnode.Attributes("width").Value), CInt(buttonnode.Attributes("height").Value))
    
                Dim btn As New Button With {
                    .Text = buttonnode.Attributes("text").Value,
                    .ForeColor = Color.Black,
                    .BackColor = Color.FromArgb(CInt(buttonnode.Attributes("color").Value) Or (CInt(alpha) << 24)),               'Or &HFF000000I),
                    .Location = location,
                    .TextAlign = ContentAlignment.MiddleCenter,
                    .Size = size,
                    .Font = New Font("Agency FB", 12, FontStyle.Regular)}
    
                container.Controls.Add(btn)
    Last edited by mikeg71; Dec 15th, 2021 at 11:55 AM.

  37. #37
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Read and write color to xml file

    Did you try changing the alpha value to observe it's effect?

    For example you could change this:-
    Code:
    Dim alpha As Byte = 150
    To this:-
    Code:
    Dim alpha As Byte = 50
    Then take notice of the difference.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  38. #38

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    Yes I did, I was changing the numbers randomly to try and get something to change, but no luck. Since I have no idea... what does the 150 represent compared to the "<< 24" in the code? Seems like whatever I try, I get no transparency.

    **UPDATE** So, if I use 150 and in the code I use "<<244", the color, which is light blue, turns to sort of a light gray color. But still no transparency.
    Last edited by mikeg71; Dec 15th, 2021 at 01:26 PM.

  39. #39
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Read and write color to xml file

    So you didn't see the colours changing?

    With an alpha of 90 you get this:-


    With an alpha of 150 you get this:-


    And with an alpha of 255, you get this:-


    Are you not getting these results?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  40. #40

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    178

    Re: Read and write color to xml file

    Ok, now I am seeing some color changes. Is there not a way to make a button transparent in the way you can make a label transparent? If I use my code and create a label instead button, I can get what I am looking for.

    Code:
    .BackColor = Color.FromArgb(140, 51, 153, 102)

Page 1 of 2 12 LastLast

Tags for this Thread

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