教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 互联网资料 >

《VB程序设计》课堂例题总结(6)

来源:网络收集 时间:2026-08-02
导读: Private Sub Command1_Click() Dim yw%, sx%, yy%, wl%, hx%, sum% yw = Val(Text1.Text) sx = Val(Text2.Text) yy = Val(Text3.Text) wl = Val(Text4.Text) hx = Val(Text5.Text) If Check1.Value = 1 Then sum =

Private Sub Command1_Click()

Dim yw%, sx%, yy%, wl%, hx%, sum% yw = Val(Text1.Text) sx = Val(Text2.Text) yy = Val(Text3.Text) wl = Val(Text4.Text) hx = Val(Text5.Text)

If Check1.Value = 1 Then sum = sum + yw If Check2.Value = 1 Then sum = sum + sx If Check3.Value = 1 Then sum = sum + yy If Check4.Value = 1 Then sum = sum + wl If Check5.Value = 1 Then sum = sum + hx Text6.Text = sum End Sub

用单选按钮设置计算的项目。

Private Sub Command1_Click() Dim i%, a%, b%, sum%

If Option1.Value = True Then For i = 100 To 200 Step 2 sum = sum + i Next Else

For i = 201 To 400 Step 2 sum = sum + i Next End If

Text1.Text = sum End Sub

用单选钮和复选框设置文本框的文字格式

26

Private Sub Check1_Click()

Text1.FontBold = Not Text1.FontBold End Sub

Private Sub Check2_Click() If Check2.Value = 1 Then Text1.FontItalic = True Else

Text1.FontItalic = False End If End Sub

Private Sub Check3_Click()

Text1.FontUnderline = Not Text1.FontUnderline End Sub

Private Sub Combo1_Click()

Text1.FontSize = Combo1.Text End Sub

Private Sub Command1_Click() Unload Me End Sub

Private Sub Form_Load()

Text1.Text = \白日依山尽,\黄河入海流,\欲穷千里目,\Chr(13) + Chr(10) & \更上一层楼。\ Text1.FontSize = Combo1.Text For i = 5 To 40

Combo1.AddItem 2 * i Next End Sub

Private Sub List1_Click()

Text1.FontName = List1.Text End Sub

Private Sub Option4_Click() Text1.ForeColor = vbBlack End Sub

Private Sub Option5_Click() Text1.ForeColor = vbRed End Sub

Private Sub Option6_Click() Text1.ForeColor = vbBlue

27

End Sub

窗体上有一文本框,一列表框、一标签和三个命令按钮,标题分别为“添加”、“删除”和“清除”。单击“添加”按钮,则把文本框中的内容添加到列表框中;单击“删除”按钮,则删除列表框中选中的项目。单击“清除”按钮,则清除列表框中的所有选项。当列表框中的数目发生变化的时候,用标签显示列表框中的项目数。

Private Sub Form_Load()

List1.AddItem \计算机文化基础\ List1.AddItem \程序设计教程\ List1.AddItem \操作系统\ List1.AddItem \多媒体技术\ List1.AddItem \网络技术基础\

Label1.Caption = \列表框项目数:\End Sub

Private Sub List1_Click() Text1.Text = List1.Text End Sub

Private Sub Command1_Click() List1.AddItem Text1.Text Text1 = \

Label1.Caption = \列表框项目数:\End Sub

Private Sub Command2_Click() List1.RemoveItem List1.ListIndex

'以下的这段代码适合于多项选择后删除 'For i = List1.ListCount - 1 To 0 Step -1 ' If List1.Selected(i) = True Then ' List1.RemoveItem i ' End If 'Next

Label1.Caption = \列表框项目数:\End Sub

Private Sub Command3_Click() List1.Clear

Label1.Caption = \列表框项目数:\End Sub

Private Sub Command4_Click()

28

For i = 0 To List1.ListCount - 1 If List1.List(i) = Text1.Text Then

MsgBox \列表框中已有\ Text1.Text = \ Text1.SetFocus Exit For End If Next

If i >= List1.ListCount - 1 Then List1.AddItem Text1.Text End If

Label1.Caption = \列表框项目数:\End Sub

输出100以内的素数

Dim i%, n%,Tag as Boolean For n= 2 to 100 Tag= True

For i = 2 To n–1 ?也可以是 2 to Sqr(n) If n Mod i = 0 Then Tag=False Exit For End If Next i

If Tag=True then Print n Next n

列表框或组合框的操作 提取选中的内容 Text1 = List1.Text 替换选中的内容

List1.List(List1.ListIndex) = Text1 操作列表框中的所有内容 For i = 0 To List1.ListCount - 1

要进行的操作,可用List1.List(i)表示各个项目 Next

操作列表框中选中的内容 For i=0 To List1.ListCount–1

(或者是For i=List1.ListCount-1 To 0 Step -1) If List1.Selected(i) = True Then 要进行的操作 Next

程序运行后,将把1~100之间能够被7整除的数添加到列表框中。并实现按钮的功能。

29

Private Sub Command1_Click() Dim i%, sum% sum = 0

For i = 1 To List1.ListCount sum = sum + List1.List(i - 1) Next

Text1.Text = sum End Sub

Private Sub Command2_Click() Dim i%, sum% sum = 0

For i = 0 To List1.ListCount - 1 If List1.Selected(i) = True Then sum = sum + List1.List(i) End If Next

Text1.Text = sum End Sub

Private Sub Command3_Click() Dim i%

For i = List1.ListCount - 1 To 0 Step -1 If List1.Selected(i) = True Then List1.RemoveItem i End If Next End Sub

Private Sub Form_Load() Dim i%

For i = 1 To 100

If i Mod 7 = 0 Then List1.AddItem i End If Next End Sub

点菜配餐

30

…… 此处隐藏:1665字,全部文档内容请下载后查看。喜欢就下载吧 ……
《VB程序设计》课堂例题总结(6).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/443196.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)