2017-03-05 70 views
2

新手VB用戶在這裏。希望在過去的一天左右我沒有注意到答案,但是我在Form1中製作了一本字典,我希望能夠在另一個表單上閱讀一個列表框,完全希望能夠添加和刪除帶按鈕的條目。什麼是最好的方法去做這件事?這裏真的來了。以其他形式閱讀字典

在Windows窗體應用程序中使用Visual Basic 2015。

提前致謝!

這是所提到的詞典,我想在其他表單上的列表框上閱讀。

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 
+0

這是Web窗體?還是Windows窗體?你應該永遠清楚你使用的框架是通過適當地標記你的問題。 – mason

+0

感謝您的建議。會相應更新。 –

+0

[顯示可編輯字典的最簡單方法是什麼]可能的重複(http://stackoverflow.com/questions/2798933/what-is-the-easiest-way-to-display-an-editable-dictionary) – Fabio

回答

2

創建一個公共屬性,而不是一個私有變量的

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

您可以從其他形式使用Form1.userLog

詳情請閱讀此MSDN Article

2

爲了解決這個問題考慮訪問這些事實:

  • ListBox不是創建或編輯Dictionary的好選擇。 DataGridView好得多。

  • 要將對象傳遞給另一個表單,只需向其他表單的構造函數添加一個合適的參數並將數據傳遞給構造函數即可。

Dictionary in Grid

字典編輯表單

在這個例子中,我創建了一個表單編輯字典。請注意這幾點:

  • 我創建從字典中的DataTable,然後設置DatTableDataGridViewDataSource,最後放物品回字典。
  • 我設置了Key列作爲PrimaryKeyDataTable以使密鑰唯一。
  • 我處理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一個實例,並把字典給它的構造,那麼你可以編輯字典usnig它。

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