VB 编制程序,当点击命令按钮时,标签框左右移动

2025-03-01 06:54:46
推荐回答(3个)
回答1:

Private Sub Command1_Click()
If Command1.Caption = "左" Then
If Label1.Left <= 0 Then
Command1.Caption = "右"
Else
Label1.Left = Label1.Left - 120
End If
ElseIf Command1.Caption = "右" Then
If Label1.Left >= Form1.Width - Label1.Width Then
Command1.Caption = "左"
Else
Label1.Left = Label1.Left + 120
End If
End If
End Sub
你试试吧,觉得移动慢就把数字改大点

回答2:

Private Sub Command1_Click()
    If Command1Caption = "左" Then
        MoveLeft
    Else
        MoveRight
    End If
End Sub
'向左
Private Sub MoveLeft()
    With Label1
        Do While (.Left + .Width) < Me.ScaleWidth
            .Left = .Left + 15
            DoEvents
        Loop
    End With
    Command1.Caption = "右"
End Sub
'向右
Private Sub MoveRight()
    With Label1
        Do While .Left > 0
            .Left = .Left - 15
            DoEvents
        Loop
    End With
    Command1.Caption = "左"
End Sub

回答3:

按钮的代码

 Private Sub Command1_Click()
    If Command1.Caption = "左" Then
        Label1.Left = Label1.Left - 50
        If Label1.Left <= 10 Then
            Command1.Caption = "右"
        End If
    End If
    
    If Command1.Caption = "右" Then
         Label1.Left = Label1.Left + 50
    End If
    
End Sub