tags:

views:

55

answers:

1

I have a hard time believing that there isn't an answer here but it certainly looks like it doesn't exist.

I am trying to iterate over everything on the form and set the visible and enabled properties based on the tag value. I find the reek of an absolutely duplicated routine between the routine iterating the controls and the routine iterating the menus--while both classes have visible, enabled and tag properties they appear to be separate items.

As far as I can tell the tree only converges at Component--but this lacks the visual properties.

+1  A: 

That's correct, there is no common ancestor. The ToolStripMenuItem class is derived from ToolStripItem, the base class for many derived classes that are parts of a MenuStrip or a ToolStrip. They are special because they are not derived from Control. They are window-less controls, they don't have a Handle property. Which is the key property of the Control base class.

This is an optimization, the Control derived classes are expensive. They need a native Windows window, a heavy operating system object with lots of overhead. Really evident when you put, say, 50 buttons on a form. You can see it paint.

Duplicating this logic is thus normal. Using the Tag property to control state isn't.

Hans Passant
The tag bit is checking whether the current user is supposed to be able to use the item or not.
Loren Pechtel
Isn't that what Enabled/Visible is for? If the user cannot use the item, then either disable it, which Windows will render in a fairly unambiguous way, or hide it altogether. In most end-user apps, the user doesn't want to see a whole bunch of things they can't do, and neither do their supervisors (because the supervisors will invariably end up fielding requests/complaints along the lines of "why don't I have permission to do this?")
KeithS
@Keith: he's using the Tag property to mean "Visible to privileged eyes only".
Hans Passant
Exactly--the tag identifies the security attached to the function. If they have authorization for that tag # it shows up, if they don't it doesn't show up. The visible/disabled is basically diagnostic only.
Loren Pechtel