Does anyone know how to force the animation of a command button when clicked on?
Told you it was short and simple ;)
Thanks in advance for the help!
Printable View
Does anyone know how to force the animation of a command button when clicked on?
Told you it was short and simple ;)
Thanks in advance for the help!
What do you mean? Animation on the CommandButton itself?
If so, add the following code to a Form with a CommandButton, 2 PictureBoxes and a Timer.
Set the Style of the CommandButton to Graphical.
Load 2 different pictures into each PictureBox
Code:Private Sub Command1_Click()
Timer1.Interval = 250
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
If Command1.Picture = Picture2 Then
Command1.Picture = Picture1
Else
Command1.Picture = Picture2
End If
End Sub
no, gin means that the appearance of the buton changes for a split second when you hit the mouse button, you know...it sinks down like a real button would do.
I havent got a clue how to do this though. It may be impossible.
Why don't you create your own command button, or just use a picturebox and change the graphics on it when mousedown and up is fired :)
Do you mean change the Picture when the button is pressed? Just change the picture of a normal Commandbutton when the MouseDown event has triggered.
Add the following to a From with a CommandButton and PictureBox (make sure there is a picture loaded in the PictureBox and set the Style of the CommandButton to Graphical)
Code:Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1.Picture = Picture1
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1.Picture = Nothing
End Sub
I think the best example is the calculator that comew with windows. See how when you type the buttons are pressed as well? That's what I mean
Well, i tried something, by clicking that button in calculator by code, it works, set keypreview to true and
Just testing to click the 0 button on the calculator.Code:Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Clickit Number(0).hwnd
End Sub
Also put this in a module:
Code:Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Declare Function GetTickCount Lib "kernel32" () As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Type POINTAPI
X As Long
Y As Long
End Type
Type RECT
Left As Long
Right As Long
Top As Long
Bottom As Long
End Type
Sub Clickit(hwnd&, Optional dealy& = 5)
Dim temp As POINTAPI, rc As RECT
GetCursorPos temp
GetWindowRect hwnd, rc
SetCursorPos (rc.Left + rc.Right) / 2, (rc.Top + rc.Bottom) / 2
mouse_event 2, 0, 0, 0, 0
t = GetTickCount + dealy
Do While t > GetTickCount
DoEvents
Loop
mouse_event 4, 0, 0, 0, 0
SetCursorPos temp.X, temp.Y
End Sub
I'm really sorry I don't think I properly explained myself. I'll explain what I want to do in my program:
Basically when I click on this picture box, I want to make it seem that the a certain command button was pressed/clicked on. So it's like as if cliked on the command button instead of the picture box. That's the reason why I want to force the animation, so when the click event of the picture box occurs the animation of the click event for the button will occur (as well as the procedure for the click event of the command button, but we all know how to do that :) )
Sorry if i was confusing you all :D
press the button down, take a screenshot (printscreen)
paste the picture somewhere and crop the button, take a screenshot again with button unpressed and crop that one too, then you have both states, you can store them in a resource file, imagelist, stdpicture or a file or whatever,
in mouse down event change the picture on the picturebox to buttonpressed image, and then swithch it back in mouseup event
Thanks kedaman! I was thinking of doing that at first, but I was thinking maybe there was a simpler way by code to do it. Just needed a Guru to confirm the idea.
Oh hey I have another question though, not realated to this post, except it's a simple one too.
When I type something in a combo box, and on the event I lose it's focus, how do I make the combo box contain the element in its list closest to what the input is. Just like if I typed something then pressed down key in the keyboard (which then displays the element closest or identical).
Do I just code it, by searching the item using loops on the lose focus event, or is there an easier way, like some method that the combo box has that I don't know?
Thanks again Guru! :)
Here's a sample I prepared to answer a similar question. It doesn't work quite the way you describe, but what this does is jump to the first item in the combobox list that has a first letter identical to what you type in the textbox. I have an idea for what you specifically asked, so I'll paste whatever I can work out.
(after some checking...) VB does this to some extent already. If you type a part of an existing item in the box and press the down arrow, the rest of the word appears in the box. It only works for things already in the list though. Like if apple was in the list, an you type 'ap' and hit down, 'apple' appears (provided that 'apple' is alphabetically the first item starting with 'ap' that's in the list). I think Sorted has to be set to True for this to work. Searching for a closest match would require some extra work.Code:Option Explicit
Private Sub Form_Load()
'some test things
Combo1.AddItem "apple"
Combo1.AddItem "aardvark"
Combo1.AddItem "banana"
Combo1.AddItem "ball"
Combo1.AddItem "cherry"
Combo1.AddItem "doorknob"
Combo1.AddItem "eggplant"
Combo1.AddItem "easy"
Combo1.AddItem "test"
Combo1.AddItem "fruit"
End Sub
Private Function Find(Target As ComboBox, SearchKey As String) As Integer
Dim SearchDomain As String
Dim Position As Integer
Dim i As Integer
SearchDomain = ""
SearchKey = LCase$(SearchKey) 'remove case sensitivity
Find = -1 'default is not finding anything
'if there's nothing in the combobox, leave function
If Target.ListCount = 0 Then Exit Function
'cycle through all items in the combobox, building a
'string using the first char of each item
For i = 0 To (Target.ListCount - 1)
SearchDomain = SearchDomain & LCase$(Left$(Target.List(i), 1))
Next i
'look for the first position of the desired key in the string
Position = InStr(SearchDomain, SearchKey)
'if it was found, return the position in the combobox corresponding
'to the found position (items in a combobox go from 0 to .ListCount - 1)
If Position > 0 Then Find = Position - 1
End Function
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim NewIndex As Integer
'try to find the pressed key in the combobox
NewIndex = Find(Combo1, Chr$(KeyAscii))
'if found, change the combobox's currently selected index and text to show it
If NewIndex <> -1 Then
Combo1.ListIndex = NewIndex
Combo1.Text = Combo1.List(Combo1.ListIndex)
End If
End Sub
Looks like it wasn't such a simple question after all! I noticed that VB does this in some extent (the down arrow after input). I used a function similar to yours. In fact it has the same algo and flow, just that yours is shorter. Thanks for the code, learned a thing or two with it!
I'll try to play around and see if I can get the closest match when I'm done the other aspects of my program.
Thank again :)