5

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?

Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121

1 Answers1

1

According to someone who posted a comment on my answer to another question, using LicenseManager doesn't work in an OnPaint method.

Community
  • 1
  • 1
adrianbanks
  • 81,306
  • 22
  • 176
  • 206
  • That's pretty obscure. It sounds almost like it doesn't work in `OnPaint` because of a "circular reference" (Perhaps `OnPaint` won't fire in the first place if the licensing model is not correct?) – Robert Harvey Jul 09 '12 at 23:15
  • @RobertHarvey I agree, it is pretty obscure. I've not tested it so cannot confirm it is the case, but the comment has two upvotes so I assume that at least two people have encountered the behaviour. – adrianbanks Jul 09 '12 at 23:17