Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calling an event

Posted on 2002-06-16
11
Medium Priority
?
191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

670 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