What are the rules for Me.DesignMode

A

Academia

In a UserControl Designer.vb file I have:



Protected Overrides Sub Dispose(ByVal disposing As Boolean)

If Not Me.DesignMode Then

MessageBox.Show("Got here " & Me.DesignMode.ToString)

When I Show Designer for a Form that contains a UserControl that contains
this UserControl and then I click the X in the Designer so the Designer
closes the Form, this message box displays:

Got Here False

How come?



What are the rules for Me.DesignMode

Should it be True when the UserControl is exiting?



Really what I need is a UserControl equivalent to the Form Closing event.

Is there anything close?
 
P

Phill W.

Academia said:
If Not Me.DesignMode Then
MessageBox.Show("Got here " & Me.DesignMode.ToString)

When I Show Designer for a Form that contains a UserControl that contains
this UserControl and then I click the X in the Designer so the Designer
closes the Form, this message box displays:

Got Here False

How come?

Because the UserControl /isn't/ being hosted by a Designer - it's on a
proper Form - so it doesn't think it's in "DesignMode".

Two ways around this:

1) Check the DesignMode property on the Parent Control (tricky because,
IIRC, it's Protected and you have to add /another/ property to expose
it), or

2) (Quick and dirty) Find out what program the Control is running in
and, if it happens to be "devenv" - the 'Studio IDE - then /assume/
you're in DesignMode (when you /debug/ the program, it'll be your exe
name that appears).

Dim sExePath as String _
= System.Reflection.Assembly.GetAssembly().Location
Dim bDesignMode as Boolean _
= ( sExePath.ToLower().IndexOf("devenv.exe") > -1 )

HTH,
Phill W.
 
A

Academia

GetAssembly requires an argument. Can I just use some general type like
Integer or maybe pass the class name?

One of the following?


Class ControlJunk

Inherits System.Windows.Forms.UserControl

....snip

GetAssembly(ControlJunk.GetType())

or

GetAssembly(ControlJunk)

or

GetAssembly(Integer.GetType())
or
GetAssembly(Integer)

Thanks, I never would have figured it out.
 
C

Claes Bergefall

Actually, doing #1 is pretty easy. The DesignMode property gets its value
from the Site property (which is public). Try this:

Private Function IsInDesignMode() As Boolean
' Returns true if this control or any of its ancestors is in design
mode
If Me.DesignMode Then
Return True
Else
Dim parent As Control = Me.Parent
While Not parent Is Nothing
Dim site As ISite = parent.Site
If Not site Is Nothing AndAlso site.DesignMode Then
Return True
End If
parent = parent.Parent
End While
Return False
End If
End Function

/claes
 
A

Academia

I wanted a library function so I changed in 3 places.
I read the help about the Site and got a feeling but not a real deep
understanding.
I'm guessing c.DesignMode is obtaining a property from c, which gets it from
Site. Correct?
While c.Site.DesignMode is going through c to the object Site which returns
the value. Is that correct?

Thanks a lot

Actually I was looking into Phil W.'s #1, but Help has very little on IIRC
and I hadn't searched the Internet yet. I don't think I ever would have
stumbled onto "Site".

Public Shared Function IsInDesignMode(ByVal c As UserControl) As Boolean

' Returns true if this control or any of its ancestors is in design mode

If c.Site.DesignMode Then

Return True

Else

Dim parent As Control = c.Parent
 
A

Academia

Revised
I wanted a library function so I changed in 3 places.
I read the help about the Site and got a feeling but not a real deep
understanding.
I'm guessing c.DesignMode is obtaining a property from c, which gets it
from Site. Correct?
While c.Site.DesignMode is going through c to the object Site which
returns the value. Is that correct?
Do you see anything wrong with what I did?
Thanks a lot, I don't think I ever would have stumbled onto "Site".

Public Shared Function IsInDesignMode(ByVal c As UserControl) As Boolean

' Returns true if this control or any of its ancestors is in design mode

If c.Site.DesignMode Then

Return True

Else

Dim parent As Control = c.Parent
IIRC means 'If It Really Counts'. Funny the stuff that gets in the way!
 
C

Claes Bergefall

Academia said:
Revised
Do you see anything wrong with what I did?

Well, you need to check that Site actually has a value before you access its
DesignMode property, otherwise you'll get a NullReferenceException.

Public Shared Function IsInDesignMode(ByVal ctrl As Control) As Boolean
While ctrl IsNot Nothing
Dim site As ISite = ctrl.Site
If site IsNot Nothing AndAlso site.DesignMode Then
Return True
End If
ctrl = ctrl.Parent
End While
Return False
End Function


/claes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top