VB.NET Account對(duì)象簡(jiǎn)介
VB.NET有很多值得學(xué)習(xí)的地方,這里我們主要介紹VB.NET Account對(duì)象,包括介紹是Load方法的Click事件等方面。
本文介紹三個(gè)文本框都用于保持當(dāng)前VB.NET Account對(duì)象的數(shù)據(jù)。它們分別叫txtAccountID、txtCustomerName和 txtBalance.顯示Load的按鈕叫btnLoad,用于載入帳號(hào)集合。另兩個(gè)按鈕在記錄間導(dǎo)航,分別叫btnBack 和 btnForward.
帳號(hào)對(duì)象集合可以保持在ArrayList(數(shù)組列表)中,因此下面一行代碼應(yīng)該在窗體代碼的最前面:
Dim colAccounts As ArrayList
下面是Load方法的Click事件代碼。它建立了一些VB.NET Account對(duì)象并把它們放入一個(gè)集合中,接著把該集合綁定到文本框。
- 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屬性綁定到一個(gè)帳號(hào)的Balance屬性,并且該控件的BackColor屬性綁定到帳號(hào)對(duì)象的BackColor屬性。它演示了。NET框架組件綁定一個(gè)以上屬性到不同數(shù)據(jù)項(xiàng)。
現(xiàn)在點(diǎn)擊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
【編輯推薦】