SettingsBinder - Komfortables binden von Controls an Settings

    • VB.NET

      SettingsBinder - Komfortables binden von Controls an Settings

      Zum grunsätzlichen Verständis des Settings und Binding Konzepts, empfehle ich ein Tutorial vom @ErfinderDesRades:
      Settings richtig verwenden + an Settings binden

      Im Normalfall wird ja jedes einzelne Control im Designer gebunden, das ist mir zu mühselig. :whistling:
      Um das ganze Binding Gedöns in einem Rutsch abzuwickeln, habe ich mir folgende Klasse zusammen gebastelt.
      Einfach die SettingPropertys in volgendem Schema benennen, "ControlName_ControlValue" (z.B. TextBox1_Text) und
      dann SettingsBinder.BindToControls(Control()) (+7 Überladungen) ausführen. Ist ein Control nicht im Array vorhanden, wird eine Exeption geworfen.

      SettingsBinder

      VB.NET-Quellcode

      1. Imports System.Configuration
      2. Public Class SettingsBinder
      3. #Region "Overloads"
      4. ''' <summary>
      5. ''' Bind SettingsProperty to controls. Propertys must have the following format :
      6. ''' "Control.Name_Control.Property", for example "TextBox1_Text".
      7. ''' </summary>
      8. ''' <param name="_controls"></param>
      9. ''' <param name="_dataSourceUpdateMode"></param>
      10. Public Shared Sub BindToControls(ByVal _controls As Control(),
      11. ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
      12. DataSourceUpdateMode.OnPropertyChanged)
      13. BindToControls(My.Settings.Properties, _controls, My.MySettings.Default, True, _dataSourceUpdateMode)
      14. End Sub
      15. ''' <summary>
      16. ''' Bind SettingsProperty to controls. Propertys must have the following format :
      17. ''' "Control.Name_Control.Property", for example "TextBox1_Text".
      18. ''' </summary>
      19. ''' <param name="_settingsPropertys"></param>
      20. ''' <param name="_controls"></param>
      21. ''' <param name="_dataSourceUpdateMode"></param>
      22. Public Shared Sub BindToControls(ByVal _settingsPropertys As SettingsPropertyCollection,
      23. ByVal _controls As Control(),
      24. ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
      25. DataSourceUpdateMode.OnPropertyChanged)
      26. BindToControls(_settingsPropertys, _controls, My.MySettings.Default, True, _dataSourceUpdateMode)
      27. End Sub
      28. ''' <summary>
      29. ''' Bind SettingsProperty to controls. Propertys must have the following format :
      30. ''' "Control.Name_Control.Property", for example "TextBox1_Text".
      31. ''' </summary>
      32. ''' <param name="_controls"></param>
      33. ''' <param name="_dataSource"></param>
      34. ''' <param name="_dataSourceUpdateMode"></param>
      35. Public Shared Sub BindToControls(ByVal _controls As Control(),
      36. ByVal _dataSource As Object,
      37. ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
      38. DataSourceUpdateMode.OnPropertyChanged)
      39. BindToControls(My.Settings.Properties, _controls, _dataSource, True, _dataSourceUpdateMode)
      40. End Sub
      41. ''' <summary>
      42. ''' Bind SettingsProperty to controls. Propertys must have the following format :
      43. ''' "Control.Name_Control.Property", for example "TextBox1_Text".
      44. ''' </summary>
      45. ''' <param name="_settingsPropertys"></param>
      46. ''' <param name="_controls"></param>
      47. ''' <param name="_dataSource"></param>
      48. ''' <param name="_dataSourceUpdateMode"></param>
      49. Public Shared Sub BindToControls(ByVal _settingsPropertys As SettingsPropertyCollection,
      50. ByVal _controls As Control(),
      51. ByVal _dataSource As Object,
      52. ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
      53. DataSourceUpdateMode.OnPropertyChanged)
      54. BindToControls(_settingsPropertys, _controls, _dataSource, True, _dataSourceUpdateMode)
      55. End Sub
      56. ''' <summary>
      57. ''' Bind SettingsProperty to controls. Propertys must have the following format :
      58. ''' "Control.Name_Control.Property", for example "TextBox1_Text".
      59. ''' </summary>
      60. ''' <param name="_controls"></param>
      61. ''' <param name="_formattingEnabled"></param>
      62. ''' <param name="_dataSourceUpdateMode"></param>
      63. Public Shared Sub BindToControls(ByVal _controls As Control(),
      64. ByVal _formattingEnabled As Boolean,
      65. ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
      66. DataSourceUpdateMode.OnPropertyChanged)
      67. BindToControls(My.Settings.Properties, _controls, My.MySettings.Default,
      68. _formattingEnabled, _dataSourceUpdateMode)
      69. End Sub
      70. ''' <summary>
      71. ''' Bind SettingsProperty to controls. Propertys must have the following format :
      72. ''' "Control.Name_Control.Property", for example "TextBox1_Text".
      73. ''' </summary>
      74. ''' <param name="_settingsPropertys"></param>
      75. ''' <param name="_controls"></param>
      76. ''' <param name="_formattingEnabled"></param>
      77. ''' <param name="_dataSourceUpdateMode"></param>
      78. Public Shared Sub BindToControls(ByVal _settingsPropertys As SettingsPropertyCollection,
      79. ByVal _controls As Control(),
      80. ByVal _formattingEnabled As Boolean,
      81. ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
      82. DataSourceUpdateMode.OnPropertyChanged)
      83. BindToControls(_settingsPropertys, _controls, My.MySettings.Default,
      84. _formattingEnabled, _dataSourceUpdateMode)
      85. End Sub
      86. ''' <summary>
      87. ''' Bind SettingsProperty to controls. Propertys must have the following format :
      88. ''' "Control.Name_Control.Property", for example "TextBox1_Text".
      89. ''' </summary>
      90. ''' <param name="_controls"></param>
      91. ''' <param name="_dataSource"></param>
      92. ''' <param name="_formattingEnabled"></param>
      93. ''' <param name="_dataSourceUpdateMode"></param>
      94. Public Shared Sub BindToControls(ByVal _controls As Control(),
      95. ByVal _dataSource As Object,
      96. ByVal _formattingEnabled As Boolean,
      97. ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
      98. DataSourceUpdateMode.OnPropertyChanged)
      99. BindToControls(My.Settings.Properties, _controls, _dataSource, _formattingEnabled, _dataSourceUpdateMode)
      100. End Sub
      101. #End Region
      102. ''' <summary>
      103. ''' Bind SettingsProperty to controls. Propertys must have the following format :
      104. ''' "Control.Name_Control.Property", for example "TextBox1_Text".
      105. ''' </summary>
      106. ''' <param name="_settingsPropertys"></param>
      107. ''' <param name="_controls"></param>
      108. ''' <param name="_dataSource"></param>
      109. ''' <param name="_formattingEnabled"></param>
      110. ''' <param name="_dataSourceUpdateMode"></param>
      111. Public Shared Sub BindToControls(ByVal _settingsPropertys As SettingsPropertyCollection,
      112. ByVal _controls As Control(),
      113. ByVal _dataSource As Object,
      114. ByVal _formattingEnabled As Boolean,
      115. ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
      116. DataSourceUpdateMode.OnPropertyChanged)
      117. Dim propertyNames As New List(Of KeyValuePair(Of String, String))
      118. For Each propertyName As String In GetSettingsPropertyNames(_settingsPropertys)
      119. Dim propertyNameSplitted As String() = propertyName.Split("_"c)
      120. If propertyNameSplitted.Length = 2 Then
      121. propertyNames.Add(
      122. New KeyValuePair(Of String, String)(propertyNameSplitted.First, propertyNameSplitted.Last))
      123. End If
      124. Next
      125. For Each settingsPropertysName As KeyValuePair(Of String, String) In propertyNames
      126. Dim fullSettingsPropertysName As String = $"{settingsPropertysName.Key}_{settingsPropertysName.Value}"
      127. If GetControlNames(_controls).Contains(settingsPropertysName.Key) Then
      128. GetControlByName(_controls, settingsPropertysName.Key).DataBindings.Add(
      129. New Binding(settingsPropertysName.Value,
      130. _dataSource,
      131. fullSettingsPropertysName,
      132. _formattingEnabled,
      133. _dataSourceUpdateMode))
      134. Else
      135. Throw New ControlBindToSettingPropertyException(
      136. $"Can't bind property '{fullSettingsPropertysName}', control '{settingsPropertysName.Key}' not found in '_controls'.")
      137. End If
      138. Next
      139. End Sub
      140. #Region "Private Methods"
      141. Private Shared Function GetControlByName(ByVal _controls As Control(), ByVal _name As String) As Control
      142. Dim controlNames As New List(Of String)
      143. For Each control As Control In _controls
      144. If control.Name = _name Then Return control
      145. Next
      146. Return Nothing
      147. End Function
      148. Private Shared Function GetControlNames(ByVal _controls As Control()) As List(Of String)
      149. Dim controlNames As New List(Of String)
      150. For Each control As Control In _controls
      151. controlNames.Add(control.Name)
      152. Next
      153. Return controlNames
      154. End Function
      155. Private Shared Function GetSettingsPropertyNames(ByVal _value As SettingsPropertyCollection) As List(Of String)
      156. Dim propertyNames As New List(Of String)
      157. For Each settingsProperty As SettingsProperty In _value
      158. propertyNames.Add(settingsProperty.Name)
      159. Next
      160. Return propertyNames
      161. End Function
      162. #End Region
      163. <Serializable>
      164. Private Class ControlBindToSettingPropertyException
      165. Inherits Exception
      166. Public Sub New(ByVal _message As String)
      167. MyBase.New(_message)
      168. End Sub
      169. End Class
      170. End Class

      Um das benötige ControlArray zu erhalten, hier noch eine kleine Helfer-Klasse.
      Helper - ControlMethods.GetControls

      VB.NET-Quellcode

      1. Namespace Helper
      2. Public Class ControlMethods
      3. #Region "Overloads"
      4. Public Shared Function GetControls(ByVal _parent As Control,
      5. ByVal Optional _blacklist As Control() = Nothing) As Control()
      6. Return GetControls(_parent, False, Nothing, _blacklist)
      7. End Function
      8. Public Shared Function GetControls(ByVal _parent As Control,
      9. ByVal _recursive As Boolean,
      10. ByVal Optional _blacklist As Control() = Nothing) As Control()
      11. Return GetControls(_parent, _recursive, Nothing, _blacklist)
      12. End Function
      13. Public Shared Function GetControls(ByVal _parent As Control,
      14. ByVal _types As Type(),
      15. ByVal Optional _blacklist As Control() = Nothing) As Control()
      16. Return GetControls(_parent, False, _types, _blacklist)
      17. End Function
      18. Public Shared Function GetControls(ByVal _parent As Control,
      19. ByVal _types As Type(),
      20. ByVal _recursive As Boolean,
      21. ByVal Optional _blacklist As Control() = Nothing) As Control()
      22. Return GetControls(_parent, _recursive, _types, _blacklist)
      23. End Function
      24. #End Region
      25. Public Shared Function GetControls(ByVal _parent As Control,
      26. ByVal _recursive As Boolean,
      27. ByVal _types As Type(),
      28. ByVal Optional _blacklist As Control() = Nothing) As Control()
      29. Dim controlList As New List(Of Control)
      30. For Each control As Control In _parent.Controls
      31. If _types Is Nothing OrElse _types.Contains(control.GetType) Then controlList.Add(control)
      32. If _recursive Then controlList =
      33. controlList.Concat(GetControls(control, _recursive, _types, _blacklist)).ToList
      34. Next
      35. If Not _blacklist Is Nothing Then controlList = controlList.Except(_blacklist).ToList
      36. Return controlList.ToArray
      37. End Function
      38. End Class
      39. End Namespace