-
Dec 2nd, 2021, 09:49 AM
#1
Thread Starter
Addicted Member
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>
-
Dec 2nd, 2021, 11:32 AM
#2
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.
-
Dec 2nd, 2021, 12:04 PM
#3
Thread Starter
Addicted Member
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.
-
Dec 7th, 2021, 07:10 AM
#4
Re: Read and write color to xml file
Originally Posted by dbasnett
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>
-
Dec 7th, 2021, 07:23 AM
#5
Re: Read and write color to xml file
Originally Posted by mikeg71
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?
-
Dec 7th, 2021, 02:39 PM
#6
Thread Starter
Addicted Member
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.
-
Dec 7th, 2021, 02:50 PM
#7
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
-
Dec 7th, 2021, 03:01 PM
#8
Thread Starter
Addicted Member
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
-
Dec 7th, 2021, 03:27 PM
#9
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
-
Dec 7th, 2021, 03:41 PM
#10
Thread Starter
Addicted Member
Re: Read and write color to xml file
Hmmm I wish I could say I totally understand that. Do you have an example?
-
Dec 7th, 2021, 04:13 PM
#11
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
-
Dec 7th, 2021, 04:14 PM
#12
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
-
Dec 7th, 2021, 05:38 PM
#13
Thread Starter
Addicted Member
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
-
Dec 7th, 2021, 06:19 PM
#14
Re: Read and write color to xml file
Post the contents of the XML file.
-
Dec 7th, 2021, 07:22 PM
#15
Thread Starter
Addicted Member
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.
-
Dec 7th, 2021, 07:58 PM
#16
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
-
Dec 8th, 2021, 09:58 AM
#17
Thread Starter
Addicted Member
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?
-
Dec 8th, 2021, 10:22 AM
#18
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.
-
Dec 8th, 2021, 10:31 AM
#19
Re: Read and write color to xml file
Originally Posted by mikeg71
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.
-
Dec 8th, 2021, 10:33 AM
#20
Re: Read and write color to xml file
Originally Posted by dday9
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.
-
Dec 8th, 2021, 11:26 AM
#21
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.
-
Dec 8th, 2021, 12:59 PM
#22
Thread Starter
Addicted Member
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
-
Dec 8th, 2021, 01:25 PM
#23
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.
-
Dec 8th, 2021, 01:40 PM
#24
Thread Starter
Addicted Member
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>
-
Dec 8th, 2021, 11:02 PM
#25
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.
-
Dec 9th, 2021, 01:20 AM
#26
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.
-
Dec 9th, 2021, 10:21 AM
#27
Thread Starter
Addicted Member
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.
-
Dec 9th, 2021, 10:53 AM
#28
Re: Read and write color to xml file
Originally Posted by mikeg71
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?
-
Dec 9th, 2021, 11:10 AM
#29
Thread Starter
Addicted Member
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
-
Dec 9th, 2021, 11:55 AM
#30
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
-
Dec 9th, 2021, 12:53 PM
#31
Thread Starter
Addicted Member
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?
-
Dec 9th, 2021, 01:39 PM
#32
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
-
Dec 9th, 2021, 01:47 PM
#33
Thread Starter
Addicted Member
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
-
Dec 15th, 2021, 11:09 AM
#34
Thread Starter
Addicted Member
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
-
Dec 15th, 2021, 11:39 AM
#35
Re: Read and write color to xml file
Originally Posted by mikeg71
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?
-
Dec 15th, 2021, 11:51 AM
#36
Thread Starter
Addicted Member
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.
-
Dec 15th, 2021, 12:35 PM
#37
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.
-
Dec 15th, 2021, 01:10 PM
#38
Thread Starter
Addicted Member
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.
-
Dec 15th, 2021, 09:14 PM
#39
-
Dec 16th, 2021, 09:43 AM
#40
Thread Starter
Addicted Member
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)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|