VB.NET Account對象簡介
VB.NET有很多值得學習的地方,這里我們主要介紹VB.NET Account對象,包括介紹是Load方法的Click事件等方面。
本文介紹三個文本框都用于保持當前VB.NET Account對象的數據。它們分別叫txtAccountID、txtCustomerName和 txtBalance.顯示Load的按鈕叫btnLoad,用于載入帳號集合。另兩個按鈕在記錄間導航,分別叫btnBack 和 btnForward.
帳號對象集合可以保持在ArrayList(數組列表)中,因此下面一行代碼應該在窗體代碼的最前面:
Dim colAccounts As ArrayList
下面是Load方法的Click事件代碼。它建立了一些VB.NET Account對象并把它們放入一個集合中,接著把該集合綁定到文本框。
- colAccounts = New ArrayList()
- colAccounts.Add(New Account(1, "ABC Company", 10))
- colAccounts.Add(New Account(2, "XYZ, Inc.", -10))
- colAccounts.Add(New Account(3, "MNP Limited", 0))
- txtAccountID.DataBindings.Add(New _
- Binding("Text", colAccounts, "AccountID"))
- txtCustomerName.DataBindings.Add(New _
- Binding("Text", colAccounts, "CustomerName"))
- txtBalance.DataBindings.Add(New _
- Binding("Text", colAccounts, "Balance"))
- txtBalance.DataBindings.Add(New _
- Binding("BackColor", colAccounts, "BackColor"))
注意最后兩行。txtBalance的Text屬性綁定到一個帳號的Balance屬性,并且該控件的BackColor屬性綁定到帳號對象的BackColor屬性。它演示了。NET框架組件綁定一個以上屬性到不同數據項。
現在點擊btnBack的Click事件,填入一下代碼:
- If Me.BindingContext(colAccounts).Position > 0 Then
- Me.BindingContext(colAccounts).Position -= 1
- End If
- 在btnForward的Click事件中寫入以下代碼:
- If Me.BindingContext(colAccounts).Position < colAccounts.Count - 1 Then
- Me.BindingContext(colAccounts).Position += 1
- End If
【編輯推薦】