1

I would like to display a reminder when opening and editing a certain form in our project. This would be while in Design Mode in Visual Studio.

I tried putting MessageBox.Show in the constructor, Paint and Load event but Nothing seems to work. Is it even possible?

public Form1()
{
    InitializeComponent();

    if (this.DesignMode)
    {
        MessageBox.Show("Remember to fix the xyz control!");
    }

    if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
    {
        MessageBox.Show("Remember to fix the xyz control!");
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
GER
  • 1,870
  • 3
  • 23
  • 30
  • Not sure about a dialog box, but you could use TODO: [Using the Task List](https://msdn.microsoft.com/en-us/library/txtwdysk.aspx) – 001 Jun 17 '16 at 17:44
  • 1
    The code in constructor of your form will not execute at design time. The designer creates an instance of base class of your form and it doesn't look in constructor of your form. So if you need a code to be executed at design time, put the code in constructor of base class of your form. To learn more about how designer works and see some interesting examples, take a look at these posts: [Can't view designer when coding a form in C#](http://stackoverflow.com/a/32299687/3110834) and [Show controls added programatically in WinForms app in Design view?](http://stackoverflow.com/a/33535154/3110834) – Reza Aghaei Jun 17 '16 at 18:03
  • 1
    This works fine. But Form1 must be the *base class* of the form you are designing. The default base class is `Form`, it doesn't complain. And above all, it doesn't have to be compiled, that was already done. – Hans Passant Jun 17 '16 at 18:03
  • Reza and Hans, putting it in a base form worked well and is how I will implement this if someone doesn't respond with a more magical answer of how to do it in Form1. – GER Jun 17 '16 at 18:33

1 Answers1

1

You can do it in following way:

Create a base form:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Load += new EventHandler(Form1_Load);
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
        if (this.DesignMode && LicenseManager.UsageMode == LicenseUsageMode.Designtime)
        {
            MessageBox.Show("Hello");
        }
    }

}

And on the second form on wards where you would like to show the message box, you will just have to inherit it, like below:

public partial class Form2 : Form1
{
    public Form2()
    {
        InitializeComponent();
    }
}

and as soon as you open a form in design time, it will show you message box.

This worked for me, I hope this will help you. :)

Hitesh
  • 3,508
  • 1
  • 18
  • 24