精品文档 str = \请输入第\个整数\a(i) = InputBox(str) next
Public Class Form1
Public a(10) As Integer
Function qiuhe(ByVal c As Integer) As Long Dim i As Integer For i = 1 To 10 c += a(i) Next Return c End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i, m As Integer Dim str As String For i = 1 To 10
str = \请输入第\ + i.ToString + \个整数\ a(i) = InputBox(str) Next
m = qiuhe(m)
TextBox1.Text = \和=\ + m.ToString
End Sub End Class
第5题 计算长方形的面积和周长
设计如下的窗体,向前两个文本框中分别输入长和宽,单击“计算”,在另外两个文本框中显示长方形的面积和周长,要求采用SUB 函数过程计算面积和周长,长和宽通过参数传递,面积和周长通过引用传递返回。
精品文档
其中计算面积的SUB过程参考代码如下:
Sub area(ByVal height As Integer, byval width as integer,byref s as integer,byref l as integer) Dim s as integer S=height*width L= (height+width)*2 End Sub 完整程序:
Public Class Form1
Sub area(ByVal height As Integer, ByVal width As Integer) Dim s As Integer Dim l As Integer s = height * width
l = (height + width) * 2 TextBox3.Text = s.ToString TextBox4.Text = l.ToString End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a, b As Integer a = TextBox1.Text b = TextBox2.Text area(a, b) End Sub End Class
试验十一 第1题 画图
参考如下的代码,在窗体中绘制不同的图形,绘制时可以采用不同的工具、颜色和线宽。要求:
绘制的图形在颜色、位置、宽度等方面与例题不要完全相同。
在绘图中应明确下列的问题:
精品文档 (1)不同工具的定义方法 (2)所使用的坐标系 (3)所绘图形的位置 (4)不同图形的绘制方法。
Public Class Form1
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g As Graphics = e.Graphics
Dim pengreen As Pen = New Pen(Color.Green)
Dim penyellow As Pen = New Pen(Color.Yellow, 5)
Dim brushSolid As SolidBrush = New SolidBrush(Color.DarkGreen) Dim fontMyfavor As Font = New Font(\幼圆\, 26, FontStyle.Italic) Dim ptP1 As Point = New Point(12, 12) Dim ptP2 As Point = New Point(50, 90) g.DrawLine(penyellow, 55, 55, 224, 99) g.DrawLine(pengreen, ptP1, ptP2)
g.DrawRectangle(pengreen, 55, 200, 100, 100) g.DrawEllipse(penyellow, 0, 0, 300, 200) g.DrawEllipse(pengreen, 300, 0, 400, 300)
g.FillEllipse(brushSolid, 155, 200, 100, 100) g.DrawEllipse(pengreen, 300, 0, 400, 300)
g.DrawString(\欢迎来到西安交通大学!\, fontMyfavor, brushSolid, 0, 400) End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub End Class
第2题 创建菜单
(1)使用工具栏中的菜单控件创建菜单,主菜单中包含3项,分别是“画图”、“输出文字”和“结束”,“画图”和“输出文字”的下拉菜单内容如下:
(2)为“画图”菜单中每一条命令编写事件代码,可参考上一题中的部分结果自行编写。
精品文档
(3)“输出文字”菜单中两个命令的执行结果分别如下:
这两部分的代码和“结束”菜单的代码可参考下面的代码:
Public Class Form1
Private Sub 画直线ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 画直线ToolStripMenuItem.Click Dim g As Graphics g = CreateGraphics()
Dim penred As Pen = New Pen(Color.Red) g.DrawLine(penred, 30, 30, 120, 120) End Sub
Private Sub 画矩形ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 画矩形ToolStripMenuItem.Click Dim g As Graphics g = CreateGraphics()
Dim pengreen As Pen = New Pen(Color.Green) g.DrawRectangle(pengreen, 55, 100, 100, 100)
End Sub
Private Sub 画椭圆ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 画椭圆ToolStripMenuItem.Click Dim g As Graphics g = CreateGraphics()
Dim penblack As Pen = New Pen(Color.Black) g.DrawEllipse(penblack, 80, 80, 100, 150)
End Sub
Private Sub 问候语ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 问候语ToolStripMenuItem.Click MsgBox(\您好\) End Sub
Private Sub 显示时间ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 显示时间ToolStripMenuItem.Click MsgBox(\今天是:\ & Now) End Sub
Private Sub 结束ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 结束ToolStripMenuItem.Click End End Sub
精品文档
Private Sub 画图ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 画图ToolStripMenuItem.Click
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub End Class
第3题 写入文本文件
设计窗体如下
图中左边文本框中multiline属性为true,程序运行时,可以向左边的文本框中输入若干行字符串,在右边文件框中输入文件名,输入时可以包含盘符和路径,单击“写入文件”按钮后,左边文本框中的内容被写入指定的文件中,写入操作完成后,屏幕上显示消息框,显示如下的信息:
参考代码如下:
Imports System.IO Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim writefile As New StreamWriter(TextBox2.Text)