0

I have a small WinForms application with only a button at the Center of the main window. That's button text is Button 1. The constructor in code behind (F7) change thats button text to Hello World after the call to InitializeComponent.

The runtime Shows "Hello World" but at design time the Designer Shows "Button 1". What is the correct way in WinForms to run custom UI-code. If I manually put that code into the designer-file than my changes gets overridden next time when designer is saved.

Here is my code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.button1.Text = "Hello World";
    }
}
Alois
  • 361
  • 2
  • 18
  • 2
    You can change the Button Text from Properties window if you'd like so it shows the same as Run Time. – Seckin Celik Dec 20 '17 at 12:33
  • You can also enter the form designer (`Form1.designer.cs`) to manually edit your controls properties. – Beltaine Dec 20 '17 at 12:34
  • 1
    The constr of `Form1` gets only executed once you launch your application, that's the intention. But if you set the `Text` right away after you initialised all the objects and stuff you can already set the `Text` in the designer class. By the way, there is no *code behind*, this class and the designer class are `partial` classes, so the both form together one single class (`Form1`), but the designing stuff of `Form1` got outsourced into the designer class to simplify code for better understanding and a more clear overview. – L. Guthardt Dec 20 '17 at 12:34
  • @SeckinCelik: Of course the real problem is a bit more complicated (setting Icons of ribbon button by an enum instead of a resource by using a special method). – Alois Dec 20 '17 at 13:48
  • @L.Guthardt: They are partial but it seems that the designer runs the design-file in another context. Because if you call a custom method from the designer files initialize method then the designer will show an error with that message: "Method 'System.Windows.Forms.Form.xxx' not found." – Alois Dec 20 '17 at 13:48
  • Take a look at this post about [how designer works](https://stackoverflow.com/a/32299687/3110834). – Reza Aghaei Jan 03 '18 at 15:42

1 Answers1

1

Title say:

WinForms Designer should run method from CodeBehind?

Designer is tricky, since you are designing the new type Form1 it will actually create an instance of base class Form and then let you to modify it, generating designer.cs and resources for you.

To ensure code will run in designer, you have to move it to base class. This can be achieved by making custom control:

public class MyForm: Form
{
    protected button1;

    public MyForm()
    {
        button1 = new Button { Text = "Hello World" }; // here
        Controls.Add(button1);
    }
}

Now when you inherit new form from it all those forms will have a button.

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

To make it easier for new users there is UserControl. While editing it the changes in code behind are not show in designer directly. But once you finish, compile it and add it to another form - designer will be running your code there when editing that another form. You will have to re-compile after every change to code behind of user control though.

Sinatr
  • 20,892
  • 15
  • 90
  • 319