943,701 Members | Top Members by Rank

Ad:
Jan 13th, 2009
0

How can I know the button is pressed?

Expand Post »
Hi,
Could anyone give me some suggestion?
How can I detect the button is pressed continuously? Actually, I use Command1_MouseDown , but it is not succeed.

I would like to do the application continuously if the button is pressed. If the user release (mouse up), I will stop the application.

How should I do?

Best regards,
zawpai
Similar Threads
Reputation Points: 7
Solved Threads: 0
Junior Poster in Training
zawpai is offline Offline
99 posts
since Oct 2007
Jan 13th, 2009
1

Re: How can I know the button is pressed?

when you say running the application continuously, i'm assuming you mean there's looping going on?

alright... well, there are a few ways you could do it... easiest way might be defining a boolean variable... for instance
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. option explicit
  2. private bRunning as boolean
then you can set the variable and run whatever you're running with the mouse down event...
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2. 'set the variable
  3. bRunning = true
  4. 'run the program
  5. call doLoopedStuff
  6. end sub
then with the mouse up event you terminate it
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2. bRunning = false
  3. end sub
all this is nice and all, but won't really help much if your program is looping intencely and doesnt' pay attention to the events... so be sure to DoEvents
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. private sub doLoopedStuff()
  2. do while(bRunning=true)
  3. doEvents
  4. 'your code stuff
  5. loop
  6. end sub
Last edited by kain_mcbride; Jan 13th, 2009 at 5:15 am. Reason: adding stuff? :p
Reputation Points: 22
Solved Threads: 3
Light Poster
kain_mcbride is offline Offline
25 posts
since Nov 2008
Jan 13th, 2009
0

Re: How can I know the button is pressed?

Hello kain_mcbride,

Thank you for your information.

I confuse MouseDown and KeyPress . So, I posted it. You know it is doing the looping automatically if we use Form_KeyDown and press any key.

So, I want to make sure them clearly. Can I ask you one more question? What the difference between MouseDown and Click ?

Best regards,
zawpai
Reputation Points: 7
Solved Threads: 0
Junior Poster in Training
zawpai is offline Offline
99 posts
since Oct 2007
Jan 13th, 2009
0

Re: How can I know the button is pressed?

hi kain_mcbride,

I have tried the method you gave me. It is working well, but

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. private sub doLoopedStuff()
  2. do while(bRunning=true)
  3. doEvents
  4. 'your code stuff
  5. loop
  6. end sub

have one problem. If we change it to

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. private sub doLoopedStuff()
  2. do while(bRunning=true)
  3. 'your code stuff
  4. doEvents
  5. loop
  6. end sub

You know, after doing doEvents , our program should Exit Do .

Thank a lot.
zawpai
Reputation Points: 7
Solved Threads: 0
Junior Poster in Training
zawpai is offline Offline
99 posts
since Oct 2007
Jan 13th, 2009
0

Re: How can I know the button is pressed?

Click to Expand / Collapse  Quote originally posted by zawpai ...
Can I ask you one more question? What the difference between MouseDown and Click ?
MouseDown is when you push the mouse button down... it's not a full click, just when you press the button...

Click is a full click... mouse down / mouse up in close succession over the same object / location...

typicly click would be used... just detects if the user has clicked something in a normal kind of way... however, i understood your specifications to say that the program would run continually until the button was released... so, mouse down fires where as click won't... then you can use mouse up later to break the loop
Reputation Points: 22
Solved Threads: 3
Light Poster
kain_mcbride is offline Offline
25 posts
since Nov 2008
Jan 14th, 2009
0

Re: How can I know the button is pressed?

Click to Expand / Collapse  Quote originally posted by zawpai ...
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. private sub doLoopedStuff()
  2. do while(bRunning=true)
  3. 'your code stuff
  4. doEvents
  5. loop
  6. end sub

You know, after doing doEvents , our program should Exit Do .

Thank a lot.
zawpai
hello zawpai

I don't fully understand, and may have misunderstood your initial problem. You were having problems with the MouseDown event, which you were trying to use to cause your program to run continually until the button was released. I assumed that you meant that you had a section of code that you wanted to run repeatedly while the button was held down.

As such, i put it in a do while loop, with the clause that bRunning had to be true for the loop to continue... DoEvents prevents the program from taking full priority in the loop, causing the events that should be firing to fire vs waiting in the loop for whatever clause or justification that needs to be met to be met...

Anyway, i'm not really sure what you mean now.
Reputation Points: 22
Solved Threads: 3
Light Poster
kain_mcbride is offline Offline
25 posts
since Nov 2008
Jan 14th, 2009
0

Re: How can I know the button is pressed?

Hello kain_mcbride,

MouseDown is when you push the mouse button down... it's not a full click, just when you press the button...

Click is a full click... mouse down / mouse up in close succession over the same object / location...

Thank you for your good explanation. Now, I am clear about it.

(Toggle Plain Text)

private sub doLoopedStuff()
 do while(bRunning=true)
  'your code stuff
  doEvents
 loop
end sub

You know, after doing doEvents , our program should Exit Do .

Sorry for above case, I didn't mean you are wrong. You are right.
Because of my application, I just change the sequence.

Thank you for your helping.

zawpai
Reputation Points: 7
Solved Threads: 0
Junior Poster in Training
zawpai is offline Offline
99 posts
since Oct 2007
Jan 15th, 2009
0

Re: How can I know the button is pressed?

using ascii checking on your button event.
Last edited by Jx_Man; Jan 15th, 2009 at 10:31 pm.
Reputation Points: 1182
Solved Threads: 392
Posting Sensei
Jx_Man is offline Offline
3,138 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Search for Exact Phrase in String using VBA
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Copying Tables From Word and Pasting in Excel in VBA





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC