开发者

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开发者_JS百科 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