你好,欢迎访问远方教程PC版!
广告位招租
网站首页 >> 统计之窗 >> VB专区 >> 文章内容

VB基础教程第四章:VB程序设计(第4页)

[日期:2014-12-06]   来源:远方教程  作者:VBFans   阅读:13063次[字体: ] 访问[旧版]
 捐赠远方教程 

  下面我们来看看代码:

在General 里声明窗体级变量:

Dim Level As Integer

Private Sub Command1_Click()
  Unload Me
End Sub

Private Sub Text1_Change()
Dim StrLength As Integer’字符串长度
Dim RandomNum As Single’随机数
Dim Str1 As String
Dim a As String
Dim i, Temp
StrLength = 0’每次开始时把字符串长度还原为0
a = Text1.Text

For i = 1 To Len(a)’Len(a)代表字符串的长度
  Temp = Asc(Mid$(a, i, 1))’挨个取字符
  If Temp > 255 Or Temp < 0 Then’不是ASC码
    StrLength = StrLength + 2
  Else
    StrLength = StrLength + 1
  End If
Next i

StrLength = StrLength / 2
Select Case StrLength

Case 0 To 9
  Picture1.Visible = True
  Picture2.Visible = False
  Picture3.Visible = False
  Label2.Caption = "慢慢说。你还有" & Str(30 - StrLength) & "个字可以说"
  Level = 1
Case 10 To 19
  If Level = 1 Then
    
RandomNum = Rnd * 10’得到随机数
    If RandomNum <= 10 And RandomNum > 9 Then
      Text1.Enabled = False
      Label2.Caption = "很抱歉这是个误会……"
      Command1.Visible = True
      Command1.Caption = "您可以离开了"
    End If
    Level = 2
  Else
    Picture1.Visible = False
    Picture2.Visible = True
    Picture3.Visible = False
    Str1 = "怎么还没说完!"
    Label2.Caption = Str1 & "你还有" & Str(30 - StrLength) & "个字可以说"
  End If
Case 20 To 29
  
If Level = 2 Then
    RandomNum = Rnd * 10
    If RandomNum <= 10 And RandomNum > 9 Then
      Text1.Enabled = False
      Label2.Caption = "很抱歉这是个误会……"
      Command1.Visible = True
      Command1.Caption = "您可以离开了"
    End If
    Level = 3
  Else
    Picture1.Visible = False
    Picture2.Visible = False
    Picture3.Visible = True
    Str1 = "我已经不耐烦了!"
    Label2.Caption = Str1 & "你还有" & Str(30 - StrLength) & "个字可以说"
  
End If
Case Else
  
Text1.Enabled = False
  Label2.Caption = "你已经没有机会了!"
  Command1.Visible = True
  Command1.Caption = "交钱吧,肇事犯!"
End Select

End Sub

06-07(岂有此理)

中间有几个函数需要解释一下:

  Mid可以从字符串中取出一部分,返回字符型,格式:Mid(字符串,从第几个字符开始,取字符的长度)
  Asc可以把字符转换为ASCII码,格式:Asc(字符串)
  Rnd,取得大于0但小于1 的数值,可以没有参数。

怎么样,十分简单吧,我们将在下一次继续讨论VB的输入机制,再见!

图片展示
 
 
相关评论
站长推荐