If you have a Form that displays data, one thing you can do is reference this.DesignMode
in the constructor to avoid populating it in the designer:
public partial class SetupForm : Form
{
private SetupItemContainer container = new SetupItemContainer();
public SetupForm()
{
InitializeComponent();
if (!this.DesignMode)
{
this.bindingSource1.DataSource = this.container;
this.Fill();
}
}
}
However, if you decide to re-write that form as a UserControl, keeping the same constructor logic, something unexpected happens - this.DesignMode
is always false no matter what. This leads to the designer invoking your logic that's meant to happen at runtime.
I just found a comment on a blog post that seem to give a fix to this but it references functionality of the LicenseManager class as a replacement that works as expected in a UserControl.
So for a UserControl I can do:
public partial class AffiliateSetup : UserControl
{
private AffiliateItemContainer container = new AffiliateItemContainer();
public AffiliateSetup()
{
InitializeComponent();
if (LicenseManager.UsageMode == LicenseUsageMode.Runtime)
{
this.bindingSource1.DataSource = this.container;
this.Fill();
}
}
}
Does using the LicenseManager
instead of DesignMode
have any caveats or implications that might dissuade me from putting in my production code?
According to someone who posted a comment on my answer to another question, using LicenseManager
doesn't work in an OnPaint
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With