Solved

Calling an event

Posted on 2002-06-16
11
170 Views
Last Modified: 2010-05-02
Hi all,

Wondering if it is possible to call an object event.  A good example would be how to call an event like the "Click" event in a particular command button from code.  

That would be good enough but even better if a call could actually initiate the graphical button press.

Thanks,

Jim
0
Comment
Question by:syntel
  • 5
  • 3
  • 2
  • +1
11 Comments
 

Expert Comment

by:Hotwu
ID: 7082842
the events are private which means they or only open to the form in which the object resides. here is an example of something like you stated in action:

Private Sub Command1_Click()

    MsgBox "Hello"
   
End Sub

Private Sub Form_Load()

    Call Command1_Click
   
End Sub

you can however manually change the command1_click event to public if you wish. hope this helps.
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 200 total points
ID: 7083255
Hotwu, please restrict yourself to posting comments. You are not new to this site so should be aware of the etiquette. I agree that your proposed answer does cover much of the ground of the question but this particular topic area does not use the answer option except in extremely rare cases.

syntel:

Indeed as Hotwu suggests you can directly call the event procedure in this way, for command buttons there is also another way to fire the event procedure:

Command1 = True

This will cause a click event to be fired though you still do not get the graphical "Click" happening.

The following code will simulate the button click:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg
As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const WM_LBUTTONDOWN = &H201
Private Const BM_SETSTATE = &HF3
Private Const WM_LBUTTONUP = &H202

Private Sub Command1_Click()
   MsgBox "Hello"
End Sub

Private Sub Command2_Click()
     Dim ret As Long
     ret = SendMessage(Command1.hwnd, WM_LBUTTONDOWN, 0&, 0&)
     ret = SendMessage(Command1.hwnd, BM_SETSTATE, 1&, 0&)
     Sleep 200
     ret = SendMessage(Command1.hwnd, BM_SETSTATE, 1&, 0&)
     ret = SendMessage(Command1.hwnd, WM_LBUTTONUP, 0&, 0&)
End Sub

This code will make the command button depress for .2 of a second. Simulating a "physical" click.

As you are actually sending click messages to the button itself then the click event will be fired if you use this code. Ideally you would put this code into a module and make it a public sub with the Control being passed as a parameter. You then have a standard function which achieves all of your aims in one go.

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const WM_LBUTTONDOWN = &H201
Private Const BM_SETSTATE = &HF3
Private Const WM_LBUTTONUP = &H202

Public Sub ClickButton(ByRef CButton As CommandButton, Optional ByVal Delay As Long = 200)
     Dim ret As Long
     ret = SendMessage(CButton.hwnd, WM_LBUTTONDOWN, 0&, 0&)
     ret = SendMessage(CButton.hwnd, BM_SETSTATE, 1&, 0&)
     Sleep Delay
     ret = SendMessage(CButton.hwnd, BM_SETSTATE, 1&, 0&)
     ret = SendMessage(CButton.hwnd, WM_LBUTTONUP, 0&, 0&)
End Sub


Then you can use the following code in your program:

ClickButton Command1,500

To click the specified button and hold it down for 500 Msecs.
0
 

Expert Comment

by:Hotwu
ID: 7083809
honestly, my apologies. i am not new to the site, because i have had an account for a great length of time, but i thought there was a difference between providing an answer and locking a question. the last time i was here i thought there was an option to lock answers. i did not mean to steal the opportunity to answer this question from any other developers. i will read through the rules to make sure that i understand the processes.
0
 

Author Comment

by:syntel
ID: 7091686
Thanks to all for the ideas.

I will be accepting TimCottee's answer as soon as I have a chance to implement it since it addresses both calling the event and the graphical action of the control.

Jim
0
 

Expert Comment

by:Hotwu
ID: 7091702
perhaps someone could explain the benefit if marking an response as "answer" if it keeps other people from receiving points for appropriate answers. i just do not understand the process here. i see no reason to even have the option to mark something as answer.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:syntel
ID: 7091750
Hotwu:

I agree that the setup of the board is not consistent with the preferences of the usership.  The original configuration was implemented I'd guess because of how points were handled in the old days before the IRS got their panties in a bunch about it and things changed.

Why the site's configuration has not changed along with the accepted etiquette is anybody's guess.

Jim
0
 

Author Comment

by:syntel
ID: 7091756
Works great!

I am going to try to use this without the Sleep and using the MouseUp and MouseDown events to manipulate the remote button.

Thanks again,

Jim
0
 

Author Comment

by:syntel
ID: 7091762
Whoops, just realized I had the C checked...meant to give you an A. SORRY!

If this can be changed let me know!!

0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7091994
syntel, all you need to do is post a question in http://www.experts-exchange.com/commspt/ include the URL of this question in the body and explain the situation. A moderator will revise the grade accordingly. Thanks for your consideration on this and glad you got a solution.

I have to agree wholeheartedly in respect of the answer option. As a member of the advisory board I am strongly arguing that it should be removed or at least altered in the way that this works. We are slowly coming to some conclusions on this though the code will not see much change quickly. This is one of the reasons that you may see me post in a number of threads against the use of the answer option, not because the answer proposed is in any way a bad one but that it reduces the collaborative effect that is one of the great features of this site.
0
 

Author Comment

by:syntel
ID: 7095327
No problem will try to get the C changed tommorrow.

BTW: I noticed that in using the API the simulated button click creates an anomalous double sized shadow for the button - it looks a little odd and goes away after the button looses focus.  Is it possible to reduce the size of that shadow or button outline?

Thanks,

Jim
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7095394
Thank you all, I've corrected this grade.
Moondancer - EE Moderator
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now