Results 1 to 10 of 10

Thread: [2005] Simple Question

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2005
    Location
    Philadelphia
    Posts
    48

    [2005] Simple Question

    What i want to do is simple but i just cant get it right so i decided to ask. Okay I have a menu strip and in one of the drop downs i wanted to have it so the items are are made from folders in the application start up path, sorry if i wasnt clear enough i didnt really know how to explain it.
    Last edited by Royal; Apr 22nd, 2006 at 03:14 AM.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    266

    Re: [2005] Simple Question

    do u want to select items from drop down and add it in the the menu strip....
    or u want to add those items in the start up path plz try to explain more

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2005
    Location
    Philadelphia
    Posts
    48

    Re: [2005] Simple Question

    i want it to add the items in on the form_load, but i dont want the user to add it in i want it to do its self by the folders in the startup path

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,448

    Re: [2005] Simple Question

    VB Code:
    1. 'Get the file names.
    2.         Dim files As String() = IO.Directory.GetFiles(Application.StartupPath)
    3.  
    4.         'Creat an array for the menu items the same size as the array of file names.
    5.         Dim menuItems(files.GetUpperBound(0)) As ToolStripMenuItem
    6.  
    7.         'Strip the path information and create menu items.
    8.         For i As Integer = 0 To files.GetUpperBound(0) Step 1
    9.             'You would probably want to use a different constructor to specify a Click event handler.
    10.             menuItems(i) = New ToolStripMenuItem(IO.Path.GetFileName(files(i)))
    11.         Next i
    12.  
    13.         'Add the new menu items to which ever existing item is to be their parent.
    14.         Me.FilesToolStripMenuItem.DropDownItems.AddRange(menuItems)
    You could also use My.Computer.FileSystem.GetFiles but an array is more efficient than a ReadOnlyCollection object.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,448

    Re: [2005] Simple Question

    Ah... reread your post and realised that what you want is a bit more complex. I'll try again and repost.

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2005
    Location
    Philadelphia
    Posts
    48

    Re: [2005] Simple Question

    Thanks man! , but yeah i did want folders instead of files.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,448

    Re: [2005] Simple Question

    Voila!
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim rootFolder As String 'The path of the folder at which to start goes here.
    3.  
    4.         Me.AddFolderAndFileMenuItems(Me.FilesToolStripMenuItem, rootFolder)
    5.     End Sub
    6.  
    7.     ''' <summary>
    8.     ''' Adds menu items for each subfolder and file.
    9.     ''' </summary>
    10.     ''' <param name="parentItem">
    11.     ''' The menu item to which to add the drop down items.
    12.     ''' </param>
    13.     ''' <param name="folder">
    14.     ''' The folder to search.
    15.     ''' </param>
    16.     ''' <remarks>
    17.     ''' This method is called recursively.
    18.     ''' </remarks>
    19.     Private Sub AddFolderAndFileMenuItems(ByVal parentItem As ToolStripMenuItem, ByVal folder As String)
    20.         'Get the subfolders.
    21.         Dim subfolders As String() = IO.Directory.GetDirectories(folder)
    22.  
    23.         'Get the files.
    24.         Dim files As String() = IO.Directory.GetFiles(folder)
    25.  
    26.         'Creat an array for the menu items.
    27.         Dim menuItems(subfolders.Length + files.Length - 1) As ToolStripMenuItem
    28.  
    29.         Dim itemIndex As Integer = 0
    30.  
    31.         'Create menu items for the subfolders.
    32.         For i As Integer = 0 To subfolders.GetUpperBound(0) Step 1
    33.             menuItems(itemIndex) = New ToolStripMenuItem(IO.Path.GetFileName(subfolders(i)))
    34.  
    35.             'Store the full path.
    36.             menuItems(itemIndex).Tag = subfolders(i)
    37.  
    38.             'Call the method recursively to build the tree.
    39.             Me.AddFolderAndFileMenuItems(menuItems(itemIndex), subfolders(i))
    40.  
    41.             itemIndex += 1
    42.         Next i
    43.  
    44.         'Create menu items for the files.
    45.         For i As Integer = 0 To files.GetUpperBound(0) Step 1
    46.             'Connect the method that will be used to handle the Click events.
    47.             menuItems(itemIndex) = New ToolStripMenuItem(IO.Path.GetFileName(files(i)), _
    48.                                                          Nothing, _
    49.                                                          New EventHandler(AddressOf OpenFile))
    50.  
    51.             'Store the full path.
    52.             menuItems(itemIndex).Tag = files(i)
    53.  
    54.             itemIndex += 1
    55.         Next i
    56.  
    57.         'Add the new menu items to the parent.
    58.         parentItem.DropDownItems.AddRange(menuItems)
    59.     End Sub
    60.  
    61.  
    62.     ''' <summary>
    63.     ''' The event handler for the Click event of each file item.
    64.     ''' </summary>
    65.     ''' <param name="sender"></param>
    66.     ''' <param name="e"></param>
    67.     ''' <remarks>
    68.     ''' The parameters are required to match the event handler signature.
    69.     ''' </remarks>
    70.     Private Sub OpenFile(ByVal sender As System.Object, ByVal e As System.EventArgs)
    71.         'Open the file that the selected menu item represents.
    72.         Process.Start(CStr(DirectCast(sender, ToolStripMenuItem).Tag))
    73.     End Sub
    You could also specify an image for each menu item when you create it but I'll leave that up to you.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,448

    Re: [2005] Simple Question

    Oh. I reread your posts again and I'm thinking you wanted just the folders, rather than folders and files maybe. Anyway, you get the idea. You can adapt what I've done to your exact needs.

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2005
    Location
    Philadelphia
    Posts
    48

    Re: [2005] Simple Question

    Well the one before that worked for me but thanks for the other to but while were here instead of me making a new thread, Anyway now that i have it loading the folders i wanted to make it when the user clicks the item it loads the contents of that folder into a listbox, sorry if i am asking alot to day trying to finish up this app and these are the little things i cant figure out.

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,448

    Re: [2005] Simple Question

    Well, the code I've posted so far shows you how to attach an event handler to the menu items, and also how to get all the files in a folder and remove the path infomation. You've got all the building blocks. Now you just need to put them together the right way.

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