1
Vote

Dropdown bug

description

Hi,
 
Found where things where going wrong. In BuildDropDownMenu there where three bugs.
 
1) The tsmi ToolStropMenuItem's created from the DropDownMenuItems collection where never added to teh DropDownMenu so it stayed empty2) There was no safeguard against i.Tag being null (which happened in the original code).3) At this point things started to show up when clicked, but the delegate code would still crash. Looking at the code faulting in 2) it also was neccesary to assign item to the tsmi.Tag property instead of using item.Tag Which was/is most of the time 0 and cannot be cast into the BreadcrumbDropDownItem expected in the delegate code.I also removed the Tag property at the top of the file as VS2008 was complaining it was hiding a inhertited one (from Control).
 
The delegate code in BuildDropDown menu could also test for the Tag being an object of the BreadcrumbDropDownMenuItem class (added it already in the code below). The bbdi.Tag then contains the value passed in the BreadcrumbDropDownMenuItem constructor.
 
Final code:
 
    private void BuildDropDownMenu()        {            this.DropDownMenu.Items.Clear();            foreach (BreadcrumbDropDownItem item in this.DropDownMenuItems)            {                ToolStripMenuItem tsmi = new ToolStripMenuItem(item.Text, item.Image,                    delegate(object s, EventArgs e1)                    {                        ToolStripMenuItem i = s as ToolStripMenuItem;                        //veg: Added safeguard for Tag being null and of the wrong type                        if (i.Tag != null && i.Tag is BreadcrumbDropDownItem)                        {                            BreadcrumbDropDownItem bddi = i.Tag as BreadcrumbDropDownItem;                            if (bddi.NavigateDelegate != null)                            {                                bddi.NavigateDelegate.Invoke(bddi, EventArgs.Empty);                            }                        }                    }, item.Text);                //veg: Changed from item.Tag to item.                tsmi.Tag = item;                //veg: Add.                this.DropDownMenu.Items.Add(tsmi);            }        }

comments