Descendant form will not display in design mode

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

I have a winform base class in which I dynamically set a web reference Url:

myService.Url = <WebServer> + "MyService.asmx";

where <WebServer> is pulled from my App.config file.

There is no problem working with this base class and the solution compiles
and runs fine. However, I am unable to display any descendant forms in
design mode in VS.NET 2003. I get a message indicating that:

"An exception occurred while trying to create and instance of <form base
class>. The exception was "Invalid URI: The format of th URI could not be
determined."

Clearly, it cannot resolve the dynamic web reference in the base class. The
immediate work-around is to:

1. comment out the assignment and rebuild
2. do any descendant form design
3. uncomment the assignment and rebuild

Surely, there is a mode elegant mechanism to address this issue?

Thanks,
Chris.
 
if (!this.DesignMode)
{
myService.Url = <WebServer> + "MyService.asmx";
}

I've found that in many cases I've had to put this at the beginning of
forms methods so that they don't bother running in design mode:

if (this.DesignMode)
{
return;
}
 
Yes, 'this.DesignMode' was my first thought but, alas, does not work. That
would be too easy. ;-)

Thanks,
Chris.
 
Can you post more code surrounding your problem?

Every problem I've had with the designer refusing to display a child
form has been because some method is running and trying to do "run
time" stuff in a design environment, and this.DesignMode is the
solution.

However, figuring out exactly which method it is and what it's trying
to do can be a bear. To this day I have two forms that _sometimes_ fail
to display in design mode with a message that there is a null
reference. Do you think I can figure out which method is being run
(which shouldn't be)? Naaah....

It's likely that it's not failing on the line of code you think it is:
that it's failing somewhere else.
 
Bruce Wood said:
Can you post more code surrounding your problem?

There is nothing else in the base form constructor other that the
InitializeComponent() call and the instantiation of the myService class
prior to setting the URL.
Every problem I've had with the designer refusing to display a child
form has been because some method is running and trying to do "run
time" stuff in a design environment, and this.DesignMode is the
solution.

However, figuring out exactly which method it is and what it's trying
to do can be a bear. To this day I have two forms that _sometimes_ fail
to display in design mode with a message that there is a null
reference. Do you think I can figure out which method is being run
(which shouldn't be)? Naaah....

It's likely that it's not failing on the line of code you think it is:
that it's failing somewhere else.

Well, all I need to do to get the descendant to display is comment out the
one line. Another option is to move the assignment line to the descendant
constructor and no problem. I guess this is just one of those things you
live with.

Thanks again,
Chris.
 
Ah... that makes sense.

DesignMode isn't set until your form has a window handle. The
constructor is too soon for that, so it returns "false".

The solution is to do as little as possible in the constructor, and do
as much set-up as possible in the form's ..._Load() method, where you
_do_ have a window handle and you _can_ use DesignMode effectively.

Perhaps if you set up Load method for your form (virtual, of course, so
your child classes can override it) and do your initialization in there?
 
Bruce Wood said:
Ah... that makes sense.

DesignMode isn't set until your form has a window handle. The
constructor is too soon for that, so it returns "false".

Light bulb!

Thank you sir; all is well using the Load.

Chris.
 
Back
Top