Results 1 to 8 of 8

Thread: [2005] construct menu at runtime

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    137

    [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

  2. #2
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2005] construct menu at runtime

    Quote 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:
    1. Public Class Form1
    2.     Dim file() As String
    3.     Dim folder() As String
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         ReDim file(9999999)
    6.         ReDim folder(9999999)
    7.         getfiles("C:\myfolder\", file, folder)
    8.         ReDim Preserve file(filesamount - 1)
    9.         ReDim Preserve folder(foldersamount - 1)
    10.     End Sub
    11.  
    12.     Dim foldersamount As Integer
    13.     Dim filesamount As Integer
    14.     Private Sub getfiles(ByVal path As String, ByVal allfiles() As String, ByVal allfolders() As String)
    15.         Dim directoryi As New IO.DirectoryInfo(path)
    16.         Dim directorys() As IO.DirectoryInfo = directoryi.GetDirectories
    17.         Dim files() As IO.FileInfo = directoryi.GetFiles
    18.         Dim directory As IO.DirectoryInfo
    19.         Dim file As IO.FileInfo
    20.         For Each file In files
    21.             allfiles(filesamount) = file.FullName
    22.             filesamount += 1
    23.         Next
    24.         For Each directory In directorys
    25.             allfolders(foldersamount) = directory.FullName
    26.             foldersamount += 1
    27.             getfiles(directory.FullName, allfiles, allfolders)
    28.         Next
    29.     End Sub
    30. End Class


    heres an example of creating a menustrip then adding to it.


    VB Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim menu As New MenuStrip 'declare it
    5.         Controls.Add(menu) 'add it to the project
    6.         menu.Show() 'display the menu strip
    7.         menu.Items.Add("Test") 'add the first item
    8.     End Sub
    9. End Class

    not sure on how to add a handler or a child but that should get u started.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    137

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Addicted Member
    Join Date
    May 2006
    Posts
    142

    Re: [2005] construct menu at runtime

    the code is not working. can u make it more clear?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] construct menu at runtime

    VB Code:
    1. Private Sub BuildFileSystemMenu(ByVal parent As ToolStripMenuItem, ByVal folder As String)
    2.     For Each subfolder As String In IO.Directory.GetDirectories(folder)
    3.         Me.BuildFileSystemMenu(DirectCast(parent.DropDownItems.Add(IO.Path.GetFileName(subfolder)), ToolStripMenuItem), subfolder)
    4.     Next subfolder
    5.  
    6.     For Each file As String In IO.Directory.GetFiles(folder)
    7.         parent.DropDownItems.Add(IO.Path.GetFileName(file))
    8.     Next
    9. 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:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     Me.BuildFileSystemMenu(Me.FileToolStripMenuItem, "C:\Documents and Settings\<your user name>\Start Menu")
    3. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    137

    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'.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width