1

The code below is for a custom menu strip. I am trying to figure out how to access the current open form so I may close it among other things. What I am basically trying to do is run "ActiveForm.Close()" when a user clicks to navigate to a new form, from the current one.

Thanks for your help!

Public Class MenuStripCustom
    Inherits MenuStrip

Private WithEvents NavToolStrip As New ToolStripMenuItem("File")
Private WithEvents NavMainMenu As New ToolStripMenuItem("Main Menu")
Private WithEvents NavSignOut As New ToolStripMenuItem("Sign Out")
Private WithEvents NavExit As New ToolStripMenuItem("Exit")

Sub New()
    Me.Items.Add(NavToolStrip)
    NavToolStrip.DropDownItems.Add(NavMainMenu)
    NavToolStrip.DropDownItems.Add(NavSignOut)
    NavToolStrip.DropDownItems.Add(NavExit)
End Sub


' All forms
Private Sub NavExit_Click(sender As Object, e As EventArgs) Handles NavExit.Click
    Application.Exit()
End Sub

Private Sub NavMainMenu_Click(sender As Object, e As EventArgs) Handles NavMainMenu.Click
    'MainMenu.visible = true
    'ActiveForm.Close()
End Sub

End Class

Edit: 'ActiveForm' is not defined for the class because this is a MenuStrip object. I'm unsure of how to access the current form through this class, when I put this object on a form. I commented out what I was trying to do at the bottom of the code. Sorry for confusion.

2
  • What is the problem? Is there an error? Commented Nov 4, 2012 at 10:48
  • 'ActiveForm' is not defined for the class, I'm unsure of how to access the current form. If you look towards the bottom, I commented out what I was trying to do. Sorry for confusion. Commented Nov 4, 2012 at 10:51

2 Answers 2

2

You can access the active form with this static property:

Form.ActiveForm

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activeform.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I am a beginner with this sort of thing.
1

You can setup your MenuStripCustom class to raise an event when File / Exit is clicked. Your form will need an event handler for that event where it can close itself.

In MenuStripCustom:

Public event ExitClicked

In NavExit_Click:

RaiseEvent ExitClicked

In your form:

Sub Exit() handles MyMenuStripCustomInstance.ExitClicked
    me.close
End Sub

Hope you're getting closer to getting it all working.

2 Comments

Thanks, I am slowly moving forward. I tried your code but I got an error 'Handles clause requires a WithEvents variable defined in the containing type or one of its base types.' For right now I might go with the other answer. I appreciate it the help though ;)
Strange, should work. Go with Form.ActiveForm - it's simpler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.