|
-
Jun 7th, 2006, 11:58 AM
#1
Thread Starter
Addicted Member
[2005] construct menu at runtime
I wish to construct a menu within my mnuOne menu item at runtime.
I want to do it based on the structure of the folders,
files within folders are structured as follows:
Line1: Name
Line1: Location
Which would be opened using FileOpen.
How would I construct the menu at runtime in VB which creates a menu item for each file and folder in location "c:/myfolder"
E.g.
Directory c:/myfolder structure is as follows:
c:/myfolder/fil1.txt
c:/myfolder/file2.txt
c:/myfolder/directory/file1.txt
c:/myfolder/dir2/fil1.txt
I would want a menu as follows:
mnuOne/filename
mnuOne/file2name
mnuOne/directory/file1name
mnuOne/dir2/file1name
Thanks
-
Jun 7th, 2006, 02:29 PM
#2
Frenzied Member
Re: [2005] construct menu at runtime
 Originally Posted by arcon5
I wish to construct a menu within my mnuOne menu item at runtime.
I want to do it based on the structure of the folders,
files within folders are structured as follows:
Line1: Name
Line1: Location
Which would be opened using FileOpen.
How would I construct the menu at runtime in VB which creates a menu item for each file and folder in location "c:/myfolder"
E.g.
Directory c:/myfolder structure is as follows:
c:/myfolder/fil1.txt
c:/myfolder/file2.txt
c:/myfolder/directory/file1.txt
c:/myfolder/dir2/fil1.txt
I would want a menu as follows:
mnuOne/filename
mnuOne/file2name
mnuOne/directory/file1name
mnuOne/dir2/file1name
Thanks
well to get all the folders and names i suggest using the function i made.
(supply it with 2 arrays to put the paths in.)(example)
VB Code:
Public Class Form1
Dim file() As String
Dim folder() As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ReDim file(9999999)
ReDim folder(9999999)
getfiles("C:\myfolder\", file, folder)
ReDim Preserve file(filesamount - 1)
ReDim Preserve folder(foldersamount - 1)
End Sub
Dim foldersamount As Integer
Dim filesamount As Integer
Private Sub getfiles(ByVal path As String, ByVal allfiles() As String, ByVal allfolders() As String)
Dim directoryi As New IO.DirectoryInfo(path)
Dim directorys() As IO.DirectoryInfo = directoryi.GetDirectories
Dim files() As IO.FileInfo = directoryi.GetFiles
Dim directory As IO.DirectoryInfo
Dim file As IO.FileInfo
For Each file In files
allfiles(filesamount) = file.FullName
filesamount += 1
Next
For Each directory In directorys
allfolders(foldersamount) = directory.FullName
foldersamount += 1
getfiles(directory.FullName, allfiles, allfolders)
Next
End Sub
End Class
heres an example of creating a menustrip then adding to it.
VB Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim menu As New MenuStrip 'declare it
Controls.Add(menu) 'add it to the project
menu.Show() 'display the menu strip
menu.Items.Add("Test") 'add the first item
End Sub
End Class
not sure on how to add a handler or a child but that should get u started.
-
Jun 8th, 2006, 12:20 AM
#3
Thread Starter
Addicted Member
Re: [2005] construct menu at runtime
So, that allows me to create a menu item, but what about creating a menu item within a menu item?
For example,
New Menu/menu item/submenu item
thanks
-
Jun 8th, 2006, 12:51 AM
#4
Re: [2005] construct menu at runtime
Every menu item has a DropDownItems property. Create a menu item and Add it to its intended parent's DropDownItems collection. Alternatively you can add a number of items to a ContextMenuStrip and assign that to the parent's DropDown property, which attaches the entire menu.
-
Jun 9th, 2006, 05:03 AM
#5
Addicted Member
Re: [2005] construct menu at runtime
the code is not working. can u make it more clear?
-
Jun 9th, 2006, 05:12 AM
#6
Re: [2005] construct menu at runtime
VB Code:
Private Sub BuildFileSystemMenu(ByVal parent As ToolStripMenuItem, ByVal folder As String)
For Each subfolder As String In IO.Directory.GetDirectories(folder)
Me.BuildFileSystemMenu(DirectCast(parent.DropDownItems.Add(IO.Path.GetFileName(subfolder)), ToolStripMenuItem), subfolder)
Next subfolder
For Each file As String In IO.Directory.GetFiles(folder)
parent.DropDownItems.Add(IO.Path.GetFileName(file))
Next
End Sub
You call this method by passing it the menu item that will be the root and the path of the root folder, e.g.
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.BuildFileSystemMenu(Me.FileToolStripMenuItem, "C:\Documents and Settings\<your user name>\Start Menu")
End Sub
-
Jun 9th, 2006, 11:13 AM
#7
Thread Starter
Addicted Member
Re: [2005] construct menu at runtime
for the line:
Me.BuildFileSystemMenu(Me.FileToolStripMenuItem, "C:\Documents and Settings\<your user name>\Start Menu")
Me.FileToolStripMenuItem error:
'FileToolStripMenuItem' is not a member of 'IE.Explorer'.
-
Jun 9th, 2006, 10:01 PM
#8
Re: [2005] construct menu at runtime
See, this is why I prefer to post instructions instead of code. If you post code people just copy and paste without engaging their mind. I'm guessing that you don't have a folder named "C:\Documents and Settings\<your user name>\Start Menu" either. I said that that was an example. I also said that you pass the ToolStripMenuItem that is supposed to be the root of the menu. What menu item do YOU want to be the root of YOUR menu?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|