User control always crashes Visual Studio
Asked Answered
E

12

15

I'm trying to open a user control in one of our projects. It was created, I believe, in VS 2003, and the project has been converted to VS2008. I can view the code fine, but when I try to load the designer view, VS stops responding and I have to close it with the task manager. I have tried leaving it running for several minutes, but it does not do anything. I ran "devenv /log" but didn't see anything unusual in the log. I can't find a specific error message anywhere. Any idea what the problem might be? Is there a lightweight editing mode I might be able to use or something?

The reason I need to have a look at the visual representation of this control is to decide where to insert some new components.

I've tried googling it and searching SO, but either I don't know what to search or there is nothing out there about this. Any help is appreciated.

(The strangest thing is that the user control seems to load fine in another project which references, but VS crashes as soon as I even so much as click on it in that project.)

EDIT

The user control is a wrapper of a 3rd party HTML editor...so not exactly something which accesses a database.

I just tried putting the control on a new project and it crashed as soon as I dragged it onto the form.

Eroto answered 18/4, 2010 at 20:44 Comment(1)
@Nick: Can you edit your question and tell us more about your user control, what it does? And that does it reference to any external sources, for instance, database, log files, etc.?Ludwigshafen
B
18

I solved my crashing designer using techniques described here:

Good Way to Debug Visual Studio Designer Errors

I've been able to debug some control designer issues by running a second instance of VS, then from your first VS instance do a "Debug -> Attach to Process" and pick "devenv".

AND

It seems that the Load() method of UserControls somehow gets parsed by the designer. Putting initialisations and other suspicious code in a if - loop - checking for IsDesignMode keeps the designer from reading code that will crash it ....

--update: looking back now, all most things solved when I overthought / refactored the application design.

Bituminize answered 11/7, 2011 at 11:24 Comment(6)
Lifesaver!!!!! I have been pulling my hair out with this issue!!!! Thanks @wornd :)Aleuromancy
Second guy recommending this "attach to process"-thing. BUT my app is crashing when adding the userControl. So I cant debug because "attach to process" is also starting the dubbing itself. In this case nothing will happen where I havnt placed the userControl....Foliolate
C4ud3x cannot follow what you mean by "itself" ;) you run a 2nd instance of VS and then attach to process dev.env, then insert you usercontrol in 1st instance of VS -> then both VS die ?Bituminize
Checking for DesignMode inside OnLoad() solved my issue, thanks.Osteoporosis
designmode ...? What did you do then...? Did you change something?Maus
@Maus - designmode is when you edit the control in the IDE - avoid doing things in the Load-Method of the Control - fe. if you read some datasource, or do any other stuff that is not available when designing - wrap that stuff whith an if !Designmode() so it woun't get executed ....Bituminize
M
14

Your user control may have a property written like this:

private int myPropName; // note the lowercase m
public int MyPropName { get { return MyPropName; } } // the property is returned
                                                     // instead of the variable

