Search code examples
c#visual-studiohardcode

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


How can I correctly replace hardcoded string for labels in Form1.Designers.cs?

Instead of:

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

I want to write:

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

After changing form in visual designer (add any component and save) VS code generator turns back label text to hardcoded string:

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

Is anybody knows how to solve this issue?


Solution

  • Prevent Auto code change 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;
             }
         }
    }