BindingSource控件是.NET Compact Framework 2.0提供的新控件之一。BindingSource控件与数据源建立连接,然后将窗体中的控件与BindingSource控件建立绑定关系来实现数据绑定,简化数据绑定的过程。BindingSource控件即是一个连接后台数据库的渠道,同时又是一个数据源,因为BindingSource控件即支持向后台数据库发送命令来检索数据,又支持直接通过BindingSource控件对数据进行访问、排序、筛选和更新操作。BindingSource控件能够自动管理许多绑定问题。BindingSource控件没有运行时界面,无法在用户界面上看到该控件。
BindingSource控件通过Current属性访问当前记录,通过List属性访问整个数据表。表4-8列出了BindingSource控件的主要属性。
表4-8:BindingSource的主要属性
属性 |
说明 |
AllowEdit |
指示是否可以编辑BindingSource控件中的记录。 |
AllowNew |
指示是否可以使用 AddNew 方法向BindingSource控件添加记录。 |
AllowRemove |
指示是否可从BindingSource控件中删除记录。 |
Count |
获取BindingSource控件中的记录数。 |
CurrencyManager |
获取与BindingSource控件关联的当前记录管理器。 |
Current |
获取BindingSource控件中的当前记录。 |
DataMember |
获取或设置连接器当前绑定到的数据源中的特定数据列表或数据库表。 |
DataSource |
获取或设置连接器绑定到的数据源。 |
Filter |
获取或设置用于筛选的表达式。 |
Item |
获取或设置指定索引的记录。 |
Sort |
获取或设置用于排序的列名来指定排序。 |
通过Current属性及RemoveCurrent、EndEdit、CancelEdit、Add和AddNew方法可实现对当前记录的编辑操作。表4-3列出了BindingSource控件的主要方法。
表4-3:BindingSource的主要方法
方法 |
说明 |
Add |
将现有项添加到内部列表中。 |
CancelEdit |
取消当前编辑操作。 |
Clear |
从列表中移除所有元素。 |
EndEdit |
将挂起的更改应用于基础数据源。 |
Find |
在数据源中查找指定的项。 |
MoveFirst |
移至列表中的第一项。 |
MoveLast |
移至列表中的最后一项。 |
MoveNext |
移至列表中的下一项。 |
MovePrevious |
移至列表中的上一项。 |
RemoveCurrent |
从列表中移除当前项。 |
清单4-11演示通过BindingSource控件将包含TreeNode列表的BindingList对象绑定到ListBox控件。清单4-12演示包含TreeNode列表的BindingList对象的类定义。
清单 4-11 使用BindingSource控件进行绑定
<group id="_x0000_s1035" style="WIDTH: 414pt; HEIGHT: 335.4pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,5846"><lock v:ext="edit" aspectratio="t"></lock><shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></path><lock v:ext="edit" aspectratio="t"></lock></shapetype><shape id="_x0000_s1036" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 5846px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></shape><rect id="_x0000_s1037" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 5846px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">……<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim myNodeList As New MyTreeNodeList()<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">myNodeList.Add(New Windows.Forms.TreeNode("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">西藏</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">"))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">myNodeList.Add(New Windows.Forms.TreeNode("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">广东省</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">"))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">myNodeList.Add(New Windows.Forms.TreeNode("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">广西壮族自治区</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">"))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">myNodeList.Add(New Windows.Forms.TreeNode("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">海南省</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">"))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">myNodeList.Add(New Windows.Forms.TreeNode("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">四川省</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">"))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">myNodeList.Add(New Windows.Forms.TreeNode("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">山东省</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">"))<span style="mso-spacerun: yes"> </span><p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">myNodeList.Add(New Windows.Forms.TreeNode("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">河南省</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">"))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">myNodeList.Add(New Windows.Forms.TreeNode("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">陕西省</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">"))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">myNodeList.Add(New Windows.Forms.TreeNode("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">海南省</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">"))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">myNodeList.Add(New Windows.Forms.TreeNode("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">湖南省</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">"))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">myNodeList.Add(New Windows.Forms.TreeNode("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">吉林省</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">"))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">myNodeList.Add(New Windows.Forms.TreeNode("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">山西省</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">"))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">myNodeList.Add(New Windows.Forms.TreeNode("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">宁夏回族自治区</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">"))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">BindingSource1.DataSource = myNodeList<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">ListBox1.DataSource = BindingSource1<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">ListBox1.DisplayMember = "Text"<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">……<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
清单 4-12 BindingList对象的类定义
<group id="_x0000_s1038" style="WIDTH: 414pt; HEIGHT: 358.8pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,6254"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1039" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 6254px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></shape><rect id="_x0000_s1040" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 6254px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Public Class MyTreeNodeList<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Inherits System.ComponentModel.BindingList(Of System.Windows.Forms.TreeNode)<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Protected Overrides ReadOnly Property SupportsSearchingCore() As Boolean<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Get<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Return True<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>End Get<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>End Property<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Protected Overrides Function FindCore(ByVal prop As System.ComponentModel.PropertyDescriptor,_<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"></span><span style="mso-spacerun: yes"> </span>ByVal key As Object) As Integer<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>' </font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">忽略</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"> prop </font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">值,直接遍历列表进行查找匹配</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Dim i As Integer<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>While i < Count<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>If Items(i).Text.ToLower() = CStr(key).ToLower() Then<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Return i<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>End If<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>i += 1<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>End While<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Return -1<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>End Function<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">End Class<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
也可以在设计时,通过属相窗口定义ListBox控件的DataSource和DisplayMember属性,来实现控件数据绑定。
设置BindingSource控件和ListBox控件数据绑定之后,就可以使用BindingSource控件的Add方法实现向ListBox控件新增数据项。清单4-13演示向ListBox控件新增数据项。
清单 4-13 向ListBox控件新增数据项
<group id="_x0000_s1032" style="WIDTH: 414pt; HEIGHT: 195pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,3400"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1033" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 3400px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></shape><rect id="_x0000_s1034" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 3400px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">……</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">If BindingSource1.AllowNew <> True Then<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Windows.Forms.MessageBox.Show("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">不能增加列表数据项。</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">")<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Else<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Dim foundIndex As Integer = BindingSource1.Add(New Windows.Forms.TreeNode(TextBox1.Text))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>If foundIndex > -1 Then<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>ListBox1.SelectedIndex = foundIndex<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Else<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Windows.Forms.MessageBox.Show("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">增加列表数据项</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"> " + TextBox1.Text + "</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">失败。</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">")<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>End If<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">End If<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">……</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
设置BindingSource控件和ListBox控件数据绑定之后,就可以使用BindingSource控件的RemoveAt方法实现从ListBox控件删除数据项。清单4-14演示从ListBox控件删除数据项。
清单 4-14 删除ListBox数据项
<group id="_x0000_s1029" style="WIDTH: 414pt; HEIGHT: 117pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,2040"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1030" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 2040px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></shape><rect id="_x0000_s1031" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 2040px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">……</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">If BindingSource1.AllowRemove <> True Then<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 20.95pt; mso-char-indent-count: 2.33"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Windows.Forms.MessageBox.Show("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">不能删除列表数据项。</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">")<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Else<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 20.95pt; mso-char-indent-count: 2.33"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">BindingSource1.RemoveAt(BindingSource1.Find("Text", TextBox1.Text))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">End If<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">……</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
设置BindingSource控件和ListBox控件数据绑定之后,就可以使用BindingSource控件的Find方法实现从ListBox控件查找数据项。清单4-15演示从ListBox控件查找数据项。
清单 4-15 查找ListBox数据项
<group id="_x0000_s1026" style="WIDTH: 414pt; HEIGHT: 195pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,3400"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1027" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 3400px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></shape><rect id="_x0000_s1028" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 3400px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">……</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">If BindingSource1.SupportsSearching <> True Then<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 20.95pt; mso-char-indent-count: 2.33"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Windows.Forms.MessageBox.Show("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">不能查找列表。</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">")<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Else<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 20.95pt; mso-char-indent-count: 2.33"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim foundIndex As Integer = BindingSource1.Find("Text", TextBox1.Text)<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 20.9pt; mso-char-indent-count: 2.32"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">If foundIndex > -1 Then<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 20.9pt; mso-char-indent-count: 2.32"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>ListBox1.SelectedIndex = foundIndex<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 20.9pt; mso-char-indent-count: 2.32"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Else<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 20.9pt; mso-char-indent-count: 2.32"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Windows.Forms.MessageBox.Show("</font></s