質問

I have dynamically added a few items to my Menu Strip and triggered and event for each item added to the strip (items are added from an XML file). What I'm trying to do next is to pass the text from each item to the event handler so I can use them in a thread. Is it possible to this? Can anyone help me with a solution please?

This is my code:

private void historyMenuItem_Click(object sender, EventArgs e){
    XmlDocument doc = new XmlDocument();  // create new xml document
    doc.Load("..\\history.xml");  // load the xml
    // create a new node list
    // and select nodes from BookItems/Book
    XmlNodeList nodeList = doc.SelectNodes("URLs/http"); 

    historyMenuItem.DropDownItems.Clear();
    foreach (XmlNode node in nodeList) {
        string page = node.Attributes["page"].Value;
        //http://msdn.microsoft.com/en-us/library/ms160990.aspx
        ToolStripMenuItem windowNewMenu = new ToolStripMenuItem(page, null, new EventHandler(MenuItemClickHandler));
        historyMenuItem.DropDownItems.Add(windowNewMenu);
    }
}

private void MenuItemClickHandler(object sender, EventArgs e){
    ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender;
    ## HERE IS WHERE I NEED HELP:
    UrlTextBox = (I need value of page here!);
    this.thread = new Thread(new ThreadStart(this.httpRequestMultiThread));
    this.thread.Start();
}
役に立ちましたか?

解決

I would suggest you to use Tag Property. It is object so you need to know to which type to cast it when you want to use it. Something like this

private void historyMenuItem_Click(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();  // create new xml document
        doc.Load("..\\history.xml");  // load the xml
        XmlNodeList nodeList = doc.SelectNodes("URLs/http");  // create a new node list
                                                              // and select nodes from BookItems/Book
        historyMenuItem.DropDownItems.Clear();
        foreach (XmlNode node in nodeList)   // for each node in the node list
        {
            string page = node.Attributes["page"].Value;

            ToolStripMenuItem windowNewMenu = new ToolStripMenuItem(page, null, new EventHandler(MenuItemClickHandler));
            windowMenuItem.Tag = page;
            historyMenuItem.DropDownItems.Add(windowNewMenu);
        }
    }
private void MenuItemClickHandler(object sender, EventArgs e)
{
    ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender;

    UrlTextBox.Text = (string)clickedItem.Tag;
    this.thread = new Thread(new ThreadStart(this.httpRequestMultiThread));
    this.thread.Start();
}

他のヒント

You can use Tag property of ToolStripMenuItem to store any additional data about the item (e.g. value of page):

windowNewMenu.Tag = node.Attributes["page"].Value;

Tag has type of object, thus you need to cast it during retrieving value:

ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender;
string page = clickedItem.Tag as string;

But in your current solution value of page is assigned to Text property of menu item (when you create item). So, you can retrieve value from that property:

string page = clickedItem.Text;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top