Author Topic: Possible to use INI to change basicButton colors?  (Read 1205 times)

PLCHorse

  • Newbie
  • *
  • Posts: 30
    • View Profile
Possible to use INI to change basicButton colors?
« on: December 06, 2024, 12:04:57 PM »
I tried importing INIFileHandling into basic button and i could change the text and the PLC address but I can't change any of the colors - it throws a typeError trying to cast String to Color. Is this possible to do and if so how would i do it?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5341
    • View Profile
    • AdvancedHMI
Re: Possible to use INI to change basicButton colors?
« Reply #1 on: December 06, 2024, 02:31:14 PM »
- In Solution Explorer, expand down the AdvancedHMIDrivers project
- Expand down the \Common folder
- Right Click Utilities.vb and select View Code
- Go to line 42 and edit the code to this:
Code: [Select]
                    If pi.PropertyType.IsEnum Then
                        Try
                            '* V3.99y - Added Enum capability
                            '* Enum type have to be converted from string to the Enum option
                            value = [Enum].Parse(pi.PropertyType, p.GetSetting(iniFileSection, settings(index)), True)
                        Catch ex As Exception
                            System.Windows.Forms.MessageBox.Show("Ini File Error - " & settings(index) & " is an an Enum. Values of " & p.GetSetting(iniFileSection, settings(index)) & " is not a valid option.")
                        End Try
                    Else
                        Dim pt As Type = targetObject.GetType().GetProperty(pi.Name).PropertyType
                        '* 6-DEC-24 Added so color properties can be used in INI files
                        If pt Is GetType(System.Drawing.Color) Then
                            Dim cc As New ColorConverter
                            value = cc.ConvertFromString(p.GetSetting(iniFileSection, settings(index)))
                        Else
                            value = Convert.ChangeType(p.GetSetting(iniFileSection, settings(index)), pt, Globalization.CultureInfo.InvariantCulture)
                        End If
                     End If

PLCHorse

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Possible to use INI to change basicButton colors?
« Reply #2 on: December 13, 2024, 12:08:17 PM »
Archie, Thank you for your help. I tried this and it still bombed out. After some uh... "Coding" I came up with this:

Regards!

Code: [Select]
    Public Shared Sub SetPropertiesByIniFile(ByVal targetObject As Object, ByVal iniFileName As String, ByVal iniFileSection As String)
        If targetObject Is Nothing Then
            Throw New System.ArgumentNullException("targetObject", "SetPropertiesByIniFile null parameter of targetObject")
        End If

        If Not String.IsNullOrEmpty(iniFileName) Then
            Dim p As New IniParser(iniFileName)
            Dim settings() As String = p.ListSettings(iniFileSection)
            '* Loop thtough all the settings in this section
            For index = 0 To settings.Length - 1
                Dim pi As System.Reflection.PropertyInfo
                pi = targetObject.GetType().GetProperty(settings(index), Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance)
                '* Check if a matching property name exists in the targetObject
                If pi IsNot Nothing Then
                    Dim value As Object = Nothing
                    If pi.PropertyType.IsEnum Then
                        If pi.PropertyType.IsEnum Then
                            Try
                                '* V3.99y - Added Enum capability
                                '* Enum type have to be converted from string to the Enum option
                                value = [Enum].Parse(pi.PropertyType, p.GetSetting(iniFileSection, settings(index)), True)
                            Catch ex As Exception
                                System.Windows.Forms.MessageBox.Show("Ini File Error - " & settings(index) & " is an an Enum. Values of " & p.GetSetting(iniFileSection, settings(index)) & " is not a valid option.")
                            End Try
                        Else
                            Dim pt As Type = targetObject.GetType().GetProperty(pi.Name).PropertyType
                            '* 6-DEC-24 Added so color properties can be used in INI files
                            If pt Is GetType(System.Drawing.Color) Then
                                Dim cc As New ColorConverter
                                value = cc.ConvertFromString(p.GetSetting(iniFileSection, settings(index)))
                            Else
                                value = Convert.ChangeType(p.GetSetting(iniFileSection, settings(index)), pt, Globalization.CultureInfo.InvariantCulture)
                            End If
                        End If
                    Else
                        Dim px As Type = targetObject.GetType().GetProperty(pi.Name).PropertyType
                        If px Is GetType(System.Drawing.Color) Then
                            Dim cc As New ColorConverter
                            value = cc.ConvertFromString(p.GetSetting(iniFileSection, settings(index)))
                        Else
                            value = Convert.ChangeType(p.GetSetting(iniFileSection, settings(index)), targetObject.GetType().GetProperty(pi.Name).PropertyType, Globalization.CultureInfo.InvariantCulture)

                        End If
                    End If
                    pi.SetValue(targetObject, value, Nothing)
                Else
                    System.Windows.Forms.MessageBox.Show("Ini File Error - " & settings(index) & " is not a valid property.")
                End If
            Next
        End If
    End Sub