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对象并把它们放入一个集合中,接着把该集合绑定到文本框。

 
 
 
  1. colAccounts = New ArrayList()  
  2. colAccounts.Add(New Account(1, "ABC Company", 10))  
  3. colAccounts.Add(New Account(2, "XYZ, Inc.", -10))  
  4. colAccounts.Add(New Account(3, "MNP Limited", 0))  
  5.  
  6. txtAccountID.DataBindings.Add(New _   
  7. Binding("Text", colAccounts, "AccountID"))  
  8. txtCustomerName.DataBindings.Add(New _   
  9. Binding("Text", colAccounts, "CustomerName"))  
  10. txtBalance.DataBindings.Add(New _  
  11. Binding("Text", colAccounts, "Balance"))  
  12. txtBalance.DataBindings.Add(New _   
  13. Binding("BackColor", colAccounts, "BackColor")) 

注意最后两行。txtBalance的Text属性绑定到一个帐号的Balance属性,并且该控件的BackColor属性绑定到帐号对象的BackColor属性。它演示了。NET框架组件绑定一个以上属性到不同数据项。

现在点击btnBack的Click事件,填入一下代码:

 
 
 
  1. If Me.BindingContext(colAccounts).Position > 0 Then  
  2. Me.BindingContext(colAccounts).Position -1 
  3. End If  
  4. 在btnForward的Click事件中写入以下代码:  
  5. If Me.BindingContext(colAccounts).Position < colAccounts.Count - 1 Then  
  6. Me.BindingContext(colAccounts).Position += 1  
  7. End If 

【编辑推荐】

  1. 简单描述VB.NET散列函数
  2. 详细分析VB.NET读写文本文件
  3. VB.NET GroupBox控件学习经验
  4. 概括VB.NET DomainUpDown控件
  5. VB.NET编码算法学习笔记
THE END