Question Closing Forms On MenuClick From Module

mpau23

Member
Joined
Dec 13, 2009
Messages
12
Programming Experience
1-3
Hi Guys,

Im running a program with multiple forms with a menu and toolstrip defined and assigned with properties in a module. each form has the
VB.NET:
Call menuload (Me)

in the form_load sub.

My problem is that these menu's have menuitems which need close the shown form and open another. a sample of one of my menuitems is:

VB.NET:
Module MenuTemplate

Friend WithEvents frm As Form

Friend WithEvents menustrip As New MenuStrip
Friend WithEvents RepairToolStripMenuItem As New ToolStripMenuItem

Public Sub menuload(ByVal frm As Form)
frm.Controls.Add(menustrip)
RepairToolStripMenuItem.Text = "Repair"
        
toolstrip.Items.Add(BookingToolStripDropDownButton1)        

BookingToolStripDropDownButton1.DropDownItems.Add(NewBookingToolStripMenuItem1)
NewBookingToolStripMenuItem1.DropDownItems.Add(RepairToolStripMenuItem)



Public Sub RepairToolStripMenuItemClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RepairToolStripMenuItem.Click

frm.close()
frmrepair.show ()



 End Sub

It throws this error at *frm.close()*: Object reference not set to an instance of an object.

if i get rid of the public defined form (frm) and do as follows:
VB.NET:
Public Sub RepairToolStripMenuItemClick([B]ByVal frm As Form[/B], ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RepairToolStripMenuItem.Click
        frm.Close()


    End Sub

i get an error at *Handles RepairToolStripMenuItem.Click* saying:
Method 'Public Sub RepairToolStripMenuItem_Click(frm As System.Windows.Forms.Form, sender As Object, e As System.EventArgs)' cannot handle event 'Public Event Click(sender As Object, e As System.EventArgs)' because they do not have a compatible signature.



any help would be much appreciated.
 
Friend WithEvents frm As Form
Makes an instance of an object hence the error:
VB.NET:
Friend WithEvents frm As New Form
You cannot add parameters to a signature for an event sub routine. So delete this part, you don't need it as you declared it in the class scope so you can call frm from anywhere inside this class.
VB.NET:
Public Sub RepairToolStripMenuItemClick([COLOR="Red"]ByVal frm As Form[/COLOR], ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RepairToolStripMenuItem.Click
And did you mean to make a blank Form or call one of your other made up forms?
 
i have many forms in which the sub rutiene is called (menuload) and i am aware that the module can only work on one form at a time. as one form is open, i want to use the menuclick (RepairToolStripMenuItemClick) to close the current form and load another. that form can then use the menu to click another menuitem to close that form and open another...ect ect.

any suggestions on making it work or an alternative?

Thanks in advance!
 
If I understand you correctly, I would use a UserControl instead of a module. Drop a menu strip on it and code what you need here. You can always pass new data to this if need be. Now you create an instance of your UC and add it to the form (dock/top) most likely.
 
Would i be able to use the same code into the usercontrol? and would it alow me to create events in which the assigned form can be closed?

I appreciate your help and patience
 
Absolutely! The code in the UC will be the same for each instance - hence behave the same, but also can have custom properties when they need something different. VB.NET Events
 
Last edited:
aah i see how that could work. however, where would i program the code for the menu. do i create the menustrip in the design view? i think i need to do some reading!
 
Yes drop the menuStrip control into the designer of the UC, then all the events and code in the UC are encapsulated.
 
ok ive made a start on that, however i cannot seem to be able to assign the UC to each form. i read that you can create the UC as an object in the toolbox from the dll file, however i cant figure how to do it. help?

thanks again.
 
Rebuild the solution and it should be in the toolbox in the "YourProjectName Components" at the top. :cool:
 
ok, so i did that, created a new menustrip in the designview, and added some of the properties i needed. But when i use the code

VB.NET:
form.close

OR[COLOR="Red"] frm.close[/COLOR]
 (after declaring frm as windows form)

No luck. same errors :( this is driving me mad!
 
ok, i guess i complained a bit before having a proper look into the custom events. I created a UC and a Class which inherited its properties. in that class im trying to create the "withevents" to try and assign an event to my menuclick. I have the code, no errors, yet NO output.

VB.NET:
Public Class btnControl

    Inherits UCMenuTemplate


    Public Event menuClick()

    Private Sub btnControl_ButtonClick() Handles Me.menuClick
        MessageBox.Show("I Think it works?")

    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        RaiseEvent menuClick()

    End Sub
End Class
 
Not sure why made a class that inherited from your component. Custom event are for routing something to a subscriber -> form that holds the UC so data can be passed to the form for further instruction. Now the form must have the addhandler statement with a routed sub(with the event signature) if it was added at runtime, or if you drop the UC you just request that event like you do for a button click event. Go to the properties window for the dropped UC and select the lightning bolt(events button) and click on the custom event it will make the sub for you.
 
Last edited:
This is my UC:
VB.NET:
Public Class menu

Public Event NewForm()
Private Sub NewFormToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewFormToolStripMenuItem.Click
    RaiseEvent NewForm()
End Sub

End Class

On the form I dropped the UC and made a NewForm event
Menu1 is the name of my UC, so this event is on the form not the component:

VB.NET:
Private Sub Menu1_NewForm() Handles Menu1.NewForm
    Dim f As New Form1
    f.Show()
    Me.Close()
End Sub
Mind you will have to set to Shutdown mode to 'last form closed' for this example. Is this more clear?
 
Ok I understand the structure and concept of what you are suggesting, however, doesn't that mean i have to code the

VB.NET:
Private Sub Menu1_NewForm() Handles Menu1.NewForm
    Dim f As New Form1
    f.Show()
    Me.Close()
End Sub

for every menuitem on everyform?
 
Back
Top