472,333 Members | 1,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,333 software developers and data experts.

Designer: mysterious behavior with form constructers and static methods

Hi,

[C# / VS2003]

I've run into a weird problem with the form designer. I have three classes:
a messagebox class and two forms (Base and Derived which is derived from
Base):

(note: only relevant code below)

<code>

public class MyMsg
{
[DllImport("user32.dll", EntryPoint="MessageBoxW",
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
private static extern IntPtr MessageBox(UInt32 hWnd, string AText,
string ACaption, UInt32 AType);

public static void Show(string AText)
{
MessageBox(0, AText, "Caption", 0);
}
}

public class BaseForm : System.Windows.Forms.Form
{
public BaseForm()
{
InitializeComponent();

MyMsg.Show("Test");
}
}

public class DerivedForm : BaseForm
{
public DerivedForm()
{
InitializeComponent();
}
}

</code>

When I show DerivedForm in the designer my messagebox is shown. When I show
BaseForm in the designer, the messagebox is NOT shown. What gives?! Is this
a bug?

Are there any specifications of what and what isn't created/shown/whatever
designtime vs. runtime?

I know the above code doesn't really make sense, but its a simplification of
some similar code used for singleton patterns...
Regards,
Sebastian

Nov 15 '05 #1
4 1712
Hello Sebastian,

When you edit a form or any other root component in the designer, the class
of the form is not instantiated. Instead, its base class is instantiated.
Something is needed to serve as the root component, but to obtain the latest
code from the InitailizeComponents method, which may not be compiled (the
form may not even be compilable), instead of instantiating the class you are
working on, the base class is instantiated, and the IntializeComponents
method is interpreted using a CodeDom interpreter built into VS. So only the
code in there will be interpreted, and of course the interpreter is somewhat
limited. This is why the message box appears when working on the derived
class: the base class code is actually run and not interpreted.

Regards,
Frank Hileman
Prodige Software Corporation

check out VG.net: www.prodigesoftware.com/screen5.png
An animated vector graphics system integrated in VS.net
beta requests: vgdotnetbeta at prodigesoftware.com

"Sebastian Bargmann" <sb@removedbecauseofspam.dk> wrote in message
news:fq*****************@news.get2net.dk...
Hi,

[C# / VS2003]

I've run into a weird problem with the form designer. I have three classes: a messagebox class and two forms (Base and Derived which is derived from
Base):

(note: only relevant code below)

<code>

public class MyMsg
{
[DllImport("user32.dll", EntryPoint="MessageBoxW",
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
private static extern IntPtr MessageBox(UInt32 hWnd, string AText,
string ACaption, UInt32 AType);

public static void Show(string AText)
{
MessageBox(0, AText, "Caption", 0);
}
}

public class BaseForm : System.Windows.Forms.Form
{
public BaseForm()
{
InitializeComponent();

MyMsg.Show("Test");
}
}

public class DerivedForm : BaseForm
{
public DerivedForm()
{
InitializeComponent();
}
}

</code>

When I show DerivedForm in the designer my messagebox is shown. When I show BaseForm in the designer, the messagebox is NOT shown. What gives?! Is this a bug?

Are there any specifications of what and what isn't created/shown/whatever
designtime vs. runtime?

I know the above code doesn't really make sense, but its a simplification of some similar code used for singleton patterns...
Regards,
Sebastian

Nov 15 '05 #2
Hi Frank - thanks for your answer,

So this behaviour is actually by design. I see...

One way to prevent the dialog (MyMsg.Show()) to be shown in the designer, is
to move it to the base-form's OnLoad method and then check the DesignMode
property (since DesignMode isn't valid in the ctor).

Are there other alternatives?
Regards,
Sebastian Bargmann
"Frank Hileman" <fr******@no.spamming.prodigesoftware.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hello Sebastian,

When you edit a form or any other root component in the designer, the class of the form is not instantiated. Instead, its base class is instantiated.
Something is needed to serve as the root component, but to obtain the latest code from the InitailizeComponents method, which may not be compiled (the
form may not even be compilable), instead of instantiating the class you are working on, the base class is instantiated, and the IntializeComponents
method is interpreted using a CodeDom interpreter built into VS. So only the code in there will be interpreted, and of course the interpreter is somewhat limited. This is why the message box appears when working on the derived
class: the base class code is actually run and not interpreted.

Nov 15 '05 #3
Sebastian, without knowing more about your specific code I don't think I can
give an intelligent answer. - Frank

"Sebastian Bargmann" <sb@removedbecauseofspam.dk> wrote in message
news:jh****************@news.get2net.dk...
Hi Frank - thanks for your answer,

So this behaviour is actually by design. I see...

One way to prevent the dialog (MyMsg.Show()) to be shown in the designer, is to move it to the base-form's OnLoad method and then check the DesignMode
property (since DesignMode isn't valid in the ctor).

Are there other alternatives?
Regards,
Sebastian Bargmann

Nov 15 '05 #4
Hi Frank,

There really isn't any more to it than you have already seen. My real
problem is probably that I need to (well, I'd like to) know how code
behaves when run in the designer vs. "normal" run.

E.g. Are there any methods or properties on UserControl (or its parents)
that behaves differently when running in the designer (DesignMode is on such
property).

I have worked a lot with Delphi and VC++ and I'm used to be able to check
the source of VCL/MFC if there's something I don't understand. Apparanty I'm
not so lucky with dotNET...

Best regards,
Sebastian
"Frank Hileman" <fr******@no.spamming.prodigesoftware.com> wrote in message
news:eu**************@tk2msftngp13.phx.gbl...
Sebastian, without knowing more about your specific code I don't think I can give an intelligent answer. - Frank

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: | last post by:
Hi, We have a form designed in the VS designer in C# and everytime we build on different machiens its chainging the .Location and .Size...
13
by: Kenneth Baltrinic | last post by:
This isn't a problem from the standpoint that its easy to work around. However, I am very currious as to the correctness of this behavior. I am...
1
by: MLongmire | last post by:
I'm trying to create a ComboBox class that will present the same printout options to users no matter where I need to use the combo box. I extend...
8
by: Aaron Smith | last post by:
I have a problem... Have a form, made some changes.. now all the controls are gone. I didn't make any changes to the designer generated region.....
11
by: John M. Gabriele | last post by:
Consider the following: #!/usr/bin/python #----------------------------------------------------------------- class Grand_parent( object ): ...
4
by: ThunderMusic | last post by:
Hi, I have a custom form that works fine when I debug it or run it in release mode but cannot be loaded in the designer... Actually, it can be...
3
by: segecko | last post by:
Hi I have a created a custom usercontrol which inherites an Excel like usercontrol. In this usercontrol I have a custom property called...
6
by: MACKTEK | last post by:
Intro: I have recently started programming in C++/CLI. I have done a lot of research on the net trying to locate an answer to this problem, but...
4
by: Phil | last post by:
VS 2008 I have a windows form that was created in the normal way using the form designer. I do not wish to use this form directly though, I want to...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.