delegate property in form


J

jonpb

Hi. I'm using VS2008.
I have extended a DataGridView with a context menu. I want to allow
users of the grid to be able to override the default functionality of
the context menu items. So, I have members of type EventHandler that are
set to the default implementations in the constructor. The
ContextMenu.Popup handler does the layout of the menu assigning the
member delegates to the menu items in the MenuItems.Add function.

Now, if I expose the EventHandlers as Properties, the program compiles
without error but the WinForms Designer blows up when you view a form
that contains this grid. It actually goes into an infinite loop sending
multiple error boxes saying something about a property not being marked
as Serializable. The only way out is the End Task on Visual Studio. But,
the EventHandler type is marked as Serializable.

If I exposes the EventHandler as a public member, everything works fine.

Is there something I'm missing, or is this a bug in VS2008

Thanks
 
Ad

Advertisements

B

Bob Powell [MVP]

Is there something I'm missing

yeah, don't do that..

The reflection system used by the designer will be looking for properties
and events seperately via the GetProperties and GetEvents reflection
methods.

Use properties for exposing the encapsulated data of your class and events
for exposing the interactions.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
J

jonpb

Bob said:
Use properties for exposing the encapsulated data of your class and
events for exposing the interactions.

Thanks for the reply.

It's actually not an event, it's a delegate. Here is the code:

DataGridViewEx.cs
public delegate void MenuInsertRowHandler(int RowIndex);
public MenuInsertRowHandler MenuInsertRow = null;

SomeDialogThatContainsADataGridViewEx.cs
void InsertPoint(int Index)
{
....
}
grid.MenuInsertRow = InsertPoint;


I suppose I'm splitting hairs here, but it really is not an event, in
that it is not a subscription of callbacks, it is a simple single
callback. That is as designed, I do not want multiple listeners, nor
does that makes sense in this case. Of course it can be implemented as
an event, it just does not seem to be right to me.

Btw, the code that breaks the designer is this:

public MenuInsertRowHandler MenuInsertRow {
get { return MenuInsertItem; }
set { MenuInsertRow = value; }
}

which is referring to a delegate, there is no event type in this
Property declaration.
 
Ad

Advertisements

J

Jack Jackson

Hi. I'm using VS2008.
I have extended a DataGridView with a context menu. I want to allow
users of the grid to be able to override the default functionality of
the context menu items. So, I have members of type EventHandler that are
set to the default implementations in the constructor. The
ContextMenu.Popup handler does the layout of the menu assigning the
member delegates to the menu items in the MenuItems.Add function.

Now, if I expose the EventHandlers as Properties, the program compiles
without error but the WinForms Designer blows up when you view a form
that contains this grid. It actually goes into an infinite loop sending
multiple error boxes saying something about a property not being marked
as Serializable. The only way out is the End Task on Visual Studio. But,
the EventHandler type is marked as Serializable.

If I exposes the EventHandler as a public member, everything works fine.

Is there something I'm missing, or is this a bug in VS2008

Thanks

Assuming you aren't trying to set this property using the Property
window, try marking it as non-serializable and hidden from the
Property window by using these two attributes (VB syntax):

<Browsable(False),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top