Reputation: 279
I have Main and Press Form in my project and I was trying to call the PressToolStripMenuItem_Click event in my Main Form from the Press Form to refresh or reload the Press Form but nothing happens. Thank you in advance. Here's my code:
Public Class frmPress
Dim main As New frmMain()
main.PressToolStripMenuItem_Click(sender, e)
End Class
Upvotes: 0
Views: 671
Reputation: 54417
You are going about things in completely the wrong way.
First of all, don't ever try to call an event handler directly. If you have code that you want executed under multiple different circumstances then put that code in its own method and then call that method from multiple different places. Whatever code you currently have in the Click
event handler of the menu item, move to its own method and then call it from the event handler. You can then call that same method from anywhere else you need to as well.
That's not your main issue though. Think about what the code you posted actually does. You already have an instance of frmMain
displayed on the screen, right? How could creating a new frmMain
instance (it's right there in your code: New frmMain
) and calling a method in that help you do something to that existing instance? If you want to affect the existing instance then you have actually call a method of that existing instance. There are multiple ways to do that.
Assuming that frmMain
is the startup form for your application then it is the default instance of its type. That means that you can take the easy option and use the default instance to access it. The default instance of a form is accessed via the type name. That means that you can just do this:
frmMain.SomeMethod()
where SomeMethod
is the method you wrote based on the advice provided above.
There are better ways but I won't go into them here. If you'd like to learn how to do this "properly", check out my blog post on the subject here. The first part is about default instances and parts 2 and 3 provide better options, although they may seem complex to a beginner. You can also learn more about default instance here.
Upvotes: 1