PC Review


Reply
Thread Tools Rate Thread

delegate property in form

 
 
jonpb
Guest
Posts: n/a
 
      18th Nov 2008
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
 
Reply With Quote
 
 
 
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      29th Nov 2008
>>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.


"jonpb" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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


 
Reply With Quote
 
jonpb
Guest
Posts: n/a
 
      2nd Dec 2008
Bob Powell [MVP] wrote:
> 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.
 
Reply With Quote
 
Jack Jackson
Guest
Posts: n/a
 
      3rd Dec 2008
On Tue, 18 Nov 2008 13:42:41 -0800, jonpb <(E-Mail Removed)> wrote:

>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)>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delegate for a property? Terry Microsoft VB .NET 7 3rd Apr 2009 03:11 PM
Delegate Gurus: Can I create a delegate for a property (as opposed to a method)? Bill Davidson Microsoft C# .NET 6 3rd Aug 2005 04:19 PM
Re: Can I create a delegate for a property? Richard Grimes [MVP] Microsoft Dot NET Framework 0 14th Oct 2004 03:59 PM
Re: Can I create a delegate for a property? Miha Markic [MVP C#] Microsoft Dot NET Framework 0 21st Sep 2004 09:08 AM
Can I create a delegate for a property? =?Utf-8?B?SGFycnkgS2Vjaw==?= Microsoft Dot NET Framework 0 20th Sep 2004 02:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:06 AM.