Menu Control

  • Thread starter Thread starter nsreddi
  • Start date Start date
N

nsreddi

I am trying to create a sample prototype on Menu's.I have 3 forms and
on the main form I created a Menu control with New, Adjust and Exit as
Menu controls in the Main Menu bar.

When I click on Menu New control I want to show New form.

I am writing the following code on menu add click.

Private Sub mnuNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuNew.Click
Dim frmNew As New frmNew
frmNew.Show()
Me.Close()
End Sub

When I tap on New menu then I want open New Form, but the application
is exiting.

Can some one tell me where am I going wrong.

Thanks
NS
 
When you close the main form the application will exit. You need to keep the
main form alive. So instead of "Me.Close()" try "Me.Hide()". Of course, you
will need a way to show your main form once you close the secondary form so
you will need to code to handle that case, or you can show your second form
using it's ShowDialog() method.
 
Thanks for the reply.But it's not working.The start up form is behaving
wierdly.Intially when the sub menu new control is tapped the
application is exiting.When i try to open the application again the
next form is opening instead of the main form.

Thanks
NS
 
Are you sure that the application is completely terminated? If you are truly
calling Close() on the main form then your application should be
terminating. If you're seeing odd behavior then ensure that you've got the
latest service pack installed (service pack 3:
http://www.microsoft.com/downloads/...11-194b-4c00-b445-f92bec03032f&displaylang=en).
If you've either already got the latest service pack applied or you're still
seeing odd behavior after installing the service pack, then try going back
to the basics with a very simple two Form system. If you look at the code
below, where Form1 is set as the startup object, tapping the "New" menu item
should cause the application to terminate (check the running programs list
to make sure that the application has exited), and tapping the "Modal" menu
item will show Form2 in a modal state.

Public Class Form1
Inherits System.Windows.Forms.Form

Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

Public Sub New()
MyBase.New()
InitializeComponent()
End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub

Private Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.MenuItem1 = New System.Windows.Forms.MenuItem
Me.MenuItem2 = New System.Windows.Forms.MenuItem
Me.MainMenu1.MenuItems.Add(Me.MenuItem1)
Me.MainMenu1.MenuItems.Add(Me.MenuItem2)
Me.MenuItem1.Text = "New"
Me.MenuItem2.Text = "Modal"
Me.Menu = Me.MainMenu1
Me.Text = "Form1"
End Sub

Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem1.Click
Dim f As New Form2
f.Show()
Me.Close()
End Sub

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click
Dim f As New Form2
f.ShowDialog()
f.Dispose()
End Sub

End Class

Public Class Form2
Inherits System.Windows.Forms.Form

Public Sub New()
MyBase.New()
InitializeComponent()
End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub

Private Sub InitializeComponent()
Me.MinimizeBox = False
Me.Text = "Form2"
End Sub

End Class
 
Thanks..Now it's working.In few forms I can't see the Menu control in
run time which I created , but I can see the menu control in other
forms.Am I missing some thing while creating menu control ??

Thanks
NS
 
It should be a pretty straight forward process - create a MainMenu instance
and set that instance into the Menu property of the Form. Or better yet,
drag and drop a MainMenu from the ToolBox onto the Form and you should be
done.
 
I tried that way too, but still can't see Menu control at the bottom of
the form during runtime.

NS
 
Post the complete code for a Form that is showing this behavior and I'll
take a look at it.
 
Here is the code.

Thanks
NS

Imports System
Imports System.Windows.Forms
Public Class frmScanAsset
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
MyBase.Dispose(disposing)
End Sub

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnEnter As System.Windows.Forms.Button
Friend WithEvents btnNew As System.Windows.Forms.Button
Friend WithEvents MainMenu2 As System.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.btnEnter = New System.Windows.Forms.Button
Me.btnNew = New System.Windows.Forms.Button
Me.MainMenu2 = New System.Windows.Forms.MainMenu
Me.MenuItem1 = New System.Windows.Forms.MenuItem
Me.MenuItem2 = New System.Windows.Forms.MenuItem
Me.MenuItem3 = New System.Windows.Forms.MenuItem
'
'btnEnter
'
Me.btnEnter.Font = New System.Drawing.Font("Tahoma", 8.25!,
System.Drawing.FontStyle.Bold)
Me.btnEnter.Location = New System.Drawing.Point(160, 240)
Me.btnEnter.Size = New System.Drawing.Size(54, 24)
Me.btnEnter.Text = "Adjust"
'
'btnNew
'
Me.btnNew.Font = New System.Drawing.Font("Tahoma", 9.0!,
System.Drawing.FontStyle.Bold)
Me.btnNew.Location = New System.Drawing.Point(16, 240)
Me.btnNew.Size = New System.Drawing.Size(56, 24)
Me.btnNew.Text = "New"
'
'MainMenu2
'
Me.MainMenu2.MenuItems.Add(Me.MenuItem1)
'
'MenuItem1
'
Me.MenuItem1.MenuItems.Add(Me.MenuItem2)
Me.MenuItem1.MenuItems.Add(Me.MenuItem3)
Me.MenuItem1.Text = "Menu"
'
'MenuItem2
'
Me.MenuItem2.Text = "New"
'
'MenuItem3
'
Me.MenuItem3.Text = "Adjust"
'
'frmScanAsset
'
Me.ClientSize = New System.Drawing.Size(240, 272)
Me.ControlBox = False
Me.Controls.Add(Me.btnNew)
Me.Controls.Add(Me.btnEnter)
Me.Menu = Me.MainMenu2
Me.Text = "frmScanAsset"

End Sub

#End Region


Private Sub mnuCancel_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs)
Dim frmMainMenu As New frmMainMenu
Me.Dispose()
frmMainMenu.Show()
End Sub

Private Sub frmScanAsset_Load(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles MyBase.Load

End Sub
Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnEnter.Click
Dim frmAdjust As New frmAdjust
Me.Close()
Cursor.Current = Cursors.WaitCursor
frmAdjust.Show()
Cursor.Current = Cursors.Default
End Sub

Private Sub mnuDetails_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs)
Dim frmAdjust As New frmAdjust
Me.Dispose()
Cursor.Current = Cursors.WaitCursor
frmAdjust.Show()
Cursor.Current = Cursors.Default
End Sub
Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNew.Click
Dim frmNew As New frmNew
Me.Close()
frmNew.Show()
End Sub

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MenuItem2.Click
Dim frmNew As New frmNew
Me.Close()
frmNew.Show()
End Sub

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MenuItem3.Click
Dim frmAdjust As New frmAdjust
Me.Close()
Cursor.Current = Cursors.WaitCursor
frmAdjust.Show()
Cursor.Current = Cursors.Default
End Sub
End Class
 
When I create a new instance of the Form that you posted, and show it using
it's ShowDialog() method, the Form is displayed with the menu at the bottom
as expected. I've got CF SP3 installed on my PPC 2003 based device.
 
Back
Top