當前位置:編程學習大全網 - 編程語言 - VB高手請進

VB高手請進

把下面代碼保存為Form1.frm文件,然後打開該文件,運行即可。

VERSION 5.00

Begin VB.Form Form1

BorderStyle = 3 'Fixed Dialog

Caption = "猜數大師"

ClientHeight = 2055

ClientLeft = 45

ClientTop = 435

ClientWidth = 3465

LinkTopic = "Form1"

MaxButton = 0 'False

MinButton = 0 'False

ScaleHeight = 2055

ScaleWidth = 3465

StartUpPosition = 2 '屏幕中心

Begin VB.CommandButton Command1

Caption = "開始"

Height = 495

Left = 1080

TabIndex = 3

Top = 960

Width = 1215

End

Begin VB.TextBox Text1

Height = 375

Left = 480

TabIndex = 0

Top = 360

Width = 855

End

Begin VB.Label Label2

Caption = "最多輸入5次1-31之間的數據,就能猜中數字"

Height = 375

Left = 120

TabIndex = 2

Top = 1560

Width = 3255

End

Begin VB.Label Label1

Caption = "Label1"

Height = 375

Left = 1680

TabIndex = 1

Top = 360

Width = 975

End

End

Attribute VB_Name = "Form1"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = True

Attribute VB_Exposed = False

Option Explicit

Dim p1 As Integer, p2 As Integer

Dim Begin As Boolean

Private Sub Command1_Click()

p1 = 1: p2 = 31

Randomize

Label1.Tag = Int(Rnd * (31 - 1) + 1)

Label1.BackColor = &H8000000F

Begin = True

'Debug.Print Label1.Tag

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

If Not (KeyAscii = 13 And Begin = True) Then Exit Sub

Dim i As Integer

i = Val(Text1.Text)

If i < 1 Or i > 31 Then

MsgBox "請輸入1-31之間的數字!", vbCritical, "提示"

Exit Sub

End If

With Label1

Select Case i - Val(Label1.Tag)

Case 0

.Caption = "妳答對了!"

.BackColor = vbGreen

Begin = False

Case Is > 0

.Caption = "大了!"

p2 = Val(Text1.Text)

.BackColor = vbRed

Case Is < 0

.Caption = "小了!"

p1 = Val(Text1.Text)

.BackColor = vbBlack

End Select

End With

Text1.SetFocus

End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyV And Shift = 2 And Begin = True Then

Text1.Text = IIf(((p1 + p2) Mod 2) = 0, (p1 + p2) / 2, Fix((p1 + p2) / 2) + 1)

End If

End Sub

  • 上一篇:200分UTF8編碼的字符32位(16?)進制數值的取得
  • 下一篇:小學二年級美術說課稿優秀範文集錦
  • copyright 2024編程學習大全網