combining menustrip opening lines

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
can these 2 lines be written in a single line?
VB.NET:
Expand Collapse Copy
cmsPic.Items(0).DropDownItems.Add(New ToolStripMenuItem With {.Text="ABC"})
AddHandler cmsPic.Items(0).DropDownItems(0).Click, AddressOf ABC_Click
 
First of all, this line:
VB.NET:
Expand Collapse Copy
cmsPic.Items(0).DropDownItems.Add(New ToolStripMenuItem With {.Text="ABC"})
can more succinctly be written like this:
VB.NET:
Expand Collapse Copy
cmsPic.Items(0).DropDownItems.Add("ABC")
As for your question about adding the event handler on the same line, because the Add method that I've suggested returns the new menu item, you could do this:
VB.NET:
Expand Collapse Copy
AddHandler cmsPic.Items(0).DropDownItems.Add("ABC").Click, AddressOf ABC_Click
There's no need to do that in this case though, because there's another overload of Add that makes it easier still:
VB.NET:
Expand Collapse Copy
cmsPic.Items(0).DropDownItems.Add("ABC", Nothing, AddressOf ABC_Click)
Of course, you'd know all this already if you ever read the documentation, which I've recommended several times and you still apparently staunchly refuse to do. It would have taken you less than a minute to find out all that just by reading the documentation for the ToolStripItemCollection.Add method, or even just paying attention to Intellisense as you typed.
 
I did the New ToolStripMenuItem with {.Text="ABC"} because sometime it would be {.Text="ABC", .Enable=False, .Tag="DEF"} . Yes i could do the "ABC", Nothing, AddressOf ABC_Click if its only "ABC", but i need the situation when there are more arguments.
 
Back
Top