【问题标题】:Reading dictionaries in other forms阅读其他形式的字典
【发布时间】:2017-03-05 14:48:55
【问题描述】:

这里是 VB 新手。希望过去一天左右我没有盯着答案看,但我在Form1 制作了一本字典,我想完全阅读到另一个表单上的列表框,希望能够添加并删除带有按钮的条目。解决此问题的最佳方法是什么?真的被困在这里了。

将 Visual Basic 2015 与 Windows 窗体应用程序一起使用。

提前致谢!

这是我想在另一个表单的列表框中阅读的字典。

Public Class Form1
Dim userLog As New Dictionary(Of String, String)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    userLog("one") = "aaa"
    userLog("two") = "aaa"
    userLog("three") = "aaa"

End Sub

【问题讨论】:

标签: .net vb.net winforms datagridview


【解决方案1】:

创建公共属性而不是私有变量

Public Property userLog As New Dictionary(Of String, String)

您可以使用Form1.userLog从其他表单访问它

阅读更多MSDN Article

【讨论】:

    【解决方案2】:

    要解决这个问题,请考虑以下事实:

    • ListBox 不是创建或编辑Dictionary 的好选择。 DataGridView 好多了。

    • 要将一个对象传递给另一个表单,只需在其他表单的构造函数中添加一个合适的参数并将数据传递给构造函数即可。

    字典编辑表格

    在本例中,我创建了一个表单来编辑字典。请注意以下几点:

    • 我从字典中创建了一个DataTable,然后将DatTable 设置为DataGridViewDataSource,最后将项目放回字典中。
    • 我将 Key 列设置为 PrimaryKey of DataTable 以使密钥唯一。
    • 我已处理 DataError 事件以在您输入无效密钥时显示错误
    • 为了将项目放回字典,我首先尝试将项目添加到一个空字典,最后如果任务成功,我清除主字典并将项目放回字典。

    示例源码如下:

    Imports System.ComponentModel
    Public Class DictionaryEditForm
        Public Sub New(ByVal Dictionary As Dictionary(Of String, String))
            InitializeComponent()
            Me.Dictionary = Dictionary
        End Sub
        Public Property Dictionary() As Dictionary(Of String, String)
        Dim Table As DataTable
        Const Key As String = "Key"
        Const Value As String = "Value"
        Private Sub DictionaryEditForm_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
            Table = New DataTable()
            Table.Columns.Add(Key, GetType(String))
            Table.Columns.Add(Value, GetType(String))
            Table.PrimaryKey = {Table.Columns(Key)}
            For Each item In Dictionary
                Table.Rows.Add(item.Key, item.Value)
            Next
            DataGridView1.DataSource = Table
        End Sub
        Private Sub SaveButton_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles SaveButton.Click
            Dim temp As New Dictionary(Of String, String)
            For Each item As DataRow In Table.Rows
                temp.Add(item.Field(Of String)(Key), item.Field(Of String)(Value))
            Next
            Dictionary.Clear()
            For Each item In temp
                Dictionary.Add(item.Key, item.Value)
            Next
            Me.DialogResult = DialogResult.OK
        End Sub
        Private Sub DataGridView1_DataError(ByVal sender As System.Object, _
        ByVal e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
            MessageBox.Show(e.Exception.Message)
            e.Cancel = True
        End Sub
    End Class
    

    用法

    创建DictionaryEditForm 的实例并将字典传递给它的构造函数就足够了,然后您可以使用它来编辑字典。

    Public Class MainForm
        Private Sub EditDictionaryButton_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles EditDictionaryButton.Click
            Dim Dictionary = New Dictionary(Of String, String) From
                {{"1", "One"}, {"2", "Two"}, {"3", "Three"}}
            Dim f = New DictionaryEditForm(Dictionary)
            f.ShowDialog()
        End Sub
    End Class
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      相关资源
      最近更新 更多