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. prevent method execution on design time using attributes

prevent method execution on design time using attributes

Scheduled Pinned Locked Moved C#
helpdesignquestion
12 Posts 5 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.
  • G Gilbert Consellado

    Is there a way to prevent a method from execution if it is in design time or the method can only be executed at runtime. I got this problem while creating a custom control because there is a method call in the constructor that will only work at runtime. now at design time while designing the form and use that control, then the form will generate the error. now i tried this at the constructor of the user control

    public ctrl_information()
    {
    InitializeComponent();
    if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)) return;
    SomeMethod();
    //Other code
    }

    what i want to achieve is something like this

    [ExecuteOnlyAtRuntime]
    public void SomeMethod()
    {
    //Code here
    }

    then call it like this

    public ctrl_information()
    {
    InitializeComponent();
    //if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)) return; -- removing this line
    //now the SomeMethod() will only be executed at runtime not in design time
    SomeMethod();
    }

    Is it possible? Please shed some light on this. Thank you

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #3

    Gilbert Consellado wrote:

    Is there a way to prevent a method from execution if it is in design time

    The code does not run in the designer, it gets compiled. If the designer raises an error then you need to show us what it is.

    G 1 Reply Last reply
    0
    • G Gilbert Consellado

      Is there a way to prevent a method from execution if it is in design time or the method can only be executed at runtime. I got this problem while creating a custom control because there is a method call in the constructor that will only work at runtime. now at design time while designing the form and use that control, then the form will generate the error. now i tried this at the constructor of the user control

      public ctrl_information()
      {
      InitializeComponent();
      if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)) return;
      SomeMethod();
      //Other code
      }

      what i want to achieve is something like this

      [ExecuteOnlyAtRuntime]
      public void SomeMethod()
      {
      //Code here
      }

      then call it like this

      public ctrl_information()
      {
      InitializeComponent();
      //if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)) return; -- removing this line
      //now the SomeMethod() will only be executed at runtime not in design time
      SomeMethod();
      }

      Is it possible? Please shed some light on this. Thank you

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #4

      You don't need an attribute, there is a Control property which does this for you: DesignMode[^] So all you have to do in your constructor is:

      public ctrl_information()
      {
      InitializeComponent();
      if (!DesignMode)
      {
      SomeMethod();
      }
      //Other code
      }

      Because a Form is derived from Control, this works there as well.

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      G 1 Reply Last reply
      0
      • L Lost User

        Gilbert Consellado wrote:

        Is there a way to prevent a method from execution if it is in design time

        The code does not run in the designer, it gets compiled. If the designer raises an error then you need to show us what it is.

        G Offline
        G Offline
        Gilbert Consellado
        wrote on last edited by
        #5

        I have a process inside the constructor of the usercontrol, then i get this error "The given key was not present in the dictinoary" on the designer from the form that uses the usercontrol. It happen because at the design time the form will try to connect to the database but the connectionstring can be generated at runtime.

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          You don't need an attribute, there is a Control property which does this for you: DesignMode[^] So all you have to do in your constructor is:

          public ctrl_information()
          {
          InitializeComponent();
          if (!DesignMode)
          {
          SomeMethod();
          }
          //Other code
          }

          Because a Form is derived from Control, this works there as well.

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          G Offline
          G Offline
          Gilbert Consellado
          wrote on last edited by
          #6

          unfortunately, it doesn't work on me

          OriginalGriffO 1 Reply Last reply
          0
          • G Gilbert Consellado

            unfortunately, it doesn't work on me

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #7

            So show us exactly what code you are using (relevant bits, only, please!)

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            G 2 Replies Last reply
            0
            • OriginalGriffO OriginalGriff

              So show us exactly what code you are using (relevant bits, only, please!)

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              G Offline
              G Offline
              Gilbert Consellado
              wrote on last edited by
              #8

              here is the constructor of the usercontrol

              public ctrl_patient_information()
              {
              InitializeComponent();
              InitializeControl();
              }

                  public void InitializeControl()
                  {
                      Patient = Patient ?? new Patient();
                      PatientContact = Patient.GetPatientContact();
                      PatientImage = new Patient.PatientImage(Patient);
                      pictureedit\_patient.Image = PatientImage.GetPatientImage();
              
                      PopulateCombos();
                      InitValidation();
              
                      Patient.PropertyChanged += Patient\_PropertyChanged;
                      InitControlBinding();
                  }
              
              	private void PopulateCombos()
              	{
              		combo\_titles.Properties.Items.AddRange(new PatientTitle\[\] { PatientTitle.MR, PatientTitle.MS });
              		combo\_titles.SelectedIndex = -1;
              		combo\_titles.Select();
              
              		combo\_gender.Properties.Items.AddRange(new Gender\[\] { Gender.Male, Gender.Female });
              		combo\_gender.SelectedIndex = -1;
              
              		combo\_religion.Properties.Items.AddRange(Religion.GetReligions(false));// the problem start here
              		combo\_religion.Leave += Religion.ValidateControlReligionItem;
              
              		combo\_marital\_status.Properties.Items.AddRange(
              				new MaritalStatus\[\]
              				{
              					MaritalStatus.Single,
              					MaritalStatus.Married,
              					MaritalStatus.Widow,
              					MaritalStatus.Seperated
              				}
              			);
              	}
              

              when the

              Religion.GetReligions(false)

              been called it will try to connect to DB but the problem is the application is still in designmode, and the connectionstring for the DB is generated at runtime, so VS cant connect to db. then the Form that use the usercontrol will get an error at designtime.

              public static BindingList<Religion> GetReligions(bool reset)
              {
              if (!reset && _bindingListItem.Count > 0) return _bindingListItem;
              DisableRaiseChangedEvent();
              _bindingListItem.Clear();
              _bindingListItemPointer.Clear();

              var ms = new MySQLSelect("SELECT \* FROM \`tbl\_religion\` WHERE \`tracked\_id\` = @tid");
              ms.AddParamWithValue("tid", HIMSHelper.TrackedID);
              foreach (var item in ms.DataReader())
              {
                  var r = new Religion()
                  {
                      ID = item\[0\].ToInt(),
                      ReligionName = item\[2\].ToString()
                  };
                  \_bindingListItem.Add(r);
                  \_bindingListItemPointer.Add(r.ID, r);
              }
              EnableRaiseChangedEvent();
              return \_bindingListItem;
              

              }

              for now i have a temporary workaround, just to make it wo

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                So show us exactly what code you are using (relevant bits, only, please!)

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                G Offline
                G Offline
                Gilbert Consellado
                wrote on last edited by
                #9

                for now my workaround, instead having a condition

                if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)) return;

                I just make

                InitializeControl()

                public then call it separately on the Form that will use the control.

                OriginalGriffO 1 Reply Last reply
                0
                • G Gilbert Consellado

                  for now my workaround, instead having a condition

                  if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)) return;

                  I just make

                  InitializeControl()

                  public then call it separately on the Form that will use the control.

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #10

                  That's not a good idea - since your control relies on it in order to work at all, it kinda breaks OOPs to make the container remember to call it. I'd probably just change it to this:

                      public void InitializeControl()
                      {
                          if (!DesignMode)
                          {
                              Patient = Patient ?? new Patient();
                              PatientContact = Patient.GetPatientContact();
                              PatientImage = new Patient.PatientImage(Patient);
                              pictureedit\_patient.Image = PatientImage.GetPatientImage();
                   
                              PopulateCombos();
                              InitValidation();
                   
                              Patient.PropertyChanged += Patient\_PropertyChanged;
                              InitControlBinding();
                          }
                      }
                  

                  So that the actual content didn't get filled in except in run mode.

                  Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  G 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    That's not a good idea - since your control relies on it in order to work at all, it kinda breaks OOPs to make the container remember to call it. I'd probably just change it to this:

                        public void InitializeControl()
                        {
                            if (!DesignMode)
                            {
                                Patient = Patient ?? new Patient();
                                PatientContact = Patient.GetPatientContact();
                                PatientImage = new Patient.PatientImage(Patient);
                                pictureedit\_patient.Image = PatientImage.GetPatientImage();
                     
                                PopulateCombos();
                                InitValidation();
                     
                                Patient.PropertyChanged += Patient\_PropertyChanged;
                                InitControlBinding();
                            }
                        }
                    

                    So that the actual content didn't get filled in except in run mode.

                    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                    G Offline
                    G Offline
                    Gilbert Consellado
                    wrote on last edited by
                    #11

                    I dont know why but DesignMode is not working, its still execute the whole block inside the "if" statment. and it give me this messages when opening the form.

                    Exception has been thrown by the target of an invocation.

                    The variable 'ctrl_patient_information' is either undeclared or was never assigned.

                    I think i am gonna use LicenseManager.UsageMode for now. BTW thank you for your help

                    1 Reply Last reply
                    0
                    • G Gilbert Consellado

                      Is there a way to prevent a method from execution if it is in design time or the method can only be executed at runtime. I got this problem while creating a custom control because there is a method call in the constructor that will only work at runtime. now at design time while designing the form and use that control, then the form will generate the error. now i tried this at the constructor of the user control

                      public ctrl_information()
                      {
                      InitializeComponent();
                      if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)) return;
                      SomeMethod();
                      //Other code
                      }

                      what i want to achieve is something like this

                      [ExecuteOnlyAtRuntime]
                      public void SomeMethod()
                      {
                      //Code here
                      }

                      then call it like this

                      public ctrl_information()
                      {
                      InitializeComponent();
                      //if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)) return; -- removing this line
                      //now the SomeMethod() will only be executed at runtime not in design time
                      SomeMethod();
                      }

                      Is it possible? Please shed some light on this. Thank you

                      N Offline
                      N Offline
                      Nathan Minier
                      wrote on last edited by
                      #12

                      Assuming that you mean Debug and Release, you can use pre-processor directives to do what you want:

                      public ctrl_information()
                      {
                      InitializeComponent();

                          #if(!DEBUG)
                          if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)) return;
                          #endif
                      
                          SomeMethod();
                          //Other code
                      }
                      
                      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