Designer: mysterious behavior with form constructers and static methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sebastian Bargmann

    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("user 32.dll", EntryPoint="Mes sageBoxW",
    CharSet=CharSet .Unicode, ExactSpelling=t rue,
    CallingConventi on=CallingConve ntion.StdCall)]
    private static extern IntPtr MessageBox(UInt 32 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()
    {
    InitializeCompo nent();

    MyMsg.Show("Tes t");
    }
    }

    public class DerivedForm : BaseForm
    {
    public DerivedForm()
    {
    InitializeCompo nent();
    }
    }

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



  • Frank Hileman

    #2
    Re: Designer: mysterious behavior with form constructers and static methods

    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 InitailizeCompo nents 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 IntializeCompon ents
    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@removedbeca useofspam.dk> wrote in message
    news:fqqvb.2735 $My5.1301@news. get2net.dk...[color=blue]
    > Hi,
    >
    > [C# / VS2003]
    >
    > I've run into a weird problem with the form designer. I have three[/color]
    classes:[color=blue]
    > a messagebox class and two forms (Base and Derived which is derived from
    > Base):
    >
    > (note: only relevant code below)
    >
    > <code>
    >
    > public class MyMsg
    > {
    > [DllImport("user 32.dll", EntryPoint="Mes sageBoxW",
    > CharSet=CharSet .Unicode, ExactSpelling=t rue,
    > CallingConventi on=CallingConve ntion.StdCall)]
    > private static extern IntPtr MessageBox(UInt 32 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()
    > {
    > InitializeCompo nent();
    >
    > MyMsg.Show("Tes t");
    > }
    > }
    >
    > public class DerivedForm : BaseForm
    > {
    > public DerivedForm()
    > {
    > InitializeCompo nent();
    > }
    > }
    >
    > </code>
    >
    > When I show DerivedForm in the designer my messagebox is shown. When I[/color]
    show[color=blue]
    > BaseForm in the designer, the messagebox is NOT shown. What gives?! Is[/color]
    this[color=blue]
    > 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[/color]
    of[color=blue]
    > some similar code used for singleton patterns...
    >
    >
    > Regards,
    > Sebastian
    >
    >
    >[/color]


    Comment

    • Sebastian Bargmann

      #3
      Re: Designer: mysterious behavior with form constructers and static methods

      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" <frankhil@no.sp amming.prodiges oftware.com> wrote in message
      news:%23JmXS1Js DHA.1760@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > Hello Sebastian,
      >
      > When you edit a form or any other root component in the designer, the[/color]
      class[color=blue]
      > 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[/color]
      latest[color=blue]
      > code from the InitailizeCompo nents method, which may not be compiled (the
      > form may not even be compilable), instead of instantiating the class you[/color]
      are[color=blue]
      > working on, the base class is instantiated, and the IntializeCompon ents
      > method is interpreted using a CodeDom interpreter built into VS. So only[/color]
      the[color=blue]
      > code in there will be interpreted, and of course the interpreter is[/color]
      somewhat[color=blue]
      > limited. This is why the message box appears when working on the derived
      > class: the base class code is actually run and not interpreted.[/color]


      Comment

      • Frank Hileman

        #4
        Re: Designer: mysterious behavior with form constructers and static methods

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

        "Sebastian Bargmann" <sb@removedbeca useofspam.dk> wrote in message
        news:jhjwb.2207 $DO6.414@news.g et2net.dk...[color=blue]
        > 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,[/color]
        is[color=blue]
        > 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[/color]


        Comment

        • Sebastian Bargmann

          #5
          Re: Designer: mysterious behavior with form constructers and static methods

          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" <frankhil@no.sp amming.prodiges oftware.com> wrote in message
          news:eubLowosDH A.3436@tk2msftn gp13.phx.gbl...[color=blue]
          > Sebastian, without knowing more about your specific code I don't think I[/color]
          can[color=blue]
          > give an intelligent answer. - Frank[/color]


          Comment

          Working...