2

I currently experience problems in VS12 and the built-in designer. I am using a custom control framework which basically extends basic user controls and DevExpress controls. For a while now i have problems saving a form in design mode when i try to change a property or move one of the controls. I tried to translate the error messages as good as possible (I am using a german version of VS).

Visual Studio Designer Error Message:
Error during code generation for the MyPropertyMap-property. Error: The property accessor for the MyPropertyMap for the settings object has caused an exception: The enumeration has changed. Enumeration could not be executed.

I tried to debug the designer exception using another instance of visual studio with CLR debugging enabled:

System-InvalidOperationException
The enumeration has changed. Enumeration could not be executed.
Source: CustomForm
StackTrace: MyNamespace.Controls.CustomForm.get_FormVariables()
TargetSite: System.Collections.Hashtable get_FormVariables()

It looks like the designer tries to access the property on the form allthough i tried to hide it from designer using the following code:

[NonSerialized]
private Hashtable formVariables = null;
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
public Hashtable FormVariables
{
    get
    {
...

I tried to add the NonSerialized field because i thought serialization of the property might cause the problem. The getter basically queries all controls on the panel of the form and adds them to a hashtable because I am doing some timer based stuff with them. But during design time it is not necessary for the designer to access this property. Any suggestions how i really can hide it from the designer and prevent this error?

If i missed to give any other important input please let me know.

EDIT:

I am still trying to find out what causes the problem. One thing i found out is that when i disable the Optimize Code Gerneration setting in visual studio designer settings the error occurs all the time. When i reenable it there error occures once on the first attempt to save the form - and does not on the second attempt - meaning that the form is saved. I am not sure right now if it does all the time - but at least for the couple of times i tried it did.

All the public properties working with enumerations or other collection objects are implemented to return null if in DesignMode now. The exception still occurs as the designer tries to access the getter functions.

Kind regards.

monotonouz
  • 21
  • 4

2 Answers2

0

I think you should set a check for DesingMode in the getter. The winforms designer, AFAIK accesses the properties no matter what attributes have been set.

CristisS
  • 1,103
  • 1
  • 12
  • 31
  • I forgot to add. The weird thing is that in my getter i am already trying to prevent the execution by doing get{ if (!this.DesignMode) {... outsite of the if block i just return the private member of the variable which is by default null – monotonouz Aug 20 '13 at 14:08
  • i will try to rebuild a gui project with the new libraries. ill try to debug the getter functions and the setting of the designmode. i dont get it why the designer still has access to the underlying code – monotonouz Aug 21 '13 at 12:17
0

The designer will by default try to execute code it sees in designer files. This can cause issues in custom controls sometimes - especially if they do an unexpected form of binding or enumerate over a collection at run time. You'll need to skip past the relevant code using the design time check

A good example from MSDN:

 void timer_Tick(object sender, EventArgs e)
 { 
   if( this.DesignMode )   
     return; 

    this.Refresh(); 
 }

Here the code will return prematurely if Designmode is active(Skipping the refresh method). This only happens when the designer is up and wont hurt a running application at all.

ChargerIIC
  • 1,620
  • 1
  • 33
  • 47
  • is it possible that the design time check doesnt work for some cases? i updated my controls - implemented a design time check everywhere it was viable. the weird thing is - sometimes the error does not even occur for a while and then again it does. is it possible there is some old code hanging in my winforms design files that continues to be executed? can i clear the already generated code and force vs to regenerate it? – monotonouz Aug 21 '13 at 09:04
  • More likely that whatever is breaking the design time check isn't occurring in all cases. I've had this issue with controls that rely on real-time data for example - if the data updates while the control is being drawn a collection enumeration exception occurs. – ChargerIIC Aug 21 '13 at 13:49
  • I tried another option to the design time check based on [this](http://stackoverflow.com/questions/1166226/detecting-design-mode-from-a-controls-constructor) thread. i used the method suggested by Vaclav Svara using the IDesignerHost calss to check for design mode. When starting up the project the message boxes state that im in design mode, but after clicking the save button to save the form these methods dont seem to be executed - meaning no message boxes. what does the designer code generation on a form save actually do? – monotonouz Aug 21 '13 at 15:18
  • It runs the code to see if any additional drawing needs to be. Bear in mind that the designer is really just a reflection of the code lying under it. When you change something in the designer, the designer just changes the underlying code. It'll execute on each redraw. – ChargerIIC Aug 21 '13 at 15:55