.NET Framework - What's wrong with this site.DesignMode code
Asked By Academia
02-Mar-08 12:00 PM
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
IWin32Window
(1)
ShowDialog
(1)
Control
(1)
OnLoad
(1)
Site
(1)
Correct
(1)
ISite
(1)
Load
(1)
Claes Bergefall replied...
Where are you running this? It will not work in the constructor (and there is no
workaround for that).
/claes
Academia replied...
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
Claes Bergefall replied...
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
Academia replied...
Thanks for the interest.
Is Owner and Parent the same thing?
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?
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?
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 Bergefall replied...
No, not in this case
Basically nothing is set in the constructor so you should avoid doing much
in there.
Correct, you won't be able to control the constructor that the designer
uses.
Not that I know of.
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

Code vorzusehen (Monitor.Enter, SyncLock in VB) Dann könnte es aber immer noch passieren, dass Control.Invoke aufgerufen wird, kurz bevor das FormClosed-Event kommt. Da Control.Invoke synchron ist, die MessageLoop aber dann nicht mehr läuft, würde es mich nicht wundern wenn es trotzdem kracht oder der Hintergrundthread blockiert. Vielleicht wäre es aber besser, ganz auf Control.Invoke zu verzichten und aus dem Hintergrundthread nur Daten oder Delegate-Objekte bereit zu stellen wird. Sollten also alle Forms im gleichen Thread laufen, spielt es keine Rolle, ob Do Control.Invoke oder den SynchronizationContext verwendest, um vom Worker Thread zum GUI Thread zu wechseln. Im gelesen hatte. Thematisch wird es auch nicht behandelt. In diesem Zusammenhang kommt noch hinzu, dass Control.InvokeRequired auch dann False liefert, wenn das Fensterhandle bereits zerst?rt oder noch nicht erstellt wurde. Es wird also keine Exception geworfen. Das ?bliche Konstrukt sub Handler if control.invokerequired then control.invoke(addressof handler) else control.eigenschaft = wert end if end sub ist m.E. dann auch nicht mehr haltbar. Bist
to prepare a customcontrol consist of a label, one property LabelText- a text for label control and a window Form. When click on the label the window form should appear and wrapper. editForm.EditText = Text; / / show the dialog and capture the dialog result DialogResult result = editForm.ShowDialog(); / / if the user cancels or closes the edit, return from the method if (result ! = DialogResult property. Why do you need another one? It is simple enough to handle the Label control's Click event if you want to do something when the user clicks the mouse button while over the Label control. The Label class already has a TextChanged event, and this event will in fact be not you have an actual object instance? Also, are you trying to create a custom Control sub-class, or a UserControl? There is a difference between the two, and your post Form, but which you can place within an actual Form). Pete actually, a custom Label control is only an example. I work on custom control which is more complicate and the custom Label control is the way to see the possible solution. If you do not provide a realistic
dwt. . . . do you want to update it now". I have numerous webpages (files) in this site and before importing it, when I tried to save, it always indicated like 13 files page it is attached to and then attach it to all the pages in your site. This suggestion does not answer your question but as a solution, if it works, it has an element of pragmatism! Malcolm Did you use FTP to import your site? If so, this is the reason the links between the DWT and attached pages are broken. FTP does not update meta data so that the destination site (on your PC) follows the source site (where you are importing from), and DWT links to attached pages rely on the meta trying to rebuild the file on my hard drive. - - Templar I do not have the site on my hard drive anymore. . . . . . . . . . crashed. I am trying to reconstruct things on my new drive / computer. The Front Page idea sounds like my only option. . . . . . . . . . but I created the site in Expression Web. . . . . . I thought Front Page and Expression Web were not compatible and do microsofts part to not make importation a bit more efficient. - - Templar Create a new local site then connect to your hosting site and sync the two by publishing down from the
Weird problem of nested ShowDialog() behavior .NET Framework Hi, everyone. I have a weird problem of nested ShowDialog() behavior. I believe that the ShowDialog mothod shows a Form as a modal dialog, so you can not perform any action class TopForm : Form { private void button1_Click(object sender, EventArgs e) { SecondForm form = new SecondForm(); form.ShowDialog(this); } } public class SecondForm : Form { private void button1_Click(object sender, EventArgs e) { ThirdForm form = new ThirdForm(); form.ShowDialog(this); } } Do the following steps. 1.Click the button on TopForm to create the SecondForm when you come across something like this, please report it on Microsoft's Connect web site, if it has not been reported already. As it happens, someone else has in fact Form dialog, Form parent) { EventHandler handler = (sender, e) = > { dialog.Activate(); }; try { parent.Activated + = handler; dialog.ShowDialog(parent); } finally { parent.Activated - = handler; } } As an extension method, you can just change calls like this: using (MyDialog dialog = new MyDialog()) { dialog.ShowDialog(this); } to this: using (MyDialog dialog = new MyDialog()) { dialog.SafeShowDialog(this); } Of course, the non
the other). Note that you can change the behavior if desired by setting the static Control.CheckForIllegalCrossThreadCalls property to "true". Then the exception will occur with or without the debugger attached which are always marshaled by Windows to the correct thread (the one that owns the control). But a) this implicit marshaling can cause hard-to-debug deadlock problems, and b) makes of the time (i.e. that 100% of the behavior is delegated to the Win32 control via window messages). Even for Forms controls that are in fact based on Win32 controls enhanced behaviors. And for Forms controls that are written mostly from scratch (they all inherit Control, and so all wind up having a window handle at some point???.NET Forms does Note that one major issue is that there are a number of members of the Control class that when accessed will implicitly cause the window handle for that Control instance to be created (creation of the window handle is generally deferred, not done when you accidentally cause the window handle to be created on the wrong thread, then your control just is not going to work correctly, because that is the thread where the messages for the control will be sent, and you probably will not have a message pump running on that