Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / March 2008

Tip: Looking for answers? Try searching our database.

What's wrong with this     site.DesignMode   code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Academia - 02 Mar 2008 17:00 GMT
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
Claes Bergefall - 03 Mar 2008 11:05 GMT
Where are you running this? It won't work in the constructor (and there's no
workaround for that).

  /claes

> Can you see what is wrong with the code below.
>
[quoted text clipped - 40 lines]
>
> End Function
Academia - 03 Mar 2008 15:39 GMT
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

> Where are you running this? It won't work in the constructor (and there's
> no workaround for that).
[quoted text clipped - 45 lines]
>>
>> End Function
Claes Bergefall - 04 Mar 2008 09:33 GMT
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

> In a constructor but not the first one executed.
> The main window is already open and I'm opening  another form when I do
[quoted text clipped - 64 lines]
>>>
>>> End Function
Academia - 04 Mar 2008 16:10 GMT
Thanks for the interest.

Is Owner and Parent the same thing?

> 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

>    /claes
>
[quoted text clipped - 66 lines]
>>>>
>>>> End Function
Claes Bergefall - 05 Mar 2008 10:24 GMT
> Thanks for the interest.
>
> Is Owner and Parent the same thing?

No, not in this case

>> 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
[quoted text clipped - 5 lines]
> 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.

>>You would need to pass a reference to the constructor if you want to get
>>access to the main form in there.
[quoted text clipped - 6 lines]
> 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.

>> 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.

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.