1

I'm trying to create a dynamic stripmenu, basically the program reads a XML file containing the structure for the menus, this I've done successfully.

But when i click on the menu "File" as example nothing happens, yes i know i have to create a function that will be called when i click the object, so i did that following the instructions of an website that explains how to do that in C#, however in VB.NET does not work, but in C# it does.

Here is the example that i saw in C#:

private void CreateMenuWithEventAndKey()
{
    MenuStrip strip = new MenuStrip();
    ToolStripMenuItem fileItem = new ToolStripMenuItem("&File");

    // Create our first item with an image and wired to a click event

    // Also sets Alt + 7 as the shortcut
    ToolStripMenuItem itemWithEventAndKey = new ToolStripMenuItem(
        "Delete Event", Image.FromFile("c:\\Delete.png"), deleteItem_Click, 
        (Keys)Shortcut.Alt7);
    fileItem.DropDownItems.Add(itemWithEventAndKey);
    strip.Items.Add(fileItem);
    this.Controls.Add(strip);
}

// Event that is called from menu item.
private void deleteItem_Click(object sender, EventArgs e)
{
    MessageBox.Show("Delete Event");
}

Here is what i've done in VB.NET.

 Private Sub BtnLogin_Click(sender As Object, e As EventArgs) Handles BtnLogin.Click
    Dim menu As New MenuStrip()

    Panel1.Visible = False
    Dim fileitem = New ToolStripMenuItem("&file")
    fileitem.DropDownItems.Add("New", Image.FromFile("C:\\add.png"), NewFile_click)
    menu.Items.Add(fileitem)
    Me.Controls.Add(menu)
End Sub

Private Sub NewFile_click(sender As Object, e As EventArgs)
    MessageBox.Show("New")
End Sub

It does not build because it says that the object "sender" and "e" is not being passed in the call of the NewFile_Click, I don't know what to do.

H H
  • 263,252
  • 30
  • 330
  • 514
fzuid
  • 27
  • 1
  • 5

1 Answers1

2

In VB.Net, use AddressOf:

fileitem.DropDownItems.Add("New", _
                           Image.FromFile("C:\\add.png"), _
                           AddressOf NewFile_click)
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Thank you, another question, do you know how i can call the function in this case? Let me explain, the menu titles will be in the XML file the name of the function too, so in the XML file i have the name of the menus and the name of the functions that will be executed when clicked. Could you help me Again? – fzuid Aug 25 '14 at 20:54
  • @FernandoZuidarxis It sounds like you should hit the Ask Question button again, but it's not very clear what you mean by "the name of the functions". Make sure you document the question with examples. – LarsTech Aug 26 '14 at 01:50
  • OK thank you anyway, i was thinking in something that is not very usefull, however thank you very much. – fzuid Aug 26 '14 at 05:14