prevent method execution on design time using attributes
-
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
-
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
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...
-
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.
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.
-
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...
unfortunately, it doesn't work on me
-
unfortunately, it doesn't work on me
So show us exactly what code you are using (relevant bits, only, please!)
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
So show us exactly what code you are using (relevant bits, only, please!)
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
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
-
So show us exactly what code you are using (relevant bits, only, please!)
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
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.
-
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.
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...
-
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 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
-
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
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 }