Build menu at runtime : Menu « GUI « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » GUI » Menu 
14.17.3.Build menu at runtime
Build menu at runtime
'Visual Basic 2005 Cookbook Solutions for VB 2005 Programmers
'by Tim Patrick (Author), John Craig (Author)
'# Publisher: O'Reilly Media, Inc. (September 212006)
'# Language: English
'# ISBN-100596101775
'# ISBN-13978-0596101770


Imports MVB = Microsoft.VisualBasic
Imports System.Windows.Forms

public class BuildYourOwnMenu
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class


Public Class Form1
    Private Declare Auto Function GetPrivateProfileString Lib "kernel32" _
        (ByVal AppName As String, _
        ByVal KeyName As String, _
        ByVal DefaultValue As String, _
        ByVal ReturnedString As System.Text.StringBuilder, _
        ByVal BufferSize As Integer, _
        ByVal FileName As StringAs Integer

    Private Sub MenuExitProgram_Click(ByVal sender As System.Object, ByVal e As System.EventArgsHandles MenuExitProgram.Click
        Me.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgsHandles Me.Load
        Dim favoritesPath As String

        favoritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)
        If (favoritesPath = ""Then Return
        If (My.Computer.FileSystem.DirectoryExists(favoritesPath= FalseThen Return

        BuildFavorites(MenuFavorites, favoritesPath)

        If (MenuFavorites.DropDownItems.Count > 1Then _
            MenuNoFavorites.Visible = False
    End Sub

    Private Sub BuildFavorites(ByVal whichMenu As ToolStripMenuItem, ByVal fromPath As String)
        Dim oneEntry As String
        Dim menuEntry As ToolStripMenuItem
        Dim linkPath As String
        Dim displayName As String

        For Each oneEntry In My.Computer.FileSystem.GetDirectories(fromPath)
            menuEntry = New ToolStripMenuItem_
                My.Computer.FileSystem.GetName(oneEntry))

            BuildFavorites(menuEntry, oneEntry)

            If (menuEntry.DropDownItems.Count > 0Then _
                whichMenu.DropDownItems.Add(menuEntry)
        Next oneEntry

        For Each oneEntry In My.Computer.FileSystem.GetFiles(fromPath, _
                FileIO.SearchOption.SearchTopLevelOnly, "*.url")
            linkPath = GetINIEntry("InternetShortcut""URL", oneEntry)
            If (linkPath <> ""Then
                displayName = My.Computer.FileSystem.GetName(oneEntry)
                displayName = MVB.Left(displayName, displayName.Length - 4)
                menuEntry = New ToolStripMenuItem(displayName)
                menuEntry.Tag = linkPath
                whichMenu.DropDownItems.Add(menuEntry)

                AddHandler menuEntry.Click, AddressOf RunFavoritesLink
            End If
        Next oneEntry
    End Sub

    Private Sub RunFavoritesLink(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' ----- Run the link.
        Dim whichMenu As ToolStripMenuItem

        whichMenu = CType(sender, ToolStripMenuItem)
        Console.WriteLine(whichMenu.Tag)
        'Process.Start(whichMenu.Tag)
    End Sub

    Private Function GetINIEntry(ByVal sectionName As String, _
            ByVal keyName As String, ByVal whichFile As StringAs String
        ' ----- Extract a value from an INI-style file.
        Dim resultLength As Integer
        Dim targetBuffer As New System.Text.StringBuilder(500)

        resultLength = GetPrivateProfileString(sectionName, keyName, "", _
            targetBuffer, targetBuffer.Capacity, whichFile)
        Return targetBuffer.ToString()
    End Function
End Class


<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    '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.MainMenu = New System.Windows.Forms.MenuStrip
        Me.MenuFile = New System.Windows.Forms.ToolStripMenuItem
        Me.MenuExitProgram = New System.Windows.Forms.ToolStripMenuItem
        Me.MenuFavorites = New System.Windows.Forms.ToolStripMenuItem
        Me.MenuNoFavorites = New System.Windows.Forms.ToolStripMenuItem
        Me.MainMenu.SuspendLayout()
        Me.SuspendLayout()
        '
        'MainMenu
        '
        Me.MainMenu.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.MenuFile, Me.MenuFavorites})
        Me.MainMenu.Location = New System.Drawing.Point(00)
        Me.MainMenu.Name = "MainMenu"
        Me.MainMenu.Size = New System.Drawing.Size(29224)
        Me.MainMenu.TabIndex = 0
        Me.MainMenu.Text = "MenuStrip1"
        '
        'MenuFile
        '
        Me.MenuFile.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.MenuExitProgram})
        Me.MenuFile.Name = "MenuFile"
        Me.MenuFile.Size = New System.Drawing.Size(3520)
        Me.MenuFile.Text = "&File"
        '
        'MenuExitProgram
        '
        Me.MenuExitProgram.Name = "MenuExitProgram"
        Me.MenuExitProgram.ShortcutKeys = CType((System.Windows.Forms.Keys.Alt Or System.Windows.Forms.Keys.F4), System.Windows.Forms.Keys)
        Me.MenuExitProgram.Size = New System.Drawing.Size(13222)
        Me.MenuExitProgram.Text = "E&xit"
        '
        'MenuFavorites
        '
        Me.MenuFavorites.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.MenuNoFavorites})
        Me.MenuFavorites.Name = "MenuFavorites"
        Me.MenuFavorites.Size = New System.Drawing.Size(6420)
        Me.MenuFavorites.Text = "Fa&vorites"
        '
        'MenuNoFavorites
        '
        Me.MenuNoFavorites.Enabled = False
        Me.MenuNoFavorites.Name = "MenuNoFavorites"
        Me.MenuNoFavorites.Size = New System.Drawing.Size(11222)
        Me.MenuNoFavorites.Text = "(empty)"
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(292121)
        Me.Controls.Add(Me.MainMenu)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MainMenuStrip = Me.MainMenu
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.Text = "Runtime Menus"
        Me.MainMenu.ResumeLayout(False)
        Me.MainMenu.PerformLayout()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents MainMenu As System.Windows.Forms.MenuStrip
    Friend WithEvents MenuFile As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents MenuExitProgram As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents MenuFavorites As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents MenuNoFavorites As System.Windows.Forms.ToolStripMenuItem

End Class
14.17.Menu
14.17.1.Create Menu in your codeCreate Menu in your code
14.17.2.Add Menu to Form window and attach actionsAdd Menu to Form window and attach actions
14.17.3.Build menu at runtimeBuild menu at runtime
14.17.4.Using menus to change font colors and stylesUsing menus to change font colors and styles
14.17.5.Owner draw menuOwner draw menu
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.