简体   繁体   中英

What's wrong in my dynamical ToolStripMenu script?

Why duplicate code? I read on a rampage in the xml data! and installing a toolstripmenu! but for some reason every so often put into the xml amennyi item is in! Why? here is the code:

try
{            
    XmlDocument xml = new XmlDocument();
    xml.Load("allomasok.xml");
    XmlNodeList xnList = xml.SelectNodes("/radiok/allomas");
    mennyi =int.Parse(xnList.Count.ToString());
    foreach (XmlNode xn in xnList)
    {
        string radioNEV = xn["neve"].InnerText;
        string radioURL = xn["url"].InnerText;

        //int i = mennyi;
       ToolStripMenuItem[] items = new ToolStripMenuItem[mennyi];

       for (int i = 0; i < items.Length; i++)
        {
            items[i] = new ToolStripMenuItem();
            items[i].Name = "mentett" + i.ToString();
            items[i].Tag = radioURL;
            items[i].Text = radioNEV;
            items[i].Click += new EventHandler(MenuItemClickHandler);
        }
        sajátokToolStripMenuItem.DropDownItems.AddRange(items);
    }            
}
catch
{
    MessageBox.Show("Hiba", "NetRadioPlayer");
}
finally
{
    MessageBox.Show("Ennyi mentett állomás van: " + mennyi);
}

Thanks.

You have too many loops, I think. Try this:

try
{            
    XmlDocument xml = new XmlDocument();
    xml.Load("allomasok.xml");
    XmlNodeList xnList = xml.SelectNodes("/radiok/allomas");
    ToolStripMenuItem mi;
    int i;

    foreach (XmlNode xn in xnList)
    {
       string radioNEV = xn["neve"].InnerText;
       string radioURL = xn["url"].InnerText;

       mi = new ToolStripMenuItem();
       mi.Name = "mentett" + i++.ToString();
       mi.Tag = radioURL;
       mi.Text = radioNEV;
       mi.Click += new EventHandler(MenuItemClickHandler);

       sajátokToolStripMenuItem.DropDownItems.Add(mi);
    }
}
catch
{
   MessageBox.Show("Hiba", "NetRadioPlayer");
}
finally
{
    MessageBox.Show("Ennyi mentett állomás van: " + mennyi);
}

If that doesn't help I'm sorry, I don't understand the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM