'Visual Basic 2005 Cookbook Solutions for VB 2005 Programmers
'by Tim Patrick (Author), John Craig (Author)
'# Publisher: O'Reilly Media, Inc. (September 21, 2006)
'# Language: English
'# ISBN-10: 0596101775
'# ISBN-13: 978-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 String) As Integer
Private Sub MenuExitProgram_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuExitProgram.Click
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim favoritesPath As String
favoritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)
If (favoritesPath = "") Then Return
If (My.Computer.FileSystem.DirectoryExists(favoritesPath) = False) Then Return
BuildFavorites(MenuFavorites, favoritesPath)
If (MenuFavorites.DropDownItems.Count > 1) Then _
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 > 0) Then _
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 String) As 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
_
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
_
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.
_
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(0, 0)
Me.MainMenu.Name = "MainMenu"
Me.MainMenu.Size = New System.Drawing.Size(292, 24)
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(35, 20)
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(132, 22)
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(64, 20)
Me.MenuFavorites.Text = "Fa&vorites"
'
'MenuNoFavorites
'
Me.MenuNoFavorites.Enabled = False
Me.MenuNoFavorites.Name = "MenuNoFavorites"
Me.MenuNoFavorites.Size = New System.Drawing.Size(112, 22)
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(292, 121)
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