将组合框/数字上下选择绑定到数组



我正在做一个VB项目,该项目有很多组合框和数字项目。

假设我们有 ComboBox1、2、3、4 和 5;我们有 NumericUpDown1、2、3、4、5。

当用户单击"保存"按钮时,我想将他们所有选择的组合框项目和数字保存到CSV文件中。 有没有一种优雅/自动的方式来绑定所有.选定索引和 .这些项目的值到数组,以便我可以轻松地将数组写出到 CSV?

到目前为止,我知道这样做的唯一方法是手动将每个与数组位置相关联:

Arr(0) = ComboBox1.SelectedIndex
Arr(1) = ComboBox2.SelectedIndex
...
Arr(5) = NumericUpDown1.Value
Arr(6) = NumericUpDown2.Value
...
etc. 

这不会太糟糕,除了我有很多这样的项目,为每个项目写一行似乎很愚蠢。 我是 VB 的新手,所以这对某些人来说可能是一个明显的解决方案。 有什么想法吗?

将它们绑定到数组将非常方便,因为我还允许用户加载 CSV 文件,我想从 CSV 值自动填充组合框和数字下调。 我知道这样做的唯一方法是在他们单击加载文件按钮时手动将每个数组项移动到相应的组合框/数字项:

ComboBox1.SelectedIndex = Arr(0)
ComboBox2.SelectedIndex = Arr(1)
...
NumericUpDown1.Value = Arr(5)
NumericUpDown2.Value = Arr(6)
...
etc. 

编辑:这是一些应用程序信息,根据要求...

可以保存/加载的 CSV 文件如下所示:

#"Device Info","123456","asdfgh","0000","1.0x","1"
000F,0000,0032,0000,00C8,0001,0078,0101,0000,0001,0000,0001
010F,0078,0000,0103,0001,0000,000A,0005,0007,0006,0000
0001,000A,000A,000A,000A,0005,0005,0005,0002
...etc

标题行仅包含序列号、版本和其他杂项信息;它由目标设备自动生成。 所有其他行都是目标设备读入并自动配置自身的配置设定点。 我正在编写这个PC程序,以便能够使用漂亮的GUI界面编辑(并从头开始创建(这些配置CSV文件。 每个项目都与特定的设定值相关联,例如 000F = 语言,0032 = 系统频率,00C8 = 系统电压等。 我看到制作此配置程序的最简单方法是使用数字输入和下拉组合框,用户可以选择他们想要的内容。 每个 NUD 和 CBOX 等同于一个 CSV 文件数据字段。

您可以使用 Controls.Find(( 根据索引值获取对所需控件的引用。 这里有一个快速的例子来说明我的意思:

For i As Integer = 1 To 30
    Dim matches() As Control = Me.Controls.Find("NumericUpDown" & i, True)
    If matches.Length > 0 AndAlso TypeOf matches(0) Is NumericUpDown Then
        DirectCast(matches(0), NumericUpDown).Value = i
    End If
Next

您可以将此类代码合并到加载/保存例程中。

我会使用二进制序列化。这样就无需在保存控件属性时设置字符串或 xml 格式。与Plutonix的解决方案类似,它仅适用于某些类型的控制。但是,可以对其进行修改以处理任何类型的控件 - 但仅支持为每个控件加载单个属性。它将适用于 X 类型的所有控件,而不是名为 xName 的控件。可以通过在面板中对要序列化的控件进行分组或其他方式来添加进一步的限制。

创建一个名为 Form1 的新窗体。添加一些 NumericUpDowns、TextBox 和 ComboBoxes。在设计时将一些值放在组合框中,否则在 Form_Load 中调用loadState()将毫无意义。但是,loadState()可以在任何时候调用(即在填充组合框之后(。

您需要导入以下两个命名空间:

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary

在您的班级中:

Private Shared stateFileName As String = "SavedState.bin"
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    saveState(Me)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    loadState(Me)
End Sub

这些是您将用于保存和加载状态的方法。保存状态方法:

Private Shared Sub saveState(instance As Form)
    Dim controlProperties As Dictionary(Of String, Object) =
        instance.Controls.OfType(Of Control).ToDictionary(Of String, Object)(
            Function(c) c.Name,
            Function(c)
                ' You can support different types of controls here too
                If TypeOf c Is NumericUpDown Then
                    Return CType(c, NumericUpDown).Value
                ElseIf TypeOf c Is ComboBox Then
                    Return CType(c, ComboBox).SelectedIndex
                Else
                    ' All other controls get their text property saved
                    ' .Text is a property of Control
                    Return c.Text
                End If
            End Function)
    Using myFileStream As Stream = File.Create(stateFileName)
        Dim serializer As New BinaryFormatter
        serializer.Serialize(myFileStream, controlProperties)
    End Using
End Sub

负载状态方法:

Private Shared Sub loadState(instance As Form)
    If File.Exists(stateFileName) Then
        Using myFileStream As Stream = File.OpenRead(stateFileName)
            Dim deserializer As New BinaryFormatter()
            Dim controlProperties = CType(deserializer.Deserialize(myFileStream), Dictionary(Of String, Object))
            For Each c As Control In instance.Controls
                If controlProperties.ContainsKey(c.Name) Then
                    ' You can support different types of controls here too
                    If TypeOf c Is NumericUpDown Then
                        CType(c, NumericUpDown).Value = CDec(controlProperties(c.Name))
                    ElseIf TypeOf c Is ComboBox Then
                        CType(c, ComboBox).SelectedIndex = CInt(controlProperties(c.Name))
                    Else
                        c.Text = controlProperties(c.Name).ToString()
                    End If
                End If
            Next
        End Using
    End If
End Sub

您应该添加异常处理,并注意二进制文件不能在没有机器帮助的情况下由人类编辑。

最新更新