Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Use OfType and ignore inherited classes

I have a MenuStrip with lots of items and am trying to have on event they all subscribe to so I am trying to do menuStrip1.Items.OfType<ToolStripMenuItem>(); and for each one I do this:

menuitem.Click += new EventHandler(MenuItem_Click);

The problem is there is a ToolStripSeperatorItem which inherits off ToolStripMenuItem and that appears is my list of items and errors because it does not have a Click event.

How can I not include these in my OfType method?

like image 393
Jon Avatar asked Jan 18 '10 16:01

Jon


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

menuStrip1.Items.OfType<ToolStripMenuItem>()
                .Where(i => i.GetType() == typeof(ToolStripMenuItem))
like image 119
LukeH Avatar answered Sep 28 '22 23:09

LukeH


menuStrip1.Items.OfType<ToolStripMenuItem>().Where(it => it.GetType() == typeof(ToolStripMenuItem));

It seems kind of redundant, but by doing both, you maintain the return type.

like image 25
Michael Bray Avatar answered Sep 28 '22 21:09

Michael Bray