Hi, ive been working on an application and im starting to struggle to get much further at the moment. What i'm trying to do is list within a menustrip the folder in a set location and the files within them at run time, then when a file is clicked it opens the file, i would also like to add a mouse hover event for each, for example:
File Menu
Folder A > File A
File B
Folder B > File C
File D
Etc
I've been able to get this far and the menu and menu items are created at run time. The problem is that im not sure how to add the event for each file (if it can be done?). I've seen an example on MSDN of how to it but that was only for one item. I've had a look on a few websites and read a couple of posts about using an array?
I'm not very sure how to go about achieving this, or what more to read about, any help would be much appreciated.
Thanks
Loading
paul danielPosted Jan 25, 2010, 7:21 AM
click here
LewisPosted Jan 21, 2010, 10:30 AM
Public Sub AddMenuAndItems()
Dim mnuFileMenu As New MainMenu()
Me.Menu = mnuFileMenu
Dim myMenuItemFile As New MenuItem("&File")
mnuFileMenu.MenuItems.Add(myMenuItemFile)
Dim dir As String
For Each dir In My.Computer.FileSystem.GetDirectories("C:\Program Files\", FileIO.SearchOption.SearchTopLevelOnly)
Dim myMenuItemExe As New MenuItem(System.IO.Path.GetFileName(dir))
myMenuItemFile.MenuItems.Add(myMenuItemExe)
Dim files As String
For Each files In My.Computer.FileSystem.GetFiles("C:\Program Files\" & myMenuItemExe.Text, FileIO.SearchOption.SearchTopLevelOnly, "*.exe")
Dim myMenuItemFolder As New MenuItem(System.IO.Path.GetFileNameWithoutExtension(files))
myMenuItemFolder.Tag = System.IO.Path.GetFullPath(files)
AddHandler myMenuItemFolder.Click, AddressOf Me.MenuOption_Click
myMenuItemExe.MenuItems.Add(myMenuItemFolder)
Next
Next
End Sub
As you can see from the code about i want to list the folders in a directory (selected by the user) and then list the .exe files within the folders. I then want to be able to click the .exe file name and start the application. This is the part i am unsure about
Thanks
Lewis
Nilanka DharmadasaPosted Jan 21, 2010, 8:33 AM
Sorry. I didn;t see that. Anyway there is no big difference. Here is the code in VB. You use the same method to all the items as in the c# code. You set the file path to the tag property of the item.
Dim file1 As New ToolStripMenuItem()
file1.Text = "File1"
file1.Tag = "C:\\test\\a.txt" 'Set File Path here
AddHandler file1.Click, AddressOf Me.MenuOption_Click 'You bind the same method to all the Menu Options
Dim file2 As New ToolStripMenuItem()
file2.Text = "File2"
file2.Tag = "C:\\test\\b.txt" 'Set File Path here
AddHandler file2.Click, AddressOf Me.MenuOption_Click
AbcToolStripMenuItem.DropDownItems.Add(file1)
AbcToolStripMenuItem.DropDownItems.Add(file2)
Then you write the method. Eventhough we use a same method for all the menu items, inside the method we can identify, which item was clicked. So we can get the filepath and open the file.
Private Sub MenuOption_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If (TypeOf sender Is ToolStripMenuItem) Then
Dim itemObject As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim filepath As String = itemObject.Tag.ToString()
'Now you have the file path of the menu item clicked. You can use it to open the file.
End If
End Sub
If you find my answer helpful, please tick 'Do you like this answer' checkbox.
LewisPosted Jan 21, 2010, 7:02 AM
Thank you for the code but how would i be able to do this in VB?
Many thanks
Lewis
Nilanka DharmadasaPosted Jan 20, 2010, 11:45 PM
It's easy.
You can use following code to bind the click event at run time. Focus on the lines in red. You may have already written other code.
ToolStripMenuItem temp = new ToolStripMenuItem();//Let's say you create a menuitem.
temp.Name = "";//set anything you like as the name.
temp.Text = "a"";//set anything you like as the text.
temp.Tag = file; //Set the file name(full path of the file) here.This is a must.
temp.Click += new System.EventHandler(this.ToolStripMenuItem_Click);//Now you bind this common method to all the menuitems.
You can use the same code to all the menuitems you create. You may give different names. But you bind same method to all the click events.
Now you should write the method you bind to click event.Eventhough we use a same method for all the menu items, inside the method we can identify, which item was clicked. So we can get the filepath and open the file.
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
ToolStripMenuItem obj = (ToolStripMenuItem)sender;//You identify which menuitem was clicked.
string filename = obj.Tag.ToString();
//Now you have the file path of the menu item clicked. You can use it to open the file.
}
catch
{
}
}
If you find my answer helpful, please tick 'Do you like this answer' checkbox.
LewisPosted Jan 20, 2010, 11:58 AM
yes i'm trying to add the events to the menu item, i have read some examples about how to that but that was for one menu item only, im tryin to add an event to each menu item created at run time, these menu items may change at each run time.
Thanks
Sam HobbsPosted Jan 20, 2010, 11:14 AM
It is not clear to me what you are asking. It seems to me that you are asking how to specify an event for a menu item; is that what you need?