I have a list view which contains various object types and I need to
create a context menu with menu items dependent on the object type. I
can detect the object type at MouseDown. I've written code to create
the context menu but on first click, it doesn't work. On the second
click on the same type of object, it shows the correct menu items.
After that, basically, it shows the menu items that correspond to the
previous object I clicked on.
Has anyone seen this before? I've posted a subset of the code below -
I know it's not particularly nice but, for now, I'm only interested in
fixing it, not rewriting it as I'm rewriting the whole thing from the
ground up with proper Unit Tests soon.
SSG
private void lstvwAD_MouseDown(object sender, MouseEventArgs e)
{
cmnuDomain.MenuItems.Clear();
cmnuDomain.MenuItems.AddRange(new MenuItem[] {
cmnuDomainLargeIcons,
cmnuDomainSmallIcons,
cmnuDomainDetails,
cmnuDomainList});
int nextMenuItemIndex = 4;
if (lstvwAD.SelectedItems.Count == 1)
{
switch
(((DirectoryEntry)lstvwAD.SelectedItems[0].Tag).SchemaClassName)
{
case "computer":
MenuItem cmnuDomainSeparator = new MenuItem("-");
this.cmnuDomain.MenuItems.Add(cmnuDomainSeparator) ;
cmnuDomainSeparator.Index = nextMenuItemIndex++;
this.cmnuDomain.MenuItems.Add(cmnuDomainViewEPOAvt ivity);
cmnuDomainViewEPOAvtivity.Index = nextMenuItemIndex++;
break;
case "user":
MenuItem cmnuDomainSeparator = new MenuItem("-");
this.cmnuDomain.MenuItems.Add(cmnuDomainSeparator) ;
cmnuDomainSeparator.Index = nextMenuItemIndex++;
this.cmnuDomain.MenuItems.Add(cmnuDomainProperties WRBR);
cmnuDomainPropertiesWRBR.Index = nextMenuItemIndex++;
this.cmnuDomain.MenuItems.Add(cmnuDomainMoveAndDis able);
cmnuDomainMoveAndDisable.Index = nextMenuItemIndex++;
break;
}
}
this.lstvwAD.ContextMenu = this.cmnuDomain;