Note that the value returned by get is the property itself. This crashes VStudio 2008 when trying to see the properties of the control, which also happens when adding the control (by drag'n drop or else) to a Form.

As pointed out by NickAldwin, this does not generate compiler warning or error.

Marsupial answered 26/8, 2010 at 19:49 Comment(6)
But this should also cause compiler errors/warnings, which I am not getting.Eroto
Bingo. I had this and it actually threw a StackOverflow but obviously only when the getter was called. Fixing this fixed my designer crash. It's a shame I didn't find this before uninstall .. reinstall for a day or 2. DERP.Anthropomorphism
I am embarrassed to admit how long I was debugging the VS crash until I stumbled on this answer. Thank you!Oyler
Just did the same dumb mistake (and I never do this I always return the member variable and not the property - I don't even understand how/why my brain typed it out like this). After 20 minutes of continual Visual Studio crashes, I double-checked based on your post. You are awesome!Wilma
Hours wasted looking in to the source of this crash... thanks !Alverson
VS2017 crashes when drawing a user control that overrides the Font property. This answer pointed me towards the properties. TY.Galimatias
M
3

Hangs like these are usually associated with networking. A database perhaps. You have to watch out for code that runs inside the UserControl at design time. Not just the constructor, also the Load event, timers, OnHandleCreated, OnResize, etcetera. The way to avoid running that code is by testing the DesignMode property, don't do anything dangerous when it is true.

Your ultimate fallback is to debug the code at design time with another instance of Visual Studio.

Mukerji answered 18/4, 2010 at 21:38 Comment(2)
+1 for debugging with another instance of VS, that works for me.Induct
I tried debugging with another instance of VS, but it doesn't seem to be reaching my code at all! If I break all, it's either in a "wait" or says no symbols are loaded for any call stack frame...Eroto
L
3

To follow up on womb's post, I found the following code to help out and block VS from crashing and reading the onLoad function.

I cannot reply and did not want to add a new comment. You can use the following in the Load method and stop VS from reading it and crashing. This was taken from this article

bool isDesignMode = (System.Diagnostics.Process.GetCurrentProcess().ProcessName.IndexOf("devenv") != -1);
            if (isDesignMode)
                return;
Lucent answered 13/10, 2012 at 13:53 Comment(1)
This is a life saver. I've been looking this isse for couple of days without results :(Radionuclide
P
1

I had the same problem. In my case, the problem was (I think) that I was initializing a member array in the declaration instead of in the constructor. The error was that the the VisualStudio project for my application immediately crashed when I tried to use the custom control from my lib that had the "bad" declaration.

It was frustrating because my custom component lib compiled with no errors.

Brooke

code that crashed:

class MyCustomObject: UserControl
{
  private List<int> myList = new <int>List();
   public MyCustomObject(){
      InitializeComponent();
     }

// other code here
} 

solution

class MyCustomObject: UserControl
{
   private List<int> myList;
   public MyCustomObject(){
        InitializeComponent();
        myList= = new <int>List();
     }

// other code here
} 
Paleolith answered 20/6, 2011 at 14:56 Comment(0)
E
1

To me helps changing Target Framework from ".Net Framework 4 Client Profile" to ".Net Framework 4" in project settings.

Edo answered 7/12, 2012 at 0:37 Comment(0)
T
0

Try to comment out parts or even the complete constructor of the control and see if it shows up. Maybe it is another initialization that fails, too.

Tenet answered 18/4, 2010 at 20:51 Comment(1)
Commenting out everything in the constructor didn't prevent it from crashing. Thanks for the suggestion, though!Eroto
L
0

After all that you have done, I would also try to load the user control in a brand new project, and see if it loads correctly; and if it does, then you can "decide where to insert some new components"... just thinking out loud!

Ludwigshafen answered 19/4, 2010 at 5:51 Comment(0)
P
0

I've had some similar problems when creating my own user controls in Visual Studio, but have been able to get the user controls to display properly by following these steps:

  1. Build or rebuild the project / solution
  2. Close Visual Studio and reopen it

These are pretty basic things that I bet you've already tried. Just throwing it out there.

Potence answered 21/4, 2010 at 23:29 Comment(0)
C
0

Recently I had annoying error after creating user control and attached it to form via designer, visual studio 2010 would always crash whenever design mode of same form where user control were attached.

After creating so many times and lot of experimentation i solved my problem by:

1 - Adding new class.cs file to project 2 - Moving all extra-supporting classes that I had added to user control itself 3 - I think this is more important: moving all custom delegates that wires events on User_Control to newly added file. 4 - Deleted User_Control.Load event.

I hope this may help someone.

Cheers

Choate answered 7/11, 2012 at 7:46 Comment(0)
G
0

I have come across this problem as well. I Have one project(ProjA) that contains user controls and another project(ProjB) that references those user controls. I found that every time I attempt to add a particular user control the vb.net Environment shuts down and reloads my solution. However, if I comment out the code that's contained within my controls LOAD event

 Private Sub SampleDisplayBox_Load(sender As Object, e As EventArgs) Handles Me.Load
        'AddHandler Sample.Sample_Reloaded, AddressOf reload
        'AddHandler Sample.Sample_Cleared, AddressOf SampleCleared
        'AddHandler My.Settings.SettingsSaving, AddressOf UpdateColors
        'UpdateColors()
        'reload()

    End Sub

and then rebuild my solution, I run into no problems when adding this control to a form. It's very strange that this problem occurs in only one control when I have 7 other controls in the same project that all function in the same manner with no errors.

Personally, I think VB.NET hates me.

Gingivitis answered 20/6, 2013 at 1:2 Comment(3)
Never mind. Now it's failing with the code commented out as well. Where did I go wrong?Gingivitis
Nailed it! On mine anyways.Gingivitis
My user control referenced a certain class as a property. Lets call it "Person". So my class (Person) might have a property (Relative AS New Person) <<< there the problem is. i fixed my error by replacing Relative AS New Person - with - Relative AS Person = NOTHING. This way the "Relative" Property is only used when it's needed.Gingivitis
A
0

I had problems with the IDE crashing/restaring VS.

This was specific to my using a Customer Designer (inherited from ParentControlDesigner) that I used to apply EnableDesignMode (and some smart tag work).

My problem VS crashing whilst adding my user control to a form (It's a big program and I have lots of Custom and User Controls) occured only when the EnableDesign was used - commen ting it out ceased the problem. This turned out to be a simple property in the User control setting a property of an underlying Custom Control (refrerring to itself as a reference for the child Custom Control so not a recursive issue). I don't know why this made a difference but it did or at least it's now working when I removed it.

The other problem I had, when removing the User control (unloading) from a form within the IDE, again was only due to the Designer inclusion. This was resolved by adding a specific dispose in the user user control dispose event that disposed the item(s) mentioned in the EnableDesign.

Debugging via a second VS instance didn't help as much as it could've (it just said Stackoverflow before going wrong and not related to the next logical line) but it did offer a clue of the general area of concern. This issue cost me days hence my report here in case it helps someone.

Needless to say my program continued to always work correctly when actually running and so it was purely a designer/VS IDE issue.

Allwein answered 8/11, 2019 at 11:29 Comment(1)
where did EnableDesign properties come from? I found nothing about that here....Maus

© 2022 - 2024 — McMap. All rights reserved.