Open File Menu List for SDI Frame
Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.IO
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim myform As Form = New Form1()
Application.Run(myform)
End Sub
End Class
Public Class OpenFileList
Private m_ApplicationName As String
Private m_FileMenu As ToolStripMenuItem
Private m_NumEntries As Integer
Private m_FileNames As Collection
Private m_MenuItems As Collection
Public Event OpenFile(ByVal file_name As String)
Public Sub New(ByVal application_name As String, ByVal file_menu As ToolStripMenuItem, ByVal num_entries As Integer)
m_ApplicationName = application_name
m_FileMenu = file_menu
m_NumEntries = num_entries
m_FileNames = New Collection
m_MenuItems = New Collection
LoadOpenFileList()
DisplayOpenFileList()
End Sub
Private Sub LoadOpenFileList()
Dim file_name As String
For i As Integer = 1 To m_NumEntries
file_name = GetSetting(m_ApplicationName,"OpenFileList", "FileName" & i, "")
If file_name.Length > 0 Then
m_FileNames.Add(file_name, file_name)
End If
Next i
End Sub
Private Sub SaveOpenFileList()
' Remove previous entries.
If GetSetting(m_ApplicationName, "OpenFileList","FileName1", "").Length > 0 Then
DeleteSetting(m_ApplicationName, "OpenFileList")
End If
For i As Integer = 1 To m_FileNames.Count
SaveSetting(m_ApplicationName,"OpenFileList", "FileName" & i, _
m_FileNames(i).ToString)
Next i
End Sub
Private Sub DisplayOpenFileList()
For Each mnu As ToolStripItem In m_MenuItems
m_FileMenu.DropDownItems.Remove(mnu)
Next mnu
m_MenuItems = New Collection
If m_FileNames.Count > 0 Then
Dim sep As New ToolStripSeparator()
m_MenuItems.Add(sep)
m_FileMenu.DropDownItems.Add(sep)
Dim mnu As ToolStripMenuItem
For i As Integer = 1 To m_FileNames.Count
mnu = New ToolStripMenuItem()
mnu.Text = "&" & i & " " & _
New FileInfo(m_FileNames(i).ToString).Name
AddHandler mnu.Click, AddressOf MruItem_Click
m_MenuItems.Add(mnu)
m_FileMenu.DropDownItems.Add(mnu)
Next i
End If
End Sub
Private Sub MruItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim mnu As ToolStripMenuItem
mnu = DirectCast(sender, ToolStripMenuItem)
For i As Integer = 1 To m_FileNames.Count
If m_MenuItems(i + 1) Is mnu Then
RaiseEvent OpenFile(m_FileNames(i).ToString)
Exit For
End If
Next i
End Sub
Public Sub Add(ByVal file_name As String)
Dim i As Integer = FileNameIndex(file_name)
If i > 0 Then m_FileNames.Remove(i)
If m_FileNames.Count > 0 Then
m_FileNames.Add(file_name, file_name, m_FileNames.Item(1))
Else
m_FileNames.Add(file_name, file_name)
End If
If m_FileNames.Count > m_NumEntries Then
m_FileNames.Remove(m_NumEntries + 1)
End If
DisplayOpenFileList()
SaveOpenFileList()
End Sub
Private Function FileNameIndex(ByVal file_name As String) As Integer
For i As Integer = 1 To m_FileNames.Count
If m_FileNames(i).ToString = file_name Then Return i
Next i
Return 0
End Function
Public Sub Remove(ByVal file_name As String)
Dim i As Integer = FileNameIndex(file_name)
If i > 0 Then
m_FileNames.Remove(i)
DisplayOpenFileList()
SaveOpenFileList()
End If
End Sub
End Class
Public Class Form1
Private WithEvents m_OpenFileList As OpenFileList
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
m_OpenFileList = New OpenFileList("OpenFileList", mnuFile, 4)
End Sub
Private Sub mnuFileOpen_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuFileOpen.Click
If dlgOpen.ShowDialog() = Windows.Forms.DialogResult.OK Then
OpenFile(dlgOpen.FileName)
End If
End Sub
Private Sub m_OpenFileList_OpenFile(ByVal file_name As String) _
Handles m_OpenFileList.OpenFile
OpenFile(file_name)
End Sub
Private Sub OpenFile(ByVal file_name As String)
txtContents.Text = File.ReadAllText(file_name)
txtContents.Select(0, 0)
m_OpenFileList.Add(file_name)
Me.Text = "# [" & New FileInfo(file_name).Name & "]"
End Sub
End Class
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Public 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.MenuStrip1 = New System.Windows.Forms.MenuStrip
Me.mnuFile = New System.Windows.Forms.ToolStripMenuItem
Me.mnuFileOpen = New System.Windows.Forms.ToolStripMenuItem
Me.txtContents = New System.Windows.Forms.TextBox
Me.dlgOpen = New System.Windows.Forms.OpenFileDialog
Me.MenuStrip1.SuspendLayout()
Me.SuspendLayout()
'
'MenuStrip1
'
Me.MenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuFile})
Me.MenuStrip1.Location = New System.Drawing.Point(0, 0)
Me.MenuStrip1.Name = "MenuStrip1"
Me.MenuStrip1.Size = New System.Drawing.Size(292, 24)
Me.MenuStrip1.TabIndex = 9
Me.MenuStrip1.Text = "MenuStrip1"
'
'mnuFile
'
Me.mnuFile.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuFileOpen})
Me.mnuFile.Name = "mnuFile"
Me.mnuFile.Size = New System.Drawing.Size(44, 20)
Me.mnuFile.Text = "&File"
'
'mnuFileOpen
'
Me.mnuFileOpen.Name = "mnuFileOpen"
Me.mnuFileOpen.ShortcutKeys = CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.O), System.Windows.Forms.Keys)
Me.mnuFileOpen.Size = New System.Drawing.Size(168, 22)
Me.mnuFileOpen.Text = "&Open"
'
'txtContents
'
Me.txtContents.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtContents.Location = New System.Drawing.Point(0, 24)
Me.txtContents.Multiline = True
Me.txtContents.Name = "txtContents"
Me.txtContents.Size = New System.Drawing.Size(292, 249)
Me.txtContents.TabIndex = 8
'
'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, 273)
Me.Controls.Add(Me.txtContents)
Me.Controls.Add(Me.MenuStrip1)
Me.Name = "Form1"
Me.Text = "SdiEditMRU []"
Me.MenuStrip1.ResumeLayout(False)
Me.MenuStrip1.PerformLayout()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents MenuStrip1 As System.Windows.Forms.MenuStrip
Friend WithEvents mnuFile As System.Windows.Forms.ToolStripMenuItem
Friend WithEvents mnuFileOpen As System.Windows.Forms.ToolStripMenuItem
Friend WithEvents txtContents As System.Windows.Forms.TextBox
Friend WithEvents dlgOpen As System.Windows.Forms.OpenFileDialog
End Class
Related examples in the same category