Solved

Calling a button/menu click from another sub

Posted on 2004-09-20
4
697 Views
Last Modified: 2012-06-21
Hi,

In my code, I have a button (OK) that has code:

 Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
         ...
 End Sub


In another spot, I have an icon shortcut as well as a keypress that should trigger this code - but unlike in VB6, I can't just say Call cmdOK_click

It seems to want arguments...  However, I don't know what sender or e really are.  I found that if I use

Call cmdOK_Click("", e)

it accepts "" for sender, but I can't find any null-type expression to replace e with, (0, "", '', null, etc)

What do I need to do to trigger a _click event from another sub?

JP
0
Comment
Question by:gleznov
4 Comments
 

Author Comment

by:gleznov
ID: 12104791
OK I cheated - I put the code from the _click into it's own private sub, then just called it without the call command in both _click and the other routines.  However, I'd still like to know if there was an answer to my original question?

JP
0
 
LVL 2

Expert Comment

by:Mehrdad_Y
ID: 12104944
Hello, you have two ways:
1- cmdOk_Click(nothing, new System.EventArgs())
2- Me.cmdOK.PerformClick()
0
 
LVL 5

Assisted Solution

by:tzxie2000
tzxie2000 earned 125 total points
ID: 12104951
you can just call cmdOK_Click(Nothing, Nothing)
but you should not use object and e in cmdOK_Click
0
 
LVL 3

Accepted Solution

by:
imu79 earned 125 total points
ID: 12105290
It depends on what exactly you're trying to do. If you actually want to 'fire' the event, then you can call the PerformClick method of the button to raise the click event for the button. If you simply want to execute the code in the click event handler, you can directly call the procedure as mentioned in the previous to comments. A more cleaner solution would be to have your event handling procedure call another private procedure and whenever you want to execute the event handling code, you can directly call the private procedure and not worry about passing parameters into the event handing procedure.

Something like this:

Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        ClickEventProcedure( )
End Sub

Private Sub ClickEventProcedure( )
    ' click event handing code...
End Sub

Private Sub AnotherSub( )
    ClickEventProcedure( )
End Sub

Ofcourse, your click event handler is using the sender and the event arguments for whatever processing you're doing, you would need to pass them in to this private event procedure. But that would defeat the whole purpose of not having to pass the event arguments. I guess in the end it all boils down what exactly is the purpose of executing the  event handling code - based on that you can decide which method best suites your requirements.

hope that helps..
Imran.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 (3.5 Framework) Remove all items from List 3 32
VB.NET 1 28
Help with error in Query 2 31
Get month and date in a format 4 35
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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