首页 VB.NET中级篇

VB.NET中级篇

举报
开通vip

VB.NET中级篇实例53:获得拨号网络项目 实例53:获得拨号网络项目 实例说明 在本实例中,我们将制作一个能够取得本地计算机上的拨号网络的项目的应用程序。程序运行结果如图53-1所示。 图53-1 运行结果 技术要点 ​ 读取注册表内容 ​ 添加到列表框 实现过程 ■​ 新建项目 打开Visual Studio.NET,选择“新建项目”,在项目类型窗口中选择“Visual Basic项目”,在模板窗口中选择“Windows应用程序”,在名称域中输入“GetDialList”,然后选择保存路径。单击“确认”。 ■​ 添加控件和...

VB.NET中级篇
实例53:获得拨号网络项目 实例53:获得拨号网络项目 实例说明 在本实例中,我们将制作一个能够取得本地计算机上的拨号网络的项目的应用程序。程序运行结果如图53-1所示。 图53-1 运行结果 技术要点 ​ 读取注册 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 内容 财务内部控制制度的内容财务内部控制制度的内容人员招聘与配置的内容项目成本控制的内容消防安全演练内容 ​ 添加到列表框 实现过程 ■​ 新建项目 打开Visual Studio.NET,选择“新建项目”,在项目类型窗口中选择“Visual Basic项目”,在模板窗口中选择“Windows应用程序”,在名称域中输入“GetDialList”,然后选择保存路径。单击“确认”。 ■​ 添加控件和设置属性 向窗体上添加一个ListBox控件。将窗体的Text属性改为“获得拨号网络项目”,其余属性都保持默认。 ■​ 添加代码 Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA"(ByVal hKey As Integer, ByVal lpSubKey As String, ByRef phkResult As Integer) As Integer Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Integer) As Integer Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA"(ByVal hKey As Integer, ByVal dwIndex As Integer, ByVal lpName As String, ByVal cbName As Integer) As Integer Const HKEY_CURRENT_USER As Integer = &H80000001 Const ERROR_NO_MORE_ITEMS As Short = 259 Const ERROR_SUCCESS As Short = 0 Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load Dim hKey As Integer Dim i As Integer Dim astr As VB6.FixedLengthString = New VB6.FixedLengthString(256) If RegOpenKey(HKEY_CURRENT_USER, "RemoteAccess\Profile", hKey) = ERROR_SUCCESS Then While RegEnumKey(hKey, i, astr.Value, 256) = ERROR_SUCCESS List1.Items.Add(astr.Value) i = i + 1 End While RegCloseKey(hKey) End If End Sub ■​ 运行程序 单击菜单“调试|启动”或单击 图标运行程序。 小结 本实例通过读取注册表中有关拨号网络的项目,然后将这些项目添加到历表框中,这就是本程序的思路。 实例54:倒计时程序 实例说明 本实例将制作一个倒计时程序。程序运行时,始终位于在任何窗口的最前面,双击窗体结束应用程序。程序运行结果如图54-1所示。可以在窗体上单击鼠标右键,在弹出的菜单中选择“设置倒计时”,设置界面如图54-2所示。 图54-1 运行结果 图54-2 设置倒计时界面 技术要点 ​ 保持窗体最前 ​ 倒计时 ​ TimeSerial ()函数 ​ SetWindowPos()API函数 实现过程 ■​ 新建项目 打开Visual Studio.NET,选择“新建项目”,在项目类型窗口中选择“Visual Basic项目”,在模板窗口中选择“Windows应用程序”,在名称域中输入“ReverseCount”,然后选择保存路径。单击“确认”。 ■​ 添加控件、菜单和窗体 为当前窗体添加两个Label控件和两个Timer控件。其中一个Timer控件控制一个Label控件在上方一直循环移动;另一个Timer控件控制时间的显示。添加一个ContextMenu控件并新建菜单“设置倒计时”和“退出”。利用菜单“项目|添加Windows窗体”,为当前项目添加一个窗体Form2,给Form2添加四个Label控件,用于说明之用;两个Button按钮和三个TextBox控件。最后通过菜单“项目|添加模块”为当前项目添加一个模块。 ■​ 设置属性 切换到“属性栏”,对窗体及窗体上的控件进行设置属性。详细情况如表54-1和表54-2所示。 表54-1 窗体Form1及控件的属性值 窗体/控件 属性 值 Form1 FormBorderStyle FixedSingle ContextMenu ContextMenu1 Label1 Text 倒计时 Timer1 Interval 100 Timer2 Interval 1000 表54-2 窗体Form2及控件的属性值 窗体/控件 属性 值 Form2 BorderStyle 0-None StartUpPositon CenterScreen Text1 Text (空) Button1 Text 更改图片 其余控件 跟界面一致即可 ■​ 添加代码 Module Module1 ' 这是在模块中添加的代码 Public hours As Short Public minutes As Short Public seconds As Short Public revtime As Date '此API函数用来使某个窗口位于所有窗口之上 Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer End Module ' 下面这些代码是在窗体Form1中添加的 ' 双及窗体,结束应用程序 Private Sub Form1_DoubleClick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.DoubleClick Me.Close() End Sub ' 启动程序时,使程序位于任何窗体的上方 Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load Me.TopMost = True End Sub ' 开始倒计时,并显示出来 Private Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tick revtime = DateAdd(Microsoft.VisualBasic.DateInterval.Second, -1, revtime) Label2.Text = Format(revtime, "hh") & ":" & Format(revtime, "mm") & ":" & Format(revtime, "ss") End Sub Private Sub MenuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuItem1.Click Form2.DefInstance.ShowDialog() End Sub Private Sub MenuItem2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuItem2.Click Me.Close() End Sub ' 下面这些代码是在窗体Form2中添加的 Private Sub Button1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button1.Click hours = Val(Text1.Text) minutes = Val(Text2.Text) seconds = Val(Text3.Text) ' 将输入转化为时间格式 revtime = TimeSerial(hours, minutes, seconds) Form1.DefInstance.Label2.Text = Format(revtime, "hh") & ":" & Format(revtime, "mm") & ":" & Format(revtime, "ss") Form1.DefInstance.Timer1.Enabled = True Form2.DefInstance.Hide() End Sub Private Sub Button2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button2.Click Me.Close() End Sub ■​ 运行程序 单击菜单“调试|启动”或单击 图标运行程序。 小结 本实例介绍了一个倒计时程序,它的思路比较简单,就是获得用户输入的时间值,通过定时器每秒钟更新显示一次。它的应用比较广泛,读者可以更改其界面,使其变得更实用、更漂亮。 实例55:串行化数据 实例说明 在本实例中,我们将利用VB.NET向大家展示一个串行化数据的实例。程序运行结果如图55-1所示。 图55-1 运行结果 技术要点 ​ 串行化的概念 ​ 进行读写操作 实现过程 ■​ 新建项目 打开Visual Studio.NET,选择“新建项目”,在项目类型窗口中选择“Visual Basic项目”,在模板窗口中选择“类库”,在名称域中输入“SerializeTest”,然后选择保存路径。单击“确认”。 ■​ 添加代码 Imports System Imports System.IO Imports System.Collections Imports System.Runtime.Serialization Imports System.Runtime.Serialization.Formatters.Binary Namespace ClassLibrary1 Public Class SerializeTest Public Shared Sub Main() Console.WriteLine("Create object graph") Dim l As New ArrayList Dim x As Integer For x = 0 To 9 Console.WriteLine(x) l.Add(x) Next x Console.Write("Serializing object graph to disk..") Dim s As Stream = File.Open("foo.bin", FileMode.Create, FileAccess.ReadWrite) Dim b As BinaryFormatter = New BinaryFormatter b.Serialize(s, l) s.Close() Console.WriteLine("Complete.") Console.Write("Deserializing object graph from disk..") Dim r As Stream = File.Open("foo.bin", FileMode.Open, FileAccess.Read) Dim c As New BinaryFormatter Dim p As ArrayList = CType(c.Deserialize(r), ArrayList) Console.WriteLine("Complete.") Dim i As Object For Each i In p Console.WriteLine(i) Next i r.Close() Console.WriteLine(Microsoft.VisualBasic.ControlChars.CrLf & "Press Return to exit.") Console.Read() End Sub End Class End Namespace ■​ 运行程序 单击菜单“调试|启动”或单击 图标运行程序。 小结 串行化是把图式对象转换成为线性字节串行的过程。字节序列可以发送到别处,例如远程计算机,然后执行序列解码,这样就在远程主机内存中生成了原来图式对象的克隆。串行化过程在VB.NET中是十分容易使用的,而且是开放的,每个VB6的程序员都曾经自觉或不自觉地使用过串行化。当任何ActiveX dll或者ActiveX exe被创建后,都有五个属性可以设置,最后一个Persistable属性就是VB6中的串行化。VB6中串行化的过程不需要用户认知,大部分开发人员也从来不管串行化做了什么和如何运作。在VB.NET中,有着公共对象库,每个支持.NET体系的语言都可以通过System.Runtime.Serialization的名字空间来理解和使用VB.NET的串行化特征。 串行化可以替代任何流,象MemoryStream,NetStream等,而不仅仅是FileStream。 实例56:控制图形移动 实例说明 在本实例中,我们将使用VScrollBar控件和HScrollBar控件来控制图片的移动,我们还可以设置移动的间距大小。程序运行结果如图56-1所示。 图56-1 运行结果 技术要点 ​ 控制图片移动 ​ 直接拖动图片 ​ 更改ScrollBar控件的移动间距大小 实现过程 ■​ 新建项目 打开Visual Studio.NET,选择“新建项目”,在项目类型窗口中选择“Visual Basic项目”,在模板窗口中选择“Windows应用程序”,在名称域中输入“ScrollBarCtl”,然后选择保存路径。单击“确认”。 ■​ 添加控件 向当前窗体上添加一个GroupBox控件,在GroupBox控件上放置六个Label控件和两个ComboBox控件;然后再添加一个Picture控件,一个VScrollBar控件和一个HScrollBar控件和六个Label控件。按照图56-1的运行结果排列好位置。 ■​ 设置属性 对窗体上的控件进行属性设置。详细情况如表56-1所示。 表56-1 窗体各控件的属性值 窗体/控件 属性 值 PictureBox Picture Human.jpg ComboBox1 Items 1,2,3,4,5 ComboBox1 Itmes 5,10,20,50 其余控件 Text 跟界面一致 ■​ 添加代码 由于空间有限,我们仅列出主要代码,其余代码请看光盘。 ' 水平滚动条移动时,改变图片的位置 Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll label11.Text = HScrollBar1.Value.ToString() hAbsPos = CSng(HScrollBar1.Value - HScrollBar1.Minimum) pictureBox1.Left = HScrollBar1.Right - CInt(hScrollMultiplier * hAbsPos) - pictureBox1.Width End Sub ' 更改大间距移动的值 Private Sub comboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comboBox1.SelectedIndexChanged VScrollBar1.LargeChange = Convert.ToInt16(comboBox1.SelectedItem.ToString()) HScrollBar1.LargeChange = Convert.ToInt16(comboBox1.SelectedItem.ToString()) label11.Text = HScrollBar1.Value.ToString() label12.Text = VScrollBar1.Value.ToString() End Sub ' 更改小间距移动的值 Private Sub comboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comboBox2.SelectedIndexChanged VScrollBar1.SmallChange = Convert.ToInt16(comboBox2.SelectedItem.ToString()) HScrollBar1.SmallChange = Convert.ToInt16(comboBox2.SelectedItem.ToString()) label11.Text = HScrollBar1.Value.ToString() label12.Text = VScrollBar1.Value.ToString() End Sub ' 直接拖动图片 Private Sub pictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pictureBox1.MouseDown dragging = True ' (e.x, e.y) 表示光标相对于图片位置的相对坐标。我们需要将这个值保存下来 oldX = e.X oldY = e.Y End Sub ■​ 运行程序 单击菜单“调试|启动”或单击 图标运行程序。 小结 本实例通过使用ScrollBar控件来控制图片的显示,这在我们要显示大图片时是很有必要的。通过对本实例的学习,读者能够掌握ScroollBar控件的基本使用方法。 实例57:多彩文本 实例说明 在本实例中,我们制作一个能够显示多种形式文本的应用程序。程序运行后,即在窗体上的不同区域输出不同的文字。程序运行结果如图57-1所示。 图57-1 运行结果 技术要点 ​ 设定不同的Brush和Font ​ 输出字体 实现过程 ■​ 新建项目 打开Visual Studio.NET,选择“新建项目”,在项目类型窗口中选择“Visual Basic项目”,在模板窗口中,选择“Windows应用程序”,在名称域中输入“GdipText”,然后选择保存路径。单击“确认”。 ■​ 添加代码 Public Sub New() MyBase.New() TextSample = Me InitializeComponent() serifFontFamily = New FontFamily(GenericFontFamilies.Serif) Me.SetStyle(ControlStyles.Opaque, True) Me.SetStyle(ControlStyles.ResizeRedraw, True) Dim backgroundImage As Image '设定背景图片 backgroundImage = New Bitmap(System.Reflection.Assembly.GetExecutingAssembly(). GetManifestResourceStream("colorbars.jpg")) '新建一个画刷,我们将使用它在背景图片上画图 backgroundBrush = New TextureBrush(backgroundImage) '设定文本图片 Dim textImage As Image = New Bitmap(System.Reflection.Assembly.GetExecutingAssembly(). GetManifestResourceStream("marble.jpg")) textTextureBrush = New TextureBrush(textImage) '设定要使用字体格式 Me.Font = New Font(serifFontFamily, 20) titleFont = New Font(serifFontFamily, 60) textFont = New Font(serifFontFamily, 11) '建立一个阴影画刷 titleShadowBrush = New SolidBrush(Color.FromArgb(70, Color.Black)) '用设定的字体和画刷输出Japanese文本 Try japaneseFont = New Font("MS Mincho", 36) linearGradBrush = New LinearGradientBrush(New Point(0, 0), New Point(0, 45), Color.Blue, Color.Red) Catch ex As Exception MessageBox.Show("The Japanese font MS Mincho needs be present to run the Japanese part of this sample" & ControlChars.CrLf & "" & ControlChars.CrLf & "" + ex.Message) doJapaneseSample = False End Try End Sub Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics g.SmoothingMode = SmoothingMode.AntiAlias '用texture画刷填充背景,并应用到一个白画布上 g.FillRectangle(backgroundBrush, ClientRectangle) g.FillRectangle(New SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle) g.DrawString("欢迎大家来到VB.NET的世界!", Me.Font, New SolidBrush(Color.Black), 10, 10) Dim titleText As String = "图形示例" g.DrawString(titleText, titleFont, titleShadowBrush, 15, 25) g.DrawString(titleText, titleFont, textTextureBrush, 10, 20) Dim textToDraw As String = "银河文化公司" Dim windowCenter As Double = Me.DisplayRectangle.Width / 2 Dim stringSize As SizeF = g.MeasureString(textToDraw, textFont) Dim startPos As Double = windowCenter - (stringSize.Width / 2) g.DrawString(textToDraw, textFont, New SolidBrush(Color.Red), CType(startPos, Single), 10) Dim rectangle1 As RectangleF = New RectangleF(20, 150, 250, 300) g.FillRectangle(New SolidBrush(Color.Gainsboro), rectangle1) g.DrawString(flowedText1, textFont, New SolidBrush(Color.Blue), rectangle1) '输出居中文本 Dim rectangle2 As RectangleF = New RectangleF(450, 150, 250, 300) g.FillRectangle(New SolidBrush(Color.Gainsboro), rectangle2) Dim format As StringFormat = New StringFormat() format.Alignment = StringAlignment.Center g.DrawString(flowedText2, textFont, New SolidBrush(Color.Blue), rectangle2, Format) '统计输出的字符数和行数 Dim characters As Integer = 0 Dim lines As Integer = 0 g.MeasureString(flowedText2, textFont, rectangle2.Size, format, characters, lines) Dim whatRenderedText As String = "共输出了" + CType(characters, String) + " 字符和 " + CType(lines, String) + "行" g.DrawString(whatRenderedText, textFont, New SolidBrush(Color.Black), 400, 440) '旋转刚才输出的Japanese文本 If (doJapaneseSample) Then g.RotateTransform(-30) g.TranslateTransform(-180, 300) g.DrawString(japaneseText, japaneseFont, linearGradBrush, 200, 140) g.ResetTransform() End If End Sub ■​ 运行程序 单击菜单“调试|启动”或单击 图标运行程序。 小结 本实例使用不同的画刷(Brush)和字体(Font),在不同的区域输出不同的文字,并可以旋转文字,而并不需要使用API函数。 实例58:开发用户自定义控件 实例说明 在本实例中将自行制作一个用户自定义控件并在另外的一个应用程序中加以引用。程序运行后,可以更改其中的内容,然后单击save即可。程序运行结果如图58-1所示。 图58-1 运行结果 技术要点 ​ 定制用户控件界面 ​ 为用户控件添加属性和过程 ​ 编译用户控件 ​ 引用用户控件 实现过程 ■​ 新建项目 打开Visual Studio.NET,选择“新建项目”,在项目类型窗口中选择“Visual Basic项目”,在模板窗口中选择“Windows控件库”,在名称域中输入“CustomControl”,然后选择保存路径。单击“确认”。 ■​ 添加控件 向当前用户控件UserControl1上添加五个Label控件和五个TexbBox控件。 ■​ 设置属性 对用户控件UserControl1上的控件设置属性,如表58-1所示。 表58-1 各控件的属性值 窗体/控件 属性 值 UserControl BackColor &H00C00000 Text5 Text ScrollBars 2-Vertical MultiLine True Label1 Text ID Text1 Text 其余Label、TextBox控件 Text 跟界面一致 ■​ 添加组件类 通过菜单“项目|添加新项”,在弹出的对话框中选择“组件类”,在名称栏中输入Customer,单击“确定”。 ■​ 添加代码 在组件类的代码窗口下输入下列代码。 Imports System Imports System.Windows.Forms Imports System.Drawing Namespace Microsoft.Samples.WinForms.VB.CustomerControl Public Class CustomerControl Inherits System.Windows.Forms.UserControl Private customer1 As Customer Public Sub New() MyBase.New() ' Required by the Windows Forms Designer InitializeComponent() ' TODO: Add any constructor code after InitializeComponent call End Sub Public Property Customer() As Customer Get Return customer1 End Get Set(ByVal Value As Customer) customer1 = Value LoadCustomer() End Set End Property Public Sub AcceptChanges() customer1.Title = textBoxTitle.Text customer1.FirstName = textBoxFirstName.Text customer1.LastName = textBoxLastName.Text customer1.Address = textBoxAddress.Text End Sub Public Sub RejectChanges() LoadCustomer() End Sub Private Sub LoadCustomer() textBoxID.Text = customer1.ID textBoxTitle.Text = customer1.Title textBoxFirstName.Text = customer1.FirstName textBoxLastName.Text = customer1.LastName textBoxAddress.Text = customer1.Address End Sub Public Overloads Overrides Sub Dispose() MyBase.Dispose() End Sub End Class End Namespace ■​ 测试用户控件 右键单击“解决 方案 气瓶 现场处置方案 .pdf气瓶 现场处置方案 .doc见习基地管理方案.doc关于群访事件的化解方案建筑工地扬尘治理专项方案下载 ”,选择“添加|新建项目”,为解决方案添加一个测试用户控件的项目。并在窗体上添加一个CustomerControl控件和两个Button控件。 ■​ 添加测试代码 '这些必不可少 Imports System Imports System.ComponentModel Imports System.Drawing Imports System.Windows.Forms Imports Microsoft.Samples.WinForms.VB.CustomerControl Imports Microsoft.VisualBasic.ControlChars Namespace Microsoft.Samples.WinForms.VB.HostApp Public Class HostApp Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() HostApp = Me 'This call is required by the Windows Forms Designer. InitializeComponent() 'TODO: Add any initialization after the InitializeComponent() call CustomerControl1.Customer = Customer.ReadCustomer() ' Set the minimum form size to the client size + the height of the title bar Me.MinimumSize = New Size(400, (373 + SystemInformation.TextHeight)) End Sub 'Form overrides dispose to clean up the component list. Public Overloads Overrides Sub Dispose() MyBase.Dispose() components.Dispose() End Sub Private Sub buttonCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonCancel.Click CustomerControl1.RejectChanges() End Sub Private Sub buttonSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonSave.Click CustomerControl1.AcceptChanges() MessageBox.Show("Customer Changes Saved: " & CrLf & CustomerControl1.Customer.ToString) End Sub Shared Sub Main() Application.Run(New HostApp()) End Sub End Class 'HostApp End Namespace ■​ 运行程序 单击菜单“调试|启动”或单击 图标运行程序。 小结 我们在本实例中自制了一个AcitveX控件,此控件具有定时提示的作用。读者可以自行开发控件,最后将该控件发布即可。 实例59:连接数据库(一) 实例说明 下面,我们将利用VB.NET自行创建一个连接数据库的应用程序,其运行结果如图59-1所示。 图59-1 运行结果 技术要点 ​ 介绍ADO.NET ​ SqlConnection和SqlCommand 实现过程 ■​ 新建项目 打开Visual Studio .NET7.0,选择“新建项目”,在项目类型窗口中,选择“Visual Basic项目”,在模板窗口中,选择“ASP.NET Web应用程序”,在名称域中输入“adooverview3”,然后选择保存路径。单击确认。 ■​ 添加代码 Imports System Imports System.Data Imports System.Data.SqlClient namespace HowTo.Samples.ADONET public class adooverview3 public shared sub Main() Dim myadooverview3 as adooverview3 myadooverview3 = new adooverview3() myadooverview3.Run() end sub public sub Run() Dim myReader as SqlDataReader Dim mySqlConnection as SqlConnection Dim mySqlCommand as SqlCommand '设置数据库的连接 mySqlConnection = new SqlConnection("server=(local)\NetSDK;uid=QSUser; pwd=QSPassword;database=northwind") mySqlCommand = new SqlCommand("select * from customers", mySqlConnection) try '输出字段名称 mySqlConnection.Open() myReader = mySqlCommand.ExecuteReader() Console.Write("Customer ID ") Console.WriteLine("Company Name") '输出纪录 do while (myReader.Read()) Console.Write(myReader("CustomerID").ToString() + " ") Console.WriteLine(myReader("CompanyName").ToString()) loop catch e as Exception Console.WriteLine(e.ToString()) finally if Not (myReader is Nothing) myReader.Close() end if if (mySqlConnection.State = ConnectionState.Open) mySqlConnection.Close() end if end try end sub end class end namespace ■​ 运行程序 打开“开始|程序|Microsoft Visual Studio.NET7.0|Visual Studio.NET Tools|Visual Studio.NET Command Prompt”,运行之后输入vbc adooverview3.vb。即可生成可执行文件。 小结 ADO.NET是ADO数据控制模型的升级版本,它是为了适应Web不断增长的规模而设计的。ADO.NET仍然使用以前的ADO对象,像Connetion和Command对象,同时还增加了一些新的ADO.NET对象,如DataSet、DataReader和DataAdapter等。 ADO.NET和以前ADO数据结构最大的不同就在于——DataSet对象,他跟以前任何的数据存储都不同。正因为如此,DataSet函数作为一个独立的实体而存在。在DataSet内部,包括像表、关系、限制、视图等等跟一个数据库一样。 实例60:打印和打印预览功能 实例说明 在本实例中,我们将制作一个能实现打印和打印预览功能的应用程序。程序运行结果如图60-1所示。 图60-1 运行结果 技术要点 ​ 打印预览功能的实现 ​ 打印功能的实现 实现过程 ■​ 新建项目 打开Visual Studio.NET,选择“新建项目”,在项目类型窗口中选择“Visual Basic项目”,在模板窗口中选择“Windows应用程序”,在名称域中输入“PrintExample”,然后选择保存路径。单击“确认”。 ■​ 添加控件和设置属性 向当前窗体上添加三个Button控件,将他们的Text属性改为和界面一致。 ■​ 添加组件类和要打印的文件 通过菜单“项目|添加组件”为当前项目添加一个组件类,并添加一个需要打印的文件。 ■​ 添加代码 '组件类中的代码 Imports System Imports System.ComponentModel Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Printing Imports System.IO Namespace Microsoft.Samples.WinForms.VB.PrintingExample5 Public Class TextFilePrintDocument Inherits PrintDocument Private printFont As Font Private streamToPrint As StreamReader Public Sub New(streamToPrint As StreamReader) MyBase.New Me.streamToPrint = streamToPrint End Sub 'Override OnBeginPrint to set up the font we are going to use Overrides Protected Sub OnBeginPrint(ev As PrintEventArgs) MyBase.OnBeginPrint(ev) printFont = new Font("Arial", 10) End Sub 'Override the OnPrintPage to provide the printing logic for the document Overrides Protected Sub OnPrintPage(ev As PrintPageEventArgs) MyBase.OnPrintPage(ev) Dim lpp As Single = 0 Dim yPos As Single = 0 Dim count As Integer = 0 Dim leftMargin As Single = ev.MarginBounds.Left Dim topMargin As Single = ev.MarginBounds.Top Dim line as String 'Work out the number of lines per page 'Use the MarginBounds on the event to do this lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics) 'Check count first so that we don't read line that we won't print line=streamToPrint.ReadLine() While ((count < lpp) And Not(line Is Nothing)) yPos = topMargin + (count * printFont.GetHeight(ev.Graphics)) ev.Graphics.DrawString (line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat()) count = count + 1 if (count < lpp) then line=streamToPrint.ReadLine() end if End While If (line <> Nothing) Then ev.HasMorePages = True Else ev.HasMorePages = False End If End Sub End Class End Namespace '主窗体中的代码 Imports System Imports System.ComponentModel Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Printing Imports System.IO Namespace Microsoft.Samples.WinForms.VB.PrintingExample5 Public Class PrintForm Inherits System.Windows.Forms.Form Private storedPageSettings As PageSettings Public Sub New () MyBase.New PrintForm = Me InitializeComponent() AddHandler printButton.Click, AddressOf printButton_Click AddHandler pageSetupButton.Click, AddressOf pageSetupButton_Click AddHandler printPreviewButton.Click, AddressOf printPreviewButton_Click End Sub '页面设置 Private Sub pageSetupButton_Click(sender As object, e As System.EventArgs) Try Dim psDlg As New PageSetupDialog If (storedPageSettings Is Nothing) Then storedPageSettings = new PageSettings() End If psDlg.PageSettings = storedPageSettings psDlg.ShowDialog Catch ex As Exception MessageBox.Show("An error occurred - " + ex.Message) End Try End Sub '开始打印 Private Sub printButton_Click(sender As object, e As System.EventArgs) Try Dim streamToPrint As StreamReader = new StreamReader ("PrintMe.Txt") Try '使用缺省打印机 Dim pd As TextFilePrintDocument = new TextFilePrintDocument(streamToPrint) If Not (storedPageSettings Is Nothing) Then pd.DefaultPageSettings = storedPageSettings End If Dim dlg As New PrintDialog() dlg.Document = pd Dim result As Dial
本文档为【VB.NET中级篇】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_962853
暂无简介~
格式:doc
大小:944KB
软件:Word
页数:59
分类:互联网
上传时间:2011-04-06
浏览量:35