Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any caveats to swapping out DesignMode for LicenseManager.UsageMode in a WinForms UserControl constructor?

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?

like image 434
Aaron Anodide Avatar asked Nov 13 '22 02:11

Aaron Anodide


1 Answers

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

like image 170
adrianbanks Avatar answered Dec 22 '22 07:12

adrianbanks