Zum grunsätzlichen Verständis des
Settings richtig verwenden + an Settings binden
Im Normalfall wird ja jedes einzelne Control im Designer gebunden, das ist mir zu mühselig.
Um das ganze
Einfach die SettingPropertys in volgendem Schema benennen,
dann
SettingsBinder
Um das benötige ControlArray zu erhalten, hier noch eine kleine Helfer-Klasse.
Helper - ControlMethods.GetControls
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.
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
) unddann
SettingsBinder.BindToControls(Control())
(+7 Überladungen) ausführen. Ist ein Control
nicht im Array
vorhanden, wird eine Exeption
geworfen.VB.NET-Quellcode
- Imports System.Configuration
- Public Class SettingsBinder
- #Region "Overloads"
- ''' <summary>
- ''' Bind SettingsProperty to controls. Propertys must have the following format :
- ''' "Control.Name_Control.Property", for example "TextBox1_Text".
- ''' </summary>
- ''' <param name="_controls"></param>
- ''' <param name="_dataSourceUpdateMode"></param>
- Public Shared Sub BindToControls(ByVal _controls As Control(),
- ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
- DataSourceUpdateMode.OnPropertyChanged)
- BindToControls(My.Settings.Properties, _controls, My.MySettings.Default, True, _dataSourceUpdateMode)
- End Sub
- ''' <summary>
- ''' Bind SettingsProperty to controls. Propertys must have the following format :
- ''' "Control.Name_Control.Property", for example "TextBox1_Text".
- ''' </summary>
- ''' <param name="_settingsPropertys"></param>
- ''' <param name="_controls"></param>
- ''' <param name="_dataSourceUpdateMode"></param>
- Public Shared Sub BindToControls(ByVal _settingsPropertys As SettingsPropertyCollection,
- ByVal _controls As Control(),
- ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
- DataSourceUpdateMode.OnPropertyChanged)
- BindToControls(_settingsPropertys, _controls, My.MySettings.Default, True, _dataSourceUpdateMode)
- End Sub
- ''' <summary>
- ''' Bind SettingsProperty to controls. Propertys must have the following format :
- ''' "Control.Name_Control.Property", for example "TextBox1_Text".
- ''' </summary>
- ''' <param name="_controls"></param>
- ''' <param name="_dataSource"></param>
- ''' <param name="_dataSourceUpdateMode"></param>
- Public Shared Sub BindToControls(ByVal _controls As Control(),
- ByVal _dataSource As Object,
- ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
- DataSourceUpdateMode.OnPropertyChanged)
- BindToControls(My.Settings.Properties, _controls, _dataSource, True, _dataSourceUpdateMode)
- End Sub
- ''' <summary>
- ''' Bind SettingsProperty to controls. Propertys must have the following format :
- ''' "Control.Name_Control.Property", for example "TextBox1_Text".
- ''' </summary>
- ''' <param name="_settingsPropertys"></param>
- ''' <param name="_controls"></param>
- ''' <param name="_dataSource"></param>
- ''' <param name="_dataSourceUpdateMode"></param>
- Public Shared Sub BindToControls(ByVal _settingsPropertys As SettingsPropertyCollection,
- ByVal _controls As Control(),
- ByVal _dataSource As Object,
- ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
- DataSourceUpdateMode.OnPropertyChanged)
- BindToControls(_settingsPropertys, _controls, _dataSource, True, _dataSourceUpdateMode)
- End Sub
- ''' <summary>
- ''' Bind SettingsProperty to controls. Propertys must have the following format :
- ''' "Control.Name_Control.Property", for example "TextBox1_Text".
- ''' </summary>
- ''' <param name="_controls"></param>
- ''' <param name="_formattingEnabled"></param>
- ''' <param name="_dataSourceUpdateMode"></param>
- Public Shared Sub BindToControls(ByVal _controls As Control(),
- ByVal _formattingEnabled As Boolean,
- ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
- DataSourceUpdateMode.OnPropertyChanged)
- BindToControls(My.Settings.Properties, _controls, My.MySettings.Default,
- _formattingEnabled, _dataSourceUpdateMode)
- End Sub
- ''' <summary>
- ''' Bind SettingsProperty to controls. Propertys must have the following format :
- ''' "Control.Name_Control.Property", for example "TextBox1_Text".
- ''' </summary>
- ''' <param name="_settingsPropertys"></param>
- ''' <param name="_controls"></param>
- ''' <param name="_formattingEnabled"></param>
- ''' <param name="_dataSourceUpdateMode"></param>
- Public Shared Sub BindToControls(ByVal _settingsPropertys As SettingsPropertyCollection,
- ByVal _controls As Control(),
- ByVal _formattingEnabled As Boolean,
- ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
- DataSourceUpdateMode.OnPropertyChanged)
- BindToControls(_settingsPropertys, _controls, My.MySettings.Default,
- _formattingEnabled, _dataSourceUpdateMode)
- End Sub
- ''' <summary>
- ''' Bind SettingsProperty to controls. Propertys must have the following format :
- ''' "Control.Name_Control.Property", for example "TextBox1_Text".
- ''' </summary>
- ''' <param name="_controls"></param>
- ''' <param name="_dataSource"></param>
- ''' <param name="_formattingEnabled"></param>
- ''' <param name="_dataSourceUpdateMode"></param>
- Public Shared Sub BindToControls(ByVal _controls As Control(),
- ByVal _dataSource As Object,
- ByVal _formattingEnabled As Boolean,
- ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
- DataSourceUpdateMode.OnPropertyChanged)
- BindToControls(My.Settings.Properties, _controls, _dataSource, _formattingEnabled, _dataSourceUpdateMode)
- End Sub
- #End Region
- ''' <summary>
- ''' Bind SettingsProperty to controls. Propertys must have the following format :
- ''' "Control.Name_Control.Property", for example "TextBox1_Text".
- ''' </summary>
- ''' <param name="_settingsPropertys"></param>
- ''' <param name="_controls"></param>
- ''' <param name="_dataSource"></param>
- ''' <param name="_formattingEnabled"></param>
- ''' <param name="_dataSourceUpdateMode"></param>
- Public Shared Sub BindToControls(ByVal _settingsPropertys As SettingsPropertyCollection,
- ByVal _controls As Control(),
- ByVal _dataSource As Object,
- ByVal _formattingEnabled As Boolean,
- ByVal Optional _dataSourceUpdateMode As DataSourceUpdateMode =
- DataSourceUpdateMode.OnPropertyChanged)
- Dim propertyNames As New List(Of KeyValuePair(Of String, String))
- For Each propertyName As String In GetSettingsPropertyNames(_settingsPropertys)
- Dim propertyNameSplitted As String() = propertyName.Split("_"c)
- If propertyNameSplitted.Length = 2 Then
- propertyNames.Add(
- New KeyValuePair(Of String, String)(propertyNameSplitted.First, propertyNameSplitted.Last))
- End If
- Next
- For Each settingsPropertysName As KeyValuePair(Of String, String) In propertyNames
- Dim fullSettingsPropertysName As String = $"{settingsPropertysName.Key}_{settingsPropertysName.Value}"
- If GetControlNames(_controls).Contains(settingsPropertysName.Key) Then
- GetControlByName(_controls, settingsPropertysName.Key).DataBindings.Add(
- New Binding(settingsPropertysName.Value,
- _dataSource,
- fullSettingsPropertysName,
- _formattingEnabled,
- _dataSourceUpdateMode))
- Else
- Throw New ControlBindToSettingPropertyException(
- $"Can't bind property '{fullSettingsPropertysName}', control '{settingsPropertysName.Key}' not found in '_controls'.")
- End If
- Next
- End Sub
- #Region "Private Methods"
- Private Shared Function GetControlByName(ByVal _controls As Control(), ByVal _name As String) As Control
- Dim controlNames As New List(Of String)
- For Each control As Control In _controls
- If control.Name = _name Then Return control
- Next
- Return Nothing
- End Function
- Private Shared Function GetControlNames(ByVal _controls As Control()) As List(Of String)
- Dim controlNames As New List(Of String)
- For Each control As Control In _controls
- controlNames.Add(control.Name)
- Next
- Return controlNames
- End Function
- Private Shared Function GetSettingsPropertyNames(ByVal _value As SettingsPropertyCollection) As List(Of String)
- Dim propertyNames As New List(Of String)
- For Each settingsProperty As SettingsProperty In _value
- propertyNames.Add(settingsProperty.Name)
- Next
- Return propertyNames
- End Function
- #End Region
- <Serializable>
- Private Class ControlBindToSettingPropertyException
- Inherits Exception
- Public Sub New(ByVal _message As String)
- MyBase.New(_message)
- End Sub
- End Class
- End Class
Um das benötige ControlArray zu erhalten, hier noch eine kleine Helfer-Klasse.
VB.NET-Quellcode
- Namespace Helper
- Public Class ControlMethods
- #Region "Overloads"
- Public Shared Function GetControls(ByVal _parent As Control,
- ByVal Optional _blacklist As Control() = Nothing) As Control()
- Return GetControls(_parent, False, Nothing, _blacklist)
- End Function
- Public Shared Function GetControls(ByVal _parent As Control,
- ByVal _recursive As Boolean,
- ByVal Optional _blacklist As Control() = Nothing) As Control()
- Return GetControls(_parent, _recursive, Nothing, _blacklist)
- End Function
- Public Shared Function GetControls(ByVal _parent As Control,
- ByVal _types As Type(),
- ByVal Optional _blacklist As Control() = Nothing) As Control()
- Return GetControls(_parent, False, _types, _blacklist)
- End Function
- Public Shared Function GetControls(ByVal _parent As Control,
- ByVal _types As Type(),
- ByVal _recursive As Boolean,
- ByVal Optional _blacklist As Control() = Nothing) As Control()
- Return GetControls(_parent, _recursive, _types, _blacklist)
- End Function
- #End Region
- Public Shared Function GetControls(ByVal _parent As Control,
- ByVal _recursive As Boolean,
- ByVal _types As Type(),
- ByVal Optional _blacklist As Control() = Nothing) As Control()
- Dim controlList As New List(Of Control)
- For Each control As Control In _parent.Controls
- If _types Is Nothing OrElse _types.Contains(control.GetType) Then controlList.Add(control)
- If _recursive Then controlList =
- controlList.Concat(GetControls(control, _recursive, _types, _blacklist)).ToList
- Next
- If Not _blacklist Is Nothing Then controlList = controlList.Except(_blacklist).ToList
- Return controlList.ToArray
- End Function
- End Class
- End Namespace
bit.ly/DotNetSearch
Code Konverter:
Online: @ developerfusion.com, @ telerik.com
Offline stand alone: Convert .NET, C# to VB Converter
VS Extensions: Convert .NET (Based on .NET 4.5), Language Convert
Code Konverter:
Online: @ developerfusion.com, @ telerik.com
Offline stand alone: Convert .NET, C# to VB Converter
VS Extensions: Convert .NET (Based on .NET 4.5), Language Convert