【VB开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。
网学会员整理了VB开源代码-VB NET程序代码--第13章综合实例 - 培训资料的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
13 13.1 13.1.1 1.任务 设计一个如图13-1所示的统计图设计器,要求该设计器能够根据数据库中所存数据,生成相应的折线图、直方图等多种统计图表。
图13-1统计图设计器 2.界面设计 界面设计如图13-2所示。
在窗体上放置1个MSChart定制控件、1个DataGrid控件和8个按钮,其Text属性设置如表13-1所示。
13.1.2MSChart MSChart是一个以图形方式显示数据的图表。
在窗体中加入MSChart(Name属性及为AMSC)控件后的画面如图13-3所示。
图中,R1,R2,R3,??表示数据的项目,每一个项目包含多个数据。
图的垂直方向为y轴,水平方向为x轴。
图中y轴方向的两侧均标有数据,为区别起见,右侧称为y1轴,左侧称为y2轴。
图13-2界面设计 图13-3MSChart控件 对MSChart控件的添加方法说明如下: 在【工具箱】中的【Windows窗体】上点击右键,选中弹出菜单的【添加/移除项】选项,将弹出的【自定义工具栏】对话框,选中【COM组件】选项卡,在下面的
列表框中选中“Microsoft Chart Control 6.0”再点击【确认】按钮,然后在【Windows窗体】上双击“Microsoft Chart Control 6.0”图标,即可往Form中添加MSChart控件。
表13-1Text属性设置值 Text Button1 Button1 输入数据 Button2 Button2 折线图 Button3 Button3 立体折线图 Button4 Button4 直方图 Button5 Button5 立体直方图 Button6 Button6 立体域图 Button7 Button7 饼图 Button8 Button8 结束 控件的主要属性说明如下: 1 RowCount属性和ColumnCount属性:这两种属性和RowCount属性用于统计图的数据设定。
一旦在窗体上配置了AMSC控件,则自动生成一个数据表,用于存放数据。
AMSC控件的数据是一个按项目排列的二维表。
初始
设计时数据表中填入的是一组随机数据,用于生成范例表。
数据表为 R1:4168320 R2:70247858 R3:6264546 R4:81266291 R5:95422836 其中,第1行、第1列的数据是41,对应于R1的第1个条形。
程序运行时,这些随机数将被实际数据所代替。
在程序设计中,必须使用AMSC控件的ColumnCount属性和RowCount属性属性预先设定与统计图显示数据相对应的数据表的整体结构。
ColumnCount属性设置数据表列总数,对应于统计图中每组数据的个数;RowCount属性设置数据表行总数,对应于统计图中的数据组数。
例如, AMSC.Columncount5 AMSC.RowCount10 设定了一个每组显示5个数据、最多10组的统计图的数据表整体结构。
同样,也可以在界面设计时利用AMSC控件的〔属性〕窗口设置这两个属性的属性值。
在
程序设计中,给数据表设定数据有两种方法,一种方法是使用AMSC的Row、Column和Data属性按行、列直接赋值。
例如, AMSC.ROw2 AMSC.Column3 AMSC.Data100 表示数据表第2行、第3列的数据是100。
第二种方法是使用数组设定数据。
该方法首先用一个二维数组设定数据,然后再送给图形控件,当然也可以送给数据表。
例如, Dim AMSCData1 to 101 to 5 As Integer AMSCData 11 11 AMSCData 1212 ?? AMSCData 105105 AMSC.ChartData AMSCData 2 RowLabel属性和ColumnLabel属性:这两种属性和ColumnLabel属性可以设定统计图中数据系列的分类标签。
例如,第1行的行标签为“冰箱”,第1列的列标签为“星期一”,在程序中可以写为 AMSC.Row1 AMSC.RowLabelquot冰箱quot AMSC.Column1 AMSC.ColumnLabelquot星期一quot 3 RowLabelCount和RowLabelIndex属性:一般而言,行、列标签只使用一行就可以了,但也可以使用多行,此时就需要使用AMSC控件的RowLabelCount和RowLabelIndex属性。
例如,设定行标签为两段时的
代码示例如下。
AMSC.RowLabelCount2 AMSC.Row1 AMSC.RowLabelIndex1 AMSC.RowLabelquot冰箱quot AMSC.RowLabelIndex2 AMSC.RowLabel“月销售量” 这里,AMSC.RowLabelIndex为指定多个行标签的索引。
利用同样的方法,使用ColumnLabelCount和ColumnLabelIndex属性可以处理多列标签。
4 TitleText,FootNoteText和ShowLegend属性:统计图中图题、脚注和图标示例的表示可以使用TitleText、FootNoteText和ShowLegend属性。
例如, AMSC.TitleTextquot产品销售量统计图quot AMSC.FootNoteTextquot2006年11月13日quot AMSC.ShowLegendTrue ′显示图标示例 5 ChartType属性:使用AMSC控件可以绘制二维直方图、三维直方图、二维折线图、二维饼图等12种内定的统计图表,使用ChartType属性可以设置显示图表的类型。
格式为 object.AChartType〔type〕 其中,object为对象名;type为Integer,描述图表类型的VtChChartType常数。
VtChChartType常数说明如表13-2所示。
表13-2VtChChartType常数说明 0VtChChartType3dBar 三维直方图 1VtChChartType2dBar 二维直方图 2VtChChartType3dLine 三维折线图 3VtChChartType2dLine 二维折线图 4VtChChartType3dArea 三维面积图 5VtChChartType2dArea 二维面积图 6VtChChartType3dStep 三维累计图 7VtChChartType2dStep 二维累计图 8VtChChartType3dCombination 三维组合图 9VtChChartType2dCombination 二维组合图 14VtChChartType2dPie 二维饼图 16VtChChartType2dXY 二维XY散点图 6 Size和BorderStyle属性:Size属性可以设置控件的高度和宽度,单位为像素。
BorderStyle属性可以设置控件的边框形态。
BorderStyle属性值如表13-3所示。
表13-3BorderStyle属性值设置说明 xxxNone 0 无(没有边框或与边框相关的元素) xxxFixedSingle 1 固定单线边框 13.1.3DataGrid 一般而言,AMSC控件与数据网格DataGrid对象相关连,可以用数据网格显示图表中的数据。
数据网格中包括图表化的数据表和用于在图表中标识系列和分类的标签。
设计人员通过数据表或数组插入或输入数据给数据网格填充信息。
DataGrid对象被配置为行和列。
可以向该矩阵添加和减去行、列和标签以更改图表的外观。
1BackColor对象:该对象设置偶数行的背景色。
2BackgroundColor对象:该对象设置网格中非行区域的背景颜色。
3BorderStyle对象:该对象设置边框样式。
4DataSouce对象:该对象设置网格所显示数据的数据源。
5Font对象:该对象设置显示的文字字体。
6Height对象:该对象设置控件高度。
7Item对象:该对象设置指定单元格值。
8Size对象:该对象设置控件的高度和宽度。
9Visible对象:该对象设置是否显示该控件。
10VisibleColumnCount对象:该对象设置可见列的数目。
11VisibleRowCount对象:该对象设置可见行的数目。
12Width对象:该对象设置控件的宽度。
13.1.4DataSet DataSet控件是ADO.
NET中的重要控件,它是从数据库中检索到的数据在内存中的缓存。
1.DataSet控件主要属性 (1)DataSetName属性:该属性设置当前DataSet控件的名称。
(2)HasErrors属性:该属性获取指示此DataSet控件的任何表的任何行中是否有错误值。
(3)NameSpace属性:该属性设置DataSet控件的命名空间。
(4)Prefix属性:该属性设置XML前缀。
(5)Tables属性:该属性获取包含在DataSet控件中的表的集合。
2.DataSet控件主要方法 (1)Clear方法:该方法清除DataSet控件。
(2)Copy方法:该方法复制DataSet控件的结构和数据。
(3)ReadXml方法:该方法将XML架构和数据读入DataSet控件。
(4)WriteXml方法:该方法从DataSet控件写XML数据,也可以选择写架构。
DataSet控件由一组DataTable对象组成,可以使这些对象与DataRelation对象互相关联,也可以通过使用UniqueConstraint和ForeignKeyConstraint对象在DataSet中实施数据完整性。
尽管DataTable对象中包含数据,但是DataRelationCollection允许遍览表的层次结构,这些表包含在通过Tables属性访问的DataTableCollection中。
当访问DataTable对象时,注意它们是按条件区分大小写的。
例如,如果一个DataTable被命名为“mydatatable”,另一个被命名为“Mydatatable”,则用于搜索其中一个表的字符串被认为是区分大小写的。
但如果“mydatatable”存在而“Mydatatable”不存在,则认为该
搜索字符串不区分大小写。
DataSet控件将数据和架构作为XML
文档形式进行读/写。
数据和架构可以通过HTTP传输,并在启用XML的任何平台上被任何应用程序使用。
使用WriteXmlSchema方法可以将该架构保存为XML架构,并且可以使用WriteXml方法保存架构和数据。
若要读取既包含架构也包含数据的XML
文档,可以使用ReadXml方法。
创建和刷新DataSet控件,并依次更新原始数据的步骤如下: ①使用SqlDataAdapter或OleDbDataAdapter,用数据源中的数据构造和填充DataSet控件中的每个DataTable对象。
②通过添加、更新或删除DataRow对象,更改单个DataTable对象中的数据。
③调用GetChanges方法,创建只反映对数据进行更改的第2个DataSet控件。
④调用SqlDataAdapter或OleDbDataAdapter的Update方法,并将第2个DataSet控件作为参数传递。
⑤调用Merge方法将第2个DataSet控件中的更改合并到第1个中。
13.1.5 1.输入数据 Private Sub btn1_ClickByVal sender As System.Object ByVal e As System.EventArgs Handles btn1.Click Dim rows cols r c As Short Dim a4 As String a1quot2000年quot a2quot2001年quot a3quot2002年quot a4quot2003年quot OleDbConnection1.Open OleDbDataAdapter1.FillDataSet11quotTABLE1quot DataGrid1.DataSourceDataSet11 rowsDataSet11.TABLE1.Rows.Count colsDataSet11.TABLE1.Columns.Count AMSC.RowCountrows AMSC.ColumnCountcols AMSC.Tit-leTextDataSet11.TABLE1.TableName.ToString AMSC.ShowLegendTrue For r1 To AMSC.RowCount AMSC.Rowr AMSC.RowLabelar For c1 To AMSC.ColumnCount AMSC.Columnc AMSC.ColumnLabelDataSet11.TABLE1.Columns c- 1 .ColumnName AMSC.DataDataSet11.TABLE1.Rowsr- 1c-1.ToString Next c Next r 图13-4折线图 OleDbConnection.Close End Sub 2.折线图 Private Sub btn2_ClickByVal sender As System.Object ByVal e As System.EventArgs Handles btn2.Click AMSC.chartTypeMSChart20Lib.VtChChartType.VtChChartType2dLine End Sub 折线图如图13-4所示。
3.立体折线图 Private Sub btn3_ClickByVal sender As System.Object ByVal e As System.EventArgs Handles btn3.Click AMSC.chartTypeMSChart20Lib.VtChChartType.VtChChartType3dLine End Sub 立体折线图如图13-5所示。
图13-5立体折线图 4.直方图 Private Sub btn4_ClickByVal sender As System.ObjectByVal e As System.EventArgsHandles btn4.Click AMSC.chartTypeMSChart20Lib.VtChChartType.VtChChartType2dBar End Sub 5.立体直方图 Private Sub btn5_ClickByVal sender As System.Object ByVal e As System.EventArgs Handles btn5.Click AMSC.chartTypeMSChart20Lib.VtChChartType.VtChChartType3dBar End Sub 立体直方图如图13-6所示。
图13-6立体直方图 6.立体域图 Private Sub btn6_ClickByVal sender As System.Object BvVal e As System.EventArgs Handles btn6.Click AMSC.chartTypeMSChart20Lib.VtChChartType.VtChChartType3dArea End Sub 立体域图如图13-7所示。
图13-7立体域图 7.饼图 Private Sub btn7_ClickByVal sender As System.Object ByVal e As System.EventArgs Handles Btn7.Click AMSC.chartTypeMSChart20Lib.VtChChartType.VtChChartType2dPie End Sub 饼图如图13-8所示。
图13-8饼图 8.结束 Private Sub btn8_ClickByVal sender As System.Object ByVal e As System.EventArgs Handles btn8.Click Application.Exit End Sub 13.2 随着信息时代的到来,人们越来越看重信息的交流,在交流之时,需要记住许多联系电话以及住址等。
为了解决记电话的烦恼制作一个软件系统来帮助管理电话号码簿是非常必要的,用以帮助解决朋友或亲人的联系电话号码信息的添加、修改、删除以及有着方便的浏览查看功能。
13.2.1 根据需求分析规划出系统的基本功能如下: 图13-9登录窗体 ① 系统登录:通过用户名和密码来判断是否为合法用户,以保护
系统的合法使用,登录窗体如图13-9所示。
② 查询功能:根据任意信息提供模糊查询的功能。
③ 添加功能:向系统中添加友人的联系电话及附注信息。
④ 修改功能:完成对系统中错误信息的修改。
⑤ 删除功能:删除系统中原有的联系人信息。
13.2.2 1.数据库设计:共有2个表:一个用来保存通讯信息的数据库表“通讯录”,另一个为存放
软件用户信息的数据库表“用户表”。
通讯录包括编号、姓名、性别、家庭电话、办公电话、手机、QQ、E-mail、地址等信息;用户表包括用户名,密码。
2.界面设计:登录窗体如图13-9所示。
主窗体如图13-10所示。
图13-10主窗体
查询窗体如图13-11所示。
图13-11查询窗体 添加窗体如图13-12所示。
图13-12添加窗体 修改窗体如图13-13所示。
图13-13修改窗体 删除窗体如图13-14所示。
图13-14删除窗体 13.2.3 1.登录窗体的
代码 Dim blnok As BooleanTrue Private Sub btn2_Click ByVal sender As System.Object ByVal e As System.EventArgs Handles btn2.Click Application.Exit End Sub Private Sub btn1_Click ByVal sender As System.Object ByVal e As System.EventArgs Handles btn1.Click Dim strsql As String strsqlquotselect用户名,密码from用户表where用户名′quot amp txt1.Text ampquot′and密码′quot amp txt2.Text amp quot′quot Dim myconn As New OleDbConnection Dim mycomm As New OleDbCommand Dim myadpt As New OleDbDataAdapter Dim myds As New DataSet Try ′判断用户名和密码是否为空 If txt1.TextquotquotOr txt2.Textquot quotThen MsgBoxquot用户名或密码不能为空quot txt1.Focus txt1.SelectAll txt2.TextNothing Exit Sub End If ′如果用户输入的数据都不为空,到数据库中去查找输入的用户名和密码是否正确 myconn.ConnectionStringquotProviderMicrosoft.Jet.OLEDB.4.0 Data Source通讯录.mdbPersist Security InfoFalsequot myconn.Open mycomm.CommandTextstr
sql mycomm.Connectionmyconn myadpt.SelectCommandmycomm myadpt.Fillmyds Dim mytable As New DataTable mytablemyds.Tables0 If mytable.Rows.Count0 Then MsgBoxquot用户名或密码输入错误请重新输入!quotMsgBoxStyle.Information txt1.Focus txt1.SelectAll txt2.Textquot quot Else blnokTrue Me.Close End If Catch ex As Exception MsgBoxex.ToString Finally myconn.Close End Try End Sub Private Sub Form5_ClosingByVal sender As Object ByVal e As System.Component_Model.CancelEventArgs Handles MyBase.Closing If blnokFalse Then e.CancelTrue End If End Sub 2.查询窗体的
代码 Private Sub Chb1_CheckedChanged ByVal sender As System.Object ByVal e As System.EventArgs Handles Chb1.CheckedChanged If Chb1.CheckedTrue Then ComboBox1.EnabledTrue Else ComboBox1.EnabledFalse ComboBox1.Textquot quot End If End Sub Private Sub Chb2_CheckedChanged ByVal sender As System.Object ByVal e As System.EventArgs Handles Chb2.CheckedChanged If Chb2.Checked True Then ComboBox2.Enabled True Else ComboBox2.Enabled False ComboBox2.Text quotquot End If End Sub Private Sub Chb3_CheckedChangedByVal sender As System.Object ByVal e As System.EventArgs Handles Chb3.CheckedChanged If Chb3.Checked True Then ComboBox3.Enabled True Else ComboBox3.Enabled False ComboBox3.Text quotquot End If End Sub Private Sub Chb4_CheckedChanged ByVal sender As System.Object ByVal e As System.EventArgs Handles Chb4.CheckedChanged If Chb4.Checked True Then ComboBox4.Enabled True Else ComboBox4.Enabled False ComboBox4.Text quotquot End If End Sub Private Sub Chb5_CheckedChanged ByVal sender As System.Object ByVal e As System.EventArgs Handles Chb5.CheckedChanged If Chb5.Checked True Then ComboBox5.Enabled True Else ComboBox5.Enabled False ComboBox5.Text quot quot End If End Sub Private Sub Chb6_CheckedChanged ByVal sender As System.Object ByVal e As System.EventArgs Handles Chb6.CheckedChanged If Chb6.Checked True Then ComboBox6.Enabled True Else ComboBox6.Enabled False ComboBox6.Text quot quot End If End Sub Private Sub Form1_LoadByVal sender As System Object ByVal e As System.EventArgs Handles MyBase.Load Dim myconn As New OleDb.OleDbConnection Dim mycomm As New OleDbCommand Dim myadpt As New OleDbDataAdapter Dim myds As New DataSet myconn.ConnectionString quotprovider microsoft jet.oledb.4.0 data source通讯录.mdbquot myconn.Open ′填充姓名 mycomm.CommandText quotselect姓名 from通讯录quot mycomm.Connection myconn myadpt.SelectCommand mycomm myadpt.Fill myds Dim mytable As New DataTable mytable myds.Tables0 Dim i As Integer ComboBox1.Items.Clear For i 0 To mytable.Rows.Count - 1 ComboBox1.Items.Add mytable.Rowsi 0 Next ′填充手机 myds.Reset mycomm.CommandText quotselect手机 from通讯录quot mycomm.Connection myconn myadpt.SelectCommand mycomm myadpt.Fillmyds mytable mvds.Tables0 ComboBox3.Items.Clear For i0 To mytable.Rows.Count - 1 ComboBox3 Items.Addmytable.Rowsi 0 Next ′填充加电 myds.Reset mycomm.CommandText quotselect家庭电话from通讯录quot mycomm.Connection myconn myadpt.SelectCommand mycomm myadpt.Fill myds mytable myds.Tables 0 ComboBox4.Items.Clear For i 0 To mytable.Rows.Count - 1 ComboBox4.Items.Add mytable.Rowsi 0 Next ′填充QQ myds.Reset mycomm.CommandText quotselect qq from通讯录quot mycomm.Connection myconn myadpt.SelectCommand mycomm myadpt.Fillmyds mytable myds.Tables0 ComboBox5.Items.Clear For i 0 To mytable.Rows.Count -1 ComboBox5.Items.Addmytable.Rowsi 0 Next ′填充Email myds.Reset mycomm.CommandTextquotselect〔 Email〕from通讯录quot mycomm.Connection myconn myadpt.SelectCommand mycomm myadpt.Fill myds mytable myds.Tables0 ComboBox6.Items.Clear For i 0 To mytable.Rows.Count - 1 ComboBox6.Items.Add mytable.Rows i 0 Next myconn.Close End Sub Private Sub btn1_Click ByVal sender As System.Object ByVal e As System.EventArgs Handles Button1.Click Dim strsq1 As String If Chb1.Checked True Then strsqlquot姓名like′quot amp ComboBox1.Text amp quot′ andquot End If If Chb2.Checked True Then strsq1 strsql ampquot性别like′quot amp ComboBox2.Text amp quot′ andquot End If If Chb3.Checked True Then strsq1 strsq1 ampquot手机like′quot amp ComboBox3.Text amp quot′andquot End If If Chb4.Checked True Then strsq1strsq1 ampquot家庭电话like′quot amp ComboBox4.Textamp quot′andquot End If IfChb5.Checked True Then strsq1 strsq1 ampquot qq like′quot amp ComboBox5.Text amp quot′andquot End If If Chb6.Checked True Then strsq1 strsq1 ampquot〔 Email〕like′quot amp ComboBox6 Text amp quot′andquot End If If strsq1 ltgt quot quot Then strsq1strsq1.Substring0 strsq1.Length - 4 strsq1 strsq1.Replace quotquot quotquot strsq1 strsq1.Replacequot?quotquot_quot End If Dim myconn As New OleDbConnection Dim mycomm As New OleDbCommand Dim myadpt As New OleDbDataAdapter Dim myds As New DataSet myconn.ConnectionString quotprovidermicrosoft.jet.oledb.4.0 data source 通讯录.mdbquot myconn.Open If strsq1 quot quotThen mycomm.CommandText quotselectfrom通讯录quot Else mycomm.CommandText quotseleet from通讯录wherequot amp strsq1 End If mycomm.Connection myconn myadpt.SelectCommand mycomm myadpt.Fillmyds myconn.Close DataGrid1.DataSource myds.Tables0 End Sub Private Sub ComboBox1_SelectedIndexChangedByVal sender As System.Object ByVal e As System.EventArgs Handles ComboBox1.SelectedindexChanged btn1_Click sender e End Sub 3.添加窗体的
代码 Private Sub Form3_Load ByVal sender As System.Object ByVal e As System.EventArgs Handles MyBase.Load Dim myconn As New OleDbConnection Dim mycomm As New OleDbCommand Dim myadpt As New OleDbDataAdapter Dim myds As New .