What's wrong with this site.DesignMode code

  • Thread starter Thread starter Academia
  • Start date Start date
A

Academia

Can you see what is wrong with the code below.

Or suggest a better way.

Sometimes when I know I'm in the Designer I hit the bottom MessageBox

I think it might be because Site has not been set yet

Because I think it might happen while InitializeComponent is setting
properties and/or when OnLoad is running.



Thanks for any help







Public Shared Function IsDesign(ByVal c As Control) As Boolean

If c Is Nothing Then

MessageBox.Show("C is Nothing"...

End If

Dim nextControl As Control = c

Dim site As ISite

While nextControl IsNot Nothing

site = nextControl.Site

If site IsNot Nothing Then

If site.DesignMode Then Return True

End If

nextControl = nextControl.Parent

End While

MessageBox.Show(" Return False " ...

Return False

End Function
 
Where are you running this? It won't work in the constructor (and there's no
workaround for that).

/claes
 
In a constructor but not the first one executed.
The main window is already open and I'm opening another form when I do the
check.

Seems like Site for the main window would be set since the window is
completely opened before the other form is instantiated.

Is it the problem that Parent property is not set yet?

Or maybe the Main form is not the Parent of the other form even though it
opened it?

Can you suggest an alternative.

Maybe get a reference to the main form to the other form so it can check
that?


Thanks
 
The main form is not the parent of the new form. If you use the version of
ShowDialog/Show that take a IWin32Window argument and pass the Mainform to
it, it will be the owner though. In that case you can use the Owner property
in the other form to get a reference to the main form. Still, that property
won't be available in the constructor. You would need to pass a reference to
the constructor if you want to get access to the main form in there.

Sounds unlikely that your main form will be in design mode in this situation
though...
Can you describe what you're trying to accomplish?

/claes
 
Thanks for the interest.

Is Owner and Parent the same thing?


Claes Bergefall said:
The main form is not the parent of the new form. If you use the version of
ShowDialog/Show that take a IWin32Window argument and pass the Mainform to
it, it will be the owner though. In that case you can use the Owner
property in the other form to get a reference to the main form. Still,
that property won't be available in the constructor.

The Site and Owner property gets set after the constructor is run?
Is there documentation saying which properties are or are not set before the
constructor is run?
You would need to pass a reference to the constructor if you want to get
access to the main form in there.

Sounds unlikely that your main form will be in design mode in this
situation though...

As I think about it now - I don't think this relates. The problem relates to
the Designer displaying the form and I believe it will use a parameterless
constructor. Also, I have no control of how it shows it.
So I have to be sure not to check Site uintil I know it has been set.

Knowing which events the Designer might raise would also help. (For a form
and also for a usercontrol on a form) I may be checking when I do not have
to. Is that documented?


Can you describe what you're trying to accomplish?



One example is that I save a forms position in the registry and restore the
position next time it is open.

I don't want to do these things in DesignMode.

There are other similar things.


Thanks again
 
Academia said:
Thanks for the interest.

Is Owner and Parent the same thing?

No, not in this case


The Site and Owner property gets set after the constructor is run?
Is there documentation saying which properties are or are not set before
the constructor is run?

Basically nothing is set in the constructor so you should avoid doing much
in there.

As I think about it now - I don't think this relates. The problem relates
to the Designer displaying the form and I believe it will use a
parameterless constructor. Also, I have no control of how it shows it.
So I have to be sure not to check Site uintil I know it has been set.

Correct, you won't be able to control the constructor that the designer
uses.

Knowing which events the Designer might raise would also help. (For a form
and also for a usercontrol on a form) I may be checking when I do not have
to. Is that documented?

Not that I know of.

One example is that I save a forms position in the registry and restore
the position next time it is open.

I don't want to do these things in DesignMode.

In this particular situation you should be able to move your code to the
Load and Closed events. In there you can check the design mode.

/claes
 
Back
Top