How to replace gated string for label text in * .Designer.cs C #?

How to replace string string for labels in Form1.Designers.cs?

Instead:

        this.button1.Text = "TheButton";
        this.label1.Text = "TheLabel";

I want to write:

        this.button1.Text = Resources.ButtonText;
        this.label1.Text = Resources.LabelText;

After changing the form in the visual designer (add some component and save), the VS code generator will turn the label text into a solid line:

        this.button1.Text = Resources.ButtonText;
        this.label1.Text = "TheLabel";

Does anyone know how to solve this problem?

+4
source share
2 answers

Prevent automatic code changes in Designer.cs for a specific line

using System;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
     {
       public Form1()
         {
           InitializeComponent();
           this.button1.Text = Resources.ButtonText;
         }
     }
}
+3
source

The easiest way is to create your own Button/ Label, where it takes its property Textfrom Resources:

class MyButton: Button
{
    // put here attributes to stop its serialization into code and displaying in the designer
    public new string Text
    {
        get {return base.Text;}
        set {base.Text = value;}
    }

    public MyButton() : base()
    {
        base.Text = Resources.ButtonText;
    }
}
+2
source

All Articles