|
Greetings,
I have a form I built in VB, using the form designer. Everything worked just fine.
Then I added some code to the load method. The code in question calls a method ofshared class I have defined. That class uses reflection( System.Runtime.Remoting.ObjectHandle, Activator.CreateInstanceFrom, etc.) to get a reference to an object determined at run time.
When I do this, I am unable to use the form designer. If I comment out that one call, the form shows just fine.
Is there a way to tell the editor/form designer to ignor that line of code when showing the form in design mode?
Before you answer, I do want to keep the call in my code as I need it to do some things that can't be done any other way. And I need it to run as part of the forms instantiation (either via the new or Load events.)
Thanks,
FletcherJ
| | FletcherJ Tuesday, September 15, 2009 12:28 AM | I just remembered something: The DesignMode property does not have the correct value during construction. You cannot use it in constructors because, well, it will be False even though it eventually becomes True. The DesignMode property is provided by the control's site. Only after the site is set can you accurately call for DesignMode. You must take the commented code out of the constructor. Where to put this code? I don't really know because I don't know what it does or its purpose. You'll have to figure that one yourself. But here's a tip: The Site property of a form can be overriden (at least in C#). You could override it, and then run this commented code of yours in the set accessor depending on the (now) accurate value of DesignMode. MCP- Marked As Answer byFletcherJ Monday, September 21, 2009 7:03 PM
-
| | webJose Monday, September 21, 2009 6:57 PM | Visual Studio will not run code that is in the Load event of a form. You must be putting this somewhere else, or the error might be related to, but not directly caused by, the code modification. I don't do VB, so I'd rather not delve too much into the specifics of the code, but I do recommend that you double and triple check where you are putting the code. Sounds to me that you are adding it to the designer file instead of the regular code file. MCP- Proposed As Answer byAland LiMSFT, ModeratorWednesday, September 16, 2009 6:53 AM
-
| | webJose Tuesday, September 15, 2009 1:42 AM | There's a DesignMode property on the form, which you can use to decide what to do:
If !DesignMode Then .... End If
- Proposed As Answer byAland LiMSFT, ModeratorWednesday, September 16, 2009 6:53 AM
- Marked As Answer byFletcherJ Monday, September 21, 2009 4:40 PM
- Unmarked As Answer byFletcherJ Monday, September 21, 2009 5:37 PM
-
| | Wollinet Tuesday, September 15, 2009 6:45 AM | Wollinet,
Thanks, that was exactly what I wanted!
FletcherJ | | FletcherJ Monday, September 21, 2009 4:40 PM | Wollinet, Ok, I am confused. I used this and it worked in one place. I have a form that is used as a parent class to some other forms. In the Sub New, I have the following code:
Sub New()
' Set any values that need to be defined during creation
' This call is required by the Windows Form Designer.
InitializeComponent()
If Not Me.DesignMode Then
'If Not String.IsNullOrEmpty(myApp.dataPath) Then
' This should only be the case at design time, not run time.
'Me.strDefExportPath = myApp.dataPath & "ex\"
'End If
End If ' Not Me.DesignMode Then
End Sub
If I uncomment theif/endif statements above, the form class still shows in design mode. But if I try to open any forms that inherit this form, they crash with a stack trace pointing to this code. And if I comment it as above, then the child forms appear correctly in design mode. But the application doesn't work right..... Why does this workso the parent class shows in design mode, but the child classes ignore it and don't show? And how do I get the child classes to display? Thanks, FletcherJ | | FletcherJ Monday, September 21, 2009 5:36 PM | If I am correct, the Sub New() is the constructor. Constructors run both in design-time and run-time, but only the ones from base classes. Therefore, you see the problem when you try to design a derived form, but you don't see the problem when you design the base form. When you design the base form, the constructor of System.Windows.Forms.Form is run. When you design MyForm (that inherits from your base form), then the constructor that you are showing above is run. MCP | | webJose Monday, September 21, 2009 5:53 PM | Now, why does it fail? Good question. What is "myApp"? Is it available at design-time? What is the exception that you get when you try to design a form that derives from your base form? Also, something doesn't match: You say that derived forms crash... in design mode? Because the first if should only allow the inner code to run in run-time, not design-time. Did I misunderstand? MCP | | webJose Monday, September 21, 2009 5:55 PM | WebJose,
I am not quite sure I fully follow your logic. I know that the Sub New of the parent class and sub New of the child class both run. But your answer doesn't help me with how I can bring up the child forms in design view without having to comment out the code in the parent class first.
My guess is that, when I open the child form, the DesignMode directive only works for code in that form/class and is ignored in any methods in the parent classes even though they too are being opened.
So I am back to the base problem - how do I get the designer to ignor the lines in the parent class?
Thanks,
FletcherJ
| | FletcherJ Monday, September 21, 2009 6:16 PM | The commented code shouldn't run in design mode because it is enclosed in an IF statement that should prevent it from running. So the issue is not that the code is running. Most likely the code is just plain being rejected. What is myApp? What does the designer error say? If you type any other code inside the IF (where the commented code is), can you get the derived forms to show ok in the designer? Try something simple, like a Debug.Print(). MCP | | webJose Monday, September 21, 2009 6:27 PM | I just remembered something: The DesignMode property does not have the correct value during construction. You cannot use it in constructors because, well, it will be False even though it eventually becomes True. The DesignMode property is provided by the control's site. Only after the site is set can you accurately call for DesignMode. You must take the commented code out of the constructor. Where to put this code? I don't really know because I don't know what it does or its purpose. You'll have to figure that one yourself. But here's a tip: The Site property of a form can be overriden (at least in C#). You could override it, and then run this commented code of yours in the set accessor depending on the (now) accurate value of DesignMode. MCP- Marked As Answer byFletcherJ Monday, September 21, 2009 7:03 PM
-
| | webJose Monday, September 21, 2009 6:57 PM | MCP,
Ok, that worked. I moved the code to the LOAD method and the DesignMode seems to be defined by the time that fires.
Thanks,
Fletcher | | FletcherJ Monday, September 21, 2009 7:03 PM |
|