Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Help on the Menu Dynamic
 

Help on the Menu Dynamic

Help on the Menu
Dear you!
I have a small program on the menu written in VB6 retrieve data from Acess run very fast and now want to switch to VBNet2008. VBNet2008 conversion is still normal, but when running the menu does not appear that waiting in line, then click on the menu bar of this menu appears, but not the font is in Unicode data is available.
Expect you to help debug programs that run on VBNet2008 still running good access Menu fast as running on VB6. Also supported is Unicode font on VBNet2008 does not function for unicode font.
Attach program with VB6 and VB2008.
Thanks
vietnt@msn.com
Option Strict Off
Option Explicit On
Friend Class frmMainMenu
	Inherits System.Windows.Forms.Form
#Region "Windows Form Designer generated code "
	Public Sub New()
		MyBase.New()
		If m_vb6FormDefInstance Is Nothing Then
			If m_InitializingDefInstance Then
				m_vb6FormDefInstance = Me
			Else
				Try 
					'For the start-up form, the first instance created is the default instance.
					If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
						m_vb6FormDefInstance = Me
					End If
				Catch
				End Try
			End If
		End If
		'This call is required by the Windows Form Designer.
		InitializeComponent()
	End Sub
	'Form overrides dispose to clean up the component list.
	Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
		If Disposing Then
			If Not components Is Nothing Then
				components.Dispose()
			End If
		End If
		MyBase.Dispose(Disposing)
	End Sub
	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer
	Public ToolTip1 As System.Windows.Forms.ToolTip
	Public WithEvents Menu_Renamed As System.Windows.Forms.MenuItem
	Public MainMenu1 As System.Windows.Forms.MainMenu
	'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.
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.Menu_Renamed = New System.Windows.Forms.MenuItem
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.Menu_Renamed})
        '
        'Menu_Renamed
        '
        Me.Menu_Renamed.Index = 0
        Me.Menu_Renamed.MdiList = True
        Me.Menu_Renamed.Text = "Menu"
        '
        'frmMainMenu
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.SystemColors.Control
        Me.ClientSize = New System.Drawing.Size(467, 332)
        Me.Cursor = System.Windows.Forms.Cursors.Default
        Me.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Location = New System.Drawing.Point(15, 37)
        Me.Menu = Me.MainMenu1
        Me.Name = "frmMainMenu"
        Me.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Form1"
        Me.WindowState = System.Windows.Forms.FormWindowState.Maximized

    End Sub
#End Region 
#Region "Upgrade Support "
	Private Shared m_vb6FormDefInstance As frmMainMenu
	Private Shared m_InitializingDefInstance As Boolean
	Public Shared Property DefInstance() As frmMainMenu
		Get
			If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
				m_InitializingDefInstance = True
				m_vb6FormDefInstance = New frmMainMenu()
				m_InitializingDefInstance = False
			End If
			DefInstance = m_vb6FormDefInstance
		End Get
		Set
			m_vb6FormDefInstance = Value
		End Set
	End Property
#End Region 
	'khai boo cac ham API can dung
	Private Declare Function GetMenu Lib "user32" (ByVal hand As Integer) As Integer
	Private Declare Function CreatePopupMenu Lib "user32" () As Integer
	Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
    'UPGRADE_WARNING: Structure MENUITEMINFO may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"'
    Private Declare Function InsertMenultem Lib "user32" Alias "InsertMenuItemA" (ByVal hMenu As Integer, ByVal un As String, ByVal bool As Boolean, ByRef lpcMenulteminfo As MENUITEMINFO) As Integer 'ByRef
	'khai bdo kieu can dung
	
	Private Structure MENUITEMINFO
		Dim cbSize As Integer
		Dim fMask As Integer
		Dim tType As Integer
		Dim fState As Integer
		Dim wID As Integer
		Dim hSubMenu As Integer
		Dim hbmpChecked As Integer
		Dim librnpUncliecked As Integer
		Dim dwltemData As Integer
		Dim dwTypeData As String
		Dim cch As Integer
	End Structure
	
	'khaibaohangcandung
	Const MF_STRING As Short = &H40s ' &H0
	Const MIIM_STRING As Short = &H40s
	Const MIIM_SUBMENU As Short = &H4s
	Const MIIM_ID As Short = &H2s
	Const MIIM_TYPE As Short = &H10s
	Const MIIM_Data As Short = &H20s
	Const MF_BYPOSITION As Integer = &H400
	
	'Khai bao cac bien can dung
	Dim MII As MENUITEMINFO
    Dim cnn As ADODB.Connection
    Dim cmd As ADODB.Command
	Dim RecordSet1 As ADODB.Recordset
    Dim RecordSet2 As ADODB.Recordset
    Dim RecordSet3 As ADODB.Recordset
    Private MyConString As String
	'Thu tuc khoi dong form ung dung
	Private Sub frmMainMenu_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim ref As Object
		Dim kq As Object
		Dim hMenu As Integer
		Dim hSubMenu As Integer
		'xac Binh menubarcua Form
		hMenu = GetMenu(Me.Handle.ToInt32)
		'xoa menu pop-up da co
        kq = DeleteMenu(hMenu, 0, MF_BYPOSITION)
        'Ket noi co so du lieu
        ConnectData()

        'MENU CAP 1
        cmd = New ADODB.Command
        cmd.let_ActiveConnection(cnn)
        cmd.CommandText = "select * from Schucnang  where cap=1 order by ma ASC"
        RecordSet1 = cmd.Execute()
        While Not RecordSet1.EOF
            MII.cbSize = Len(MII)
            MII.dwTypeData = RecordSet1.Fields("tenchucnang").Value
            MII.cch = Len(MII.dwTypeData)
            MII.hSubMenu = CreatePopupMenu()
            MII.fMask = MIIM_ID Or MIIM_STRING Or MIIM_SUBMENU
            ref = InsertMenultem(hMenu, RecordSet1.Fields("ma").Value, MF_BYPOSITION, MII)

            'MENU CAP 2
            hSubMenu = MII.hSubMenu
            cmd.CommandText = "select * from Schucnang where  cap=2 and  ID = " & RecordSet1.Fields("ID").Value & " order by ma ASC"
            RecordSet2 = cmd.Execute()
            While Not RecordSet2.EOF()
                MII.cbSize = Len(MII)
                MII.dwTypeData = RecordSet2.Fields("tenchucnang").Value
                MII.cch = Len(MII.dwTypeData)
                MII.fMask = MIIM_STRING
                ref = InsertMenultem(hSubMenu, RecordSet2.Fields("ma").Value, MF_BYPOSITION, MII)
               
                RecordSet2.MoveNext()
            End While
            RecordSet1.MoveNext()
        End While
        'dong tac doi tuong da dung lai
        RecordSet1.Close()
        RecordSet2.Close()
        cnn.Close()
    End Sub
    Private Sub ConnectData()
        MyConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = D:\prjMenu\datamenu.mdb"
        cnn = New ADODB.Connection
        cnn.Open(MyConString)
    End Sub
End Class

Viet, nguyen tan  Tuesday, July 21, 2009 1:36 AM

Hi Viet,

I have tested your code and found two issues:

1. The menus are note displayed correctly. Only the first main menu is displayed and its text is 'Menu'. But what we want is all the menus are displayed and the text of them ought to be the data in the Tenchucnang column.

2. The special characters are not displayed correctly which are replaced with '?'. For example: ' Danh Mục' is changed into ' Danh M?c�

Since we are programming in .Net, I suggest that we’d better not call windows API(such as InsertMenultem) to add menus, but directly use the classes and methods in .Net library to do that. This is the modified code snippet:

Option Strict Off
Option Explicit On
Imports System.IO

Friend Class frmMainMenu
    Inherits System.Windows.Forms.Form
#Region "Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        If m_vb6FormDefInstance Is Nothing Then
            If m_InitializingDefInstance Then
                m_vb6FormDefInstance = Me
            Else
                Try
                    'For the start-up form, the first instance created is the default instance.
                    If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
                        m_vb6FormDefInstance = Me
                    End If
                Catch
                End Try
            End If
        End If
        'This call is required by the Windows Form Designer.
        InitializeComponent()
    End Sub
    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
        If Disposing Then
            If Not components Is Nothing Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(Disposing)
    End Sub
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    Public ToolTip1 As System.Windows.Forms.ToolTip
    Public MainMenu1 As System.Windows.Forms.MainMenu
    '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.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
        Me.MainMenu1 = New System.Windows.Forms.MainMenu(Me.components)
        Me.Menu_Renamed = New System.Windows.Forms.MenuItem
        Me.SuspendLayout()
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.Menu_Renamed})
        '
        'Menu_Renamed
        '
        Me.Menu_Renamed.Index = 0
        Me.Menu_Renamed.MdiList = True
        Me.Menu_Renamed.Text = "Menu"
        '
        'frmMainMenu
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.SystemColors.Control
        Me.ClientSize = New System.Drawing.Size(467, 332)
        Me.Cursor = System.Windows.Forms.Cursors.Default
        Me.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Location = New System.Drawing.Point(15, 37)
        Me.Menu = Me.MainMenu1
        Me.Name = "frmMainMenu"
        Me.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Form1"
        Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
        Me.ResumeLayout(False)

    End Sub
#End Region
#Region "Upgrade Support "
    Private Shared m_vb6FormDefInstance As frmMainMenu
    Private Shared m_InitializingDefInstance As Boolean
    Public Shared Property DefInstance() As frmMainMenu
        Get
            If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
                m_InitializingDefInstance = True
                m_vb6FormDefInstance = New frmMainMenu()
                m_InitializingDefInstance = False
            End If
            DefInstance = m_vb6FormDefInstance
        End Get
        Set(ByVal value As frmMainMenu)
            m_vb6FormDefInstance = value
        End Set
    End Property
#End Region

    Dim cnn As ADODB.Connection
    Dim cmd As ADODB.Command
    Dim RecordSet1 As ADODB.Recordset
    Dim RecordSet2 As ADODB.Recordset
    Dim RecordSet3 As ADODB.Recordset
    Private MyConString As String
    'Thu tuc khoi dong form ung dung
    Private Sub frmMainMenu_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        
        'Ket noi co so du lieu
        ConnectData()
        'Clear the menu items.
        Me.MainMenu1.MenuItems.Clear()
        'MENU CAP 1
        cmd = New ADODB.Command
        cmd.let_ActiveConnection(cnn)
        cmd.CommandText = "select * from Schucnang  where cap=1 order by ma ASC"
        RecordSet1 = cmd.Execute()
        While Not RecordSet1.EOF
            'Add the main menu.
            Dim mainMenu As New MenuItem()
            mainMenu.Name = CType(RecordSet1.Fields("ma").Value, String)
            mainMenu.Text = RecordSet1.Fields("tenchucnang").Value
            Me.MainMenu1.MenuItems.Add(mainMenu)

            'MENU CAP 2
            cmd.CommandText = "select * from Schucnang where  cap=2 and  ID = " & RecordSet1.Fields("ID").Value & " order by ma ASC"
            RecordSet2 = cmd.Execute()
            While Not RecordSet2.EOF()
                'Add the child menu.
                Dim subMenu As New MenuItem()
                subMenu.Name = CType(RecordSet2.Fields("ma").Value, String)
                subMenu.Text = RecordSet2.Fields("tenchucnang").Value
                mainMenu.MenuItems.Add(subMenu)

                RecordSet2.MoveNext()
            End While

            RecordSet1.MoveNext()
        End While
        'dong tac doi tuong da dung lai
        RecordSet1.Close()
        RecordSet2.Close()
        cnn.Close()
    End Sub
    Private Sub ConnectData()
        MyConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Users\v-shunli\Downloads\datamenu.mdb"
        cnn = New ADODB.Connection
        cnn.Open(MyConString)
    End Sub
End Class


You can get more information about MenuMenu from
http://msdn.microsoft.com/en-us/library/system.windows.forms.mainmenu.aspx.

By the way, we have a new menu control named MenuStrip which has richer functions than MainMenu. You can get more information about this control from:
http://msdn.microsoft.com/en-us/library/system.windows.forms.menustrip.aspx.

Let me know if this helps.

Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, July 22, 2009 9:03 AM

Hi Viet,

Could email the file "datamenu.mdb" to me? This is my email: alala666888@hotmail.com. This would help me to debug your program. Thanks.

Regards,

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, July 22, 2009 7:28 AM

Hi Viet,

I have tested your code and found two issues:

1. The menus are note displayed correctly. Only the first main menu is displayed and its text is 'Menu'. But what we want is all the menus are displayed and the text of them ought to be the data in the Tenchucnang column.

2. The special characters are not displayed correctly which are replaced with '?'. For example: ' Danh Mục' is changed into ' Danh M?c�

Since we are programming in .Net, I suggest that we’d better not call windows API(such as InsertMenultem) to add menus, but directly use the classes and methods in .Net library to do that. This is the modified code snippet:

Option Strict Off
Option Explicit On
Imports System.IO

Friend Class frmMainMenu
    Inherits System.Windows.Forms.Form
#Region "Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        If m_vb6FormDefInstance Is Nothing Then
            If m_InitializingDefInstance Then
                m_vb6FormDefInstance = Me
            Else
                Try
                    'For the start-up form, the first instance created is the default instance.
                    If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
                        m_vb6FormDefInstance = Me
                    End If
                Catch
                End Try
            End If
        End If
        'This call is required by the Windows Form Designer.
        InitializeComponent()
    End Sub
    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
        If Disposing Then
            If Not components Is Nothing Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(Disposing)
    End Sub
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    Public ToolTip1 As System.Windows.Forms.ToolTip
    Public MainMenu1 As System.Windows.Forms.MainMenu
    '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.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
        Me.MainMenu1 = New System.Windows.Forms.MainMenu(Me.components)
        Me.Menu_Renamed = New System.Windows.Forms.MenuItem
        Me.SuspendLayout()
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.Menu_Renamed})
        '
        'Menu_Renamed
        '
        Me.Menu_Renamed.Index = 0
        Me.Menu_Renamed.MdiList = True
        Me.Menu_Renamed.Text = "Menu"
        '
        'frmMainMenu
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.SystemColors.Control
        Me.ClientSize = New System.Drawing.Size(467, 332)
        Me.Cursor = System.Windows.Forms.Cursors.Default
        Me.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Location = New System.Drawing.Point(15, 37)
        Me.Menu = Me.MainMenu1
        Me.Name = "frmMainMenu"
        Me.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Form1"
        Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
        Me.ResumeLayout(False)

    End Sub
#End Region
#Region "Upgrade Support "
    Private Shared m_vb6FormDefInstance As frmMainMenu
    Private Shared m_InitializingDefInstance As Boolean
    Public Shared Property DefInstance() As frmMainMenu
        Get
            If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
                m_InitializingDefInstance = True
                m_vb6FormDefInstance = New frmMainMenu()
                m_InitializingDefInstance = False
            End If
            DefInstance = m_vb6FormDefInstance
        End Get
        Set(ByVal value As frmMainMenu)
            m_vb6FormDefInstance = value
        End Set
    End Property
#End Region

    Dim cnn As ADODB.Connection
    Dim cmd As ADODB.Command
    Dim RecordSet1 As ADODB.Recordset
    Dim RecordSet2 As ADODB.Recordset
    Dim RecordSet3 As ADODB.Recordset
    Private MyConString As String
    'Thu tuc khoi dong form ung dung
    Private Sub frmMainMenu_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        
        'Ket noi co so du lieu
        ConnectData()
        'Clear the menu items.
        Me.MainMenu1.MenuItems.Clear()
        'MENU CAP 1
        cmd = New ADODB.Command
        cmd.let_ActiveConnection(cnn)
        cmd.CommandText = "select * from Schucnang  where cap=1 order by ma ASC"
        RecordSet1 = cmd.Execute()
        While Not RecordSet1.EOF
            'Add the main menu.
            Dim mainMenu As New MenuItem()
            mainMenu.Name = CType(RecordSet1.Fields("ma").Value, String)
            mainMenu.Text = RecordSet1.Fields("tenchucnang").Value
            Me.MainMenu1.MenuItems.Add(mainMenu)

            'MENU CAP 2
            cmd.CommandText = "select * from Schucnang where  cap=2 and  ID = " & RecordSet1.Fields("ID").Value & " order by ma ASC"
            RecordSet2 = cmd.Execute()
            While Not RecordSet2.EOF()
                'Add the child menu.
                Dim subMenu As New MenuItem()
                subMenu.Name = CType(RecordSet2.Fields("ma").Value, String)
                subMenu.Text = RecordSet2.Fields("tenchucnang").Value
                mainMenu.MenuItems.Add(subMenu)

                RecordSet2.MoveNext()
            End While

            RecordSet1.MoveNext()
        End While
        'dong tac doi tuong da dung lai
        RecordSet1.Close()
        RecordSet2.Close()
        cnn.Close()
    End Sub
    Private Sub ConnectData()
        MyConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Users\v-shunli\Downloads\datamenu.mdb"
        cnn = New ADODB.Connection
        cnn.Open(MyConString)
    End Sub
End Class


You can get more information about MenuMenu from
http://msdn.microsoft.com/en-us/library/system.windows.forms.mainmenu.aspx.

By the way, we have a new menu control named MenuStrip which has richer functions than MainMenu. You can get more information about this control from:
http://msdn.microsoft.com/en-us/library/system.windows.forms.menustrip.aspx.

Let me know if this helps.

Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, July 22, 2009 9:03 AM
Hi Aland Li,
I was running the program on his menu dynamic, but show how the problem is when it does run skip jump menu from a menu, why it does not end once the well.
Also language issues in the data copy it so that no problems.
He can see to help this problem are not.
Thanked him much.
Vietnt
Viet, nguyen tan  Tuesday, July 28, 2009 3:26 AM

Hi Viet,

Do you mean my code snippet does not meet your needs? Could you tell me what problem you meet now?

Best regards,

Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Tuesday, July 28, 2009 3:34 AM
Hi Aland Li,
Not what your code is or, but there is a problem when running the menu it jumps every time the menu and then new strategies all.
I think there is data or export to loop on menu level 1, level 2 menu make menu appear brief time when running the program.
You can run the program find the problem.

Thanks!
Vietnt
Viet, nguyen tan  Tuesday, July 28, 2009 3:56 AM

You can use google to search for other answers

Custom Search

More Threads

• Custom DocumentDesigner for a Windows Form and some issues
• Confusion over when <File>.Designer.cs
• How to display Child Form besides a tree-view column.
• ISupportInitialize and Nested Components
• Problem with DataGridView CustomControl
• possible to use the UserControl designer for a custom control?
• Windows form control
• How to generate pictureboxes from a loop?
• Creating a buddy Icon Class
• How to get the ListView header name in C#?