1.新建工程,界面如下图,将窗体名称改为f1,标题属性为“我的第一个工程”, Text2允许多行,三个命令按钮名称分别为C1、C2、C3,单击C1按钮将Text1的字体改为黑体,Text2的字体改为隶书。 单击C2按钮将Text1的文字颜改为红,Text2的背景颜改为蓝。单击C3按钮结束程序运行。
Private Sub Command1_Click()
Text1.Font = 黑体
End Sub
Private Sub Command2_Click() Text1.ForeColor = vbRed
Text2.ForeColor = vbBlue End Sub
Private Sub Command3_Click() End End Sub
2.新建工程,界面如上图,运行时,鼠标在“改变窗体颜”按钮上按下时使窗体背景变为红,鼠标抬起时使窗体背景变为绿。鼠标在窗体空白地方按下时使Text1的内容为“在窗体上按下了鼠标”,鼠标抬起时使Text1的内容为“在窗体上抬起了鼠标”。
Private Sub Command1_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single)
Form1.BackColor = vbRed End Sub
Private Sub Command1_MouseUp (Button As Integer, Shift As Integer, X As Single, Y As Single)
Form1.BackColor = vbGreen End Sub
Private Sub Form _MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single) Text1.Text = "在窗体上按下了鼠标" End Sub
Private Sub Form _MouseUp (Button As Integer, Shift As Integer, X As Single, Y As Single) Text1.Text = "在窗体上抬起了鼠标" End Sub
3.新建工程,界面如下图,运行时,在文本框Text1中输入内容的同时,在文本框Text2、Text3中显示相同的内容,但字体不同(字体自定)。
Private Sub Command1_Click() Text1.Text = ""
Text2
Text1
Text2.Text = ""
Text3.Text = ""
End Sub
Private Sub Command2_Click() End
End Sub Private Sub Text1_Change()
Text2.Text = Text1.Text
Text3.Text = Text1.Text
End Sub
注:“字体不同”可以在属性那调“Font”
4.设计一个窗体,如上图所示。当单击某个命令按钮时,对文本框中的文字完成相应的设置。其中,每单击一次“增大”或“缩小”按钮将使文本框中的文字增大或缩小5磅,“粗体”按钮将使文本框中的文字加粗,“斜体”按钮将使文本框中的文字倾斜,“下划线”按钮将使文本框中的文字加下划线,“删除线”按钮将使文本框中的文字加删除线。
Private Sub Command1_Click() Text1.FontSize = Text1.FontSize + 5 End Sub
Private Sub Command2_Click() Text1.FontSize = Text1.FontSize - 5 End Sub
Private Sub Command3_Click() Text1.FontBold = True
End Sub Private Sub Command4_Click() Text1.FontItalic = True
End Sub
Private Sub Command5_Click() Text1.FontUnderline = True End Sub
Private Sub Command6_Click() Text1.FontStrikethru = True End Sub
5.设计一个窗体,如下图所示。程序中将π定义成符号常量(用Const ),运行时,输入底面半径r 和圆柱高h,然后单击“计算”命令按钮,在相应文本框中显示计算结果。运算结果为只读。Private Sub Command1_Click()
Const pi = 3.14
h = Val(Text1)
r = Val(Text2)
Text3.Text = pi * r ^ 2
Text4.Text = 2 * pi * r * h
Text5.Text = pi * r ^ 2 * h
End Sub
6.设计界面如上图所示。单击“出题”按钮,产生任意两个【1,100】之间的随机整数于Text1、 Text2中,单击“计算”按钮,求两个数的和。运算结果为只读。
Private Sub Command1_Click() Text1.Text = Int(100 * Rnd + 1) Text2.Text = Int(100 * Rnd + 1) End Sub Private Sub Command2_Click()
a = Val(Text1.Text)
b = Val(Text1.Text)
Text3.Text = a + b
End Sub
7.设计一个数字钟表程序,界面如图所示。运行时单击窗体,显示当前的年月日星期及时间。Private Sub Form_Click()
Label2.Caption = Year(Now)
Label4.Caption = Month(Now)
Label6.Caption = Day(Now)
Label9.Caption = Weekday(Now)
Label11.Caption = Time
End Sub
8.设计一个数学函数程序,界面如图所示。程序运行时,在文本框中输入任意数值,单击相应按钮标签中显示结果。文本框始终处于选中状态。
Private Sub Command1_Click() Label3.Caption = Abs(Val(Text1)) Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text) End Sub Private Sub Command2_Click() Label3.Caption = Sqr(Val(Text1)) Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text) End Sub
Private Sub Command3_Click() Label3.Caption = Fix(Val(Text1)) Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text) End Sub
Private Sub Command4_Click() Label3.Caption = Int(Val(Text1)) Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text) End Sub
Private Sub Command5_Click() Label3.Caption = Sgn(Val(Text1)) Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text) End Sub
9.设计一工程,在文本框中输入一整数,单击“判断”按钮,判断能否同时被3、5、7整除,若能用MsgBox函数显示“能同时被3、5、7整除”,否则显示“不能整除”。
Private Sub Command1_Click()
a = Val(Text1)
If a Mod 3 = 0 And a Mod 5 = 0 And a Mod 7 = 0 Then
MsgBox "能同时被3、5、7整除"
Else
MsgBox "不能整除"
End If
End Sub
10.设计一个计算购书价钱的程序,界面如下图所示。为了提高运行速度,设置Tab键序,使得输入完单价后,焦点定位到输入数量文本框中。单击“计算总价”或用访问键,算出总价。总价文本框要求为只读属性。完成相应功能。
Private Sub Command1_Click()
a = Val(Text1)
b = Val(Text2) Text3.Text = a * b End Sub Private Sub Command2_Click() End
End Sub
11.设计一工程,界面如上图所示。程序运行,单击“计算”,求出对应角度的三角函数的绝对值于文本框Text2中,同时要求选中文本框Text1内容,以便继续输入。
Private Sub Command1_Click()
a = Val(Text1)
b = a * 3.14 / 180
c = Abs(Sin(b))
d = Abs(Cos(b))
Text2.Text = Text2.Text & a & Space(10) & Format(c, "0.000") _
贴吧vb是什么意思& Space(10) & Format(d, "0.000") & vbCrLf
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
End Sub
12.设计一个程序,用户界面如下图所示。程序运行后,单击“计算”,求出应付款额;单击“清除”,清除所有文本框内容。
Private Sub Command1_Click()
a = Val(Text1)
b = Val(Text2)
c = Val(Text3)
Text4.Text = a * b * c
End Sub Private Sub Command2_Click() Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End Sub
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论