Can someone tell me how I can make a button move to random spots on the form??
Printable View
Can someone tell me how I can make a button move to random spots on the form??
Code:Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim x1 As Long, y1 As Long
x1 = Int(Rnd * (Width - Command1.Width))
y1 = Int(Rnd * (Height - Command1.Height))
Command1.Move x1, y1
End Sub
I'm not sure if this is what you're looking for, but this function will randomly relocate a command button somewhere on a form:
Private Sub moveButton(cntlButton As Control)
Dim newX As Long
Dim newY As Long
Randomize
newX = Int((Me.Width - cntlButton.Width - 150) * Rnd + 15)
newY = Int((Me.Height - cntlButton.Height - 420) * Rnd + 15)
cntlButton.Move newX, newY
End Sub
Private Sub Command1_Click()
Call moveButton(Command1)
End Sub
can you *edited*clean*edited* that up alittle and tell me what that all do??Quote:
Originally posted by mstuehler
I'm not sure if this is what you're looking for, but this function will randomly relocate a command button somewhere on a form:
Private Sub moveButton(cntlButton As Control)
Dim newX As Long
Dim newY As Long
Randomize
newX = Int((Me.Width - cntlButton.Width - 150) * Rnd + 15)
newY = Int((Me.Height - cntlButton.Height - 420) * Rnd + 15)
cntlButton.Move newX, newY
End Sub
Private Sub Command1_Click()
Call moveButton(Command1)
End Sub
btw...I want it to keep going to random spots non stop
that code work, but how can I be able to make it not be able to use Tab to get over the button??Quote:
Originally posted by Nucleus
Code:Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim x1 As Long, y1 As Long
x1 = Int(Rnd * (Width - Command1.Width))
y1 = Int(Rnd * (Height - Command1.Height))
Command1.Move x1, y1
End Sub
If you use Nucleus' code (which is always a good idea), you can add the following to prevent the user from using [tab] to get to the button:
Private Sub Command1_GotFocus()
[some other object].SetFocus
End Sub
In other words, if the button you don't want the user to get is Command1, you can deflect the focus to another object (for example, Command2) using Command2.SetFocus in the GotFocus event of Command1.
Hope this helps.
Cheers!
It works!!! thank you.Quote:
Originally posted by mstuehler
If you use Nucleus' code (which is always a good idea), you can add the following to prevent the user from using [tab] to get to the button:
Private Sub Command1_GotFocus()
[some other object].SetFocus
End Sub
In other words, if the button you don't want the user to get is Command1, you can deflect the focus to another object (for example, Command2) using Command2.SetFocus in the GotFocus event of Command1.
Hope this helps.
Cheers!
Set the TabStop property for the button to False.Quote:
that code work, but how can I be able to make it not be able to use Tab to get over the button??