Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Identifying DesignTime Mode for Forms

Identifying DesignTime Mode for Forms

Scheduled Pinned Locked Moved C#
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Firoz
    wrote on last edited by
    #1

    Hi, Is there any way for differentiating whether a Forms Constructor is called at DesignTime (by the FormsDesigner) or at Runtime. I have some code in a Forms Constructor that should be executed only when the Application is Run, and not when the Form is opened by the Forms Designer. Thanks, Firoz

    L 1 Reply Last reply
    0
    • F Firoz

      Hi, Is there any way for differentiating whether a Forms Constructor is called at DesignTime (by the FormsDesigner) or at Runtime. I have some code in a Forms Constructor that should be executed only when the Application is Run, and not when the Form is opened by the Forms Designer. Thanks, Firoz

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      If possible, you can add another constructor and those values there. The desingner will (:~ ) allways use the default constuctor, else you can check the DesignTime property, but that can be a bit over the top for something small. Hope this helps :)

      F 1 Reply Last reply
      0
      • L leppie

        If possible, you can add another constructor and those values there. The desingner will (:~ ) allways use the default constuctor, else you can check the DesignTime property, but that can be a bit over the top for something small. Hope this helps :)

        F Offline
        F Offline
        Firoz
        wrote on last edited by
        #3

        Hi leppie, Thanks, leppie wrote: add another constructor That is a great Idea. It works. But, (in my project) this will require code modification in a lot of places. Actually, I have a base form and around 40 forms derived from this base form. It would be wonderful, if I could put some code in the Base Form constructor itself, instead of modifying the constructor of all derived forms. But I liked your Idea, and I will keep it as a last option. leppie wrote: check the DesignTime property I had tried this before, but this property always returns false. Can you put some more light into this (how to use the DesignTime property). Thanks, Firoz

        L 1 Reply Last reply
        0
        • F Firoz

          Hi leppie, Thanks, leppie wrote: add another constructor That is a great Idea. It works. But, (in my project) this will require code modification in a lot of places. Actually, I have a base form and around 40 forms derived from this base form. It would be wonderful, if I could put some code in the Base Form constructor itself, instead of modifying the constructor of all derived forms. But I liked your Idea, and I will keep it as a last option. leppie wrote: check the DesignTime property I had tried this before, but this property always returns false. Can you put some more light into this (how to use the DesignTime property). Thanks, Firoz

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          so this does not work?

          public MyClass()
          {
          if (DesignMode)
          {
          //add some extra stuff
          //Add a msgbox to popup maybe???
          }
          else
          {
          //do some different stuff, so u can see the difference, change color or something
          }
          //do normal stuff
          }

          Note you can never check for this yourself... Hope this helps :)

          J 1 Reply Last reply
          0
          • L leppie

            so this does not work?

            public MyClass()
            {
            if (DesignMode)
            {
            //add some extra stuff
            //Add a msgbox to popup maybe???
            }
            else
            {
            //do some different stuff, so u can see the difference, change color or something
            }
            //do normal stuff
            }

            Note you can never check for this yourself... Hope this helps :)

            J Offline
            J Offline
            James T Johnson
            wrote on last edited by
            #5

            leppie wrote: so this does not work? Nope, that doesn't work... Why? Think about it this way; the DesignMode mode property uses the ISite property to do its work. But at that point of your code no properties have been set so ISite is still null which is why DesignMode will always return false. Shortly after code execution has left the constructor for your object, then ISite will be set and DesignMode will return its proper value. James "And we are all men; apart from the females." - Colin Davies

            L 1 Reply Last reply
            0
            • J James T Johnson

              leppie wrote: so this does not work? Nope, that doesn't work... Why? Think about it this way; the DesignMode mode property uses the ISite property to do its work. But at that point of your code no properties have been set so ISite is still null which is why DesignMode will always return false. Shortly after code execution has left the constructor for your object, then ISite will be set and DesignMode will return its proper value. James "And we are all men; apart from the females." - Colin Davies

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              Thanx James So the only way to go is an overloaded constructor?

              J 1 Reply Last reply
              0
              • L leppie

                Thanx James So the only way to go is an overloaded constructor?

                J Offline
                J Offline
                James T Johnson
                wrote on last edited by
                #7

                leppie wrote: So the only way to go is an overloaded constructor? I don't really like that tactic because it forces the user's code to be modified from what the form designer spits out. If at all possible I would try to delay the creation/use of the stuff that can't be done in DesignMode until the Load event or some other time. Of course this isn't always possible so you are stuck with things like creating a different constructor. James "And we are all men; apart from the females." - Colin Davies

                P 1 Reply Last reply
                0
                • J James T Johnson

                  leppie wrote: So the only way to go is an overloaded constructor? I don't really like that tactic because it forces the user's code to be modified from what the form designer spits out. If at all possible I would try to delay the creation/use of the stuff that can't be done in DesignMode until the Load event or some other time. Of course this isn't always possible so you are stuck with things like creating a different constructor. James "And we are all men; apart from the females." - Colin Davies

                  P Offline
                  P Offline
                  Paul Riley
                  wrote on last edited by
                  #8

                  Maybe I'm missing the point here but why not do it in the Form_Load event? Paul

                  L 1 Reply Last reply
                  0
                  • P Paul Riley

                    Maybe I'm missing the point here but why not do it in the Form_Load event? Paul

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #9

                    I think thats what James is telling us. We should only attempt to get a correct value from DesignMode after the form has been Loaded. Something like this:

                    bool loaded = false;

                    protected override void OnLoad(EventArgs e){
                    loaded = true;
                    base.OnLoad(e);
                    }

                    Then we use:

                    public int SomeProperty {
                    get {
                    if (loaded & DesignMode) dosomethingelse();
                    return someint;
                    }
                    }

                    Correct me if I understand this wrong...:~

                    P 1 Reply Last reply
                    0
                    • L leppie

                      I think thats what James is telling us. We should only attempt to get a correct value from DesignMode after the form has been Loaded. Something like this:

                      bool loaded = false;

                      protected override void OnLoad(EventArgs e){
                      loaded = true;
                      base.OnLoad(e);
                      }

                      Then we use:

                      public int SomeProperty {
                      get {
                      if (loaded & DesignMode) dosomethingelse();
                      return someint;
                      }
                      }

                      Correct me if I understand this wrong...:~

                      P Offline
                      P Offline
                      Paul Riley
                      wrote on last edited by
                      #10

                      leppie wrote: I think thats what James is telling us. Ooops! Missed the change of name part-way down this thread. leppie wrote: Something like this: Why bother with all that? There shouldn't be any need for DesignMode or overloading OnLoad. Just handle the Load event. Paul

                      L 1 Reply Last reply
                      0
                      • P Paul Riley

                        leppie wrote: I think thats what James is telling us. Ooops! Missed the change of name part-way down this thread. leppie wrote: Something like this: Why bother with all that? There shouldn't be any need for DesignMode or overloading OnLoad. Just handle the Load event. Paul

                        L Offline
                        L Offline
                        leppie
                        wrote on last edited by
                        #11

                        Paul Riley wrote: Why bother with all that? There shouldn't be any need for DesignMode or overloading OnLoad. Just handle the Load event. I guess thats to do with your own programming style. To me, its easier to override the function calling the event. You mite rather want to utilize the event, but in that case u need to keep track of it.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups