473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1763
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 properties yet we are not changing anything in this. We...
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 developing an MDI application. In the application...
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 the ComboBox class and in the constructor I add...
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.. I'm not really sure what happened to them, but this...
11
by: John M. Gabriele | last post by:
Consider the following: #!/usr/bin/python #----------------------------------------------------------------- class Grand_parent( object ): def speak( self ): print 'Grand_parent.speak()'...
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 loaded in the designer when no control is on it, but...
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 SpreadTemplate, which is an enum with (at the moment) two...
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 there seems to be a lack of Tutorials and books on...
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 use it as a basis for some other forms. I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.