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

如图所示

另外的要求就是当标签框移动到屏幕左边的极限值时,命令按钮变为“右”,标签框开始向右移动。

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
你试试吧,觉得移动慢就把数字改大点
温馨提示:内容为网友见解,仅供参考
第1个回答  2014-04-21
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

第2个回答  2014-04-21

按钮的代码

 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

相似回答