Solved

Calling a button/menu click from another sub

Posted on 2004-09-20
4
686 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

11 Experts available now in Live!

Get 1:1 Help Now