4

Form1.Designers.cs のラベルのハードコードされた文字列を正しく置き換えるにはどうすればよいですか?

それ以外の:

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

私は書きたい:

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

ビジュアル デザイナーでフォームを変更した後 (コンポーネントを追加して保存)、VS コード ジェネレーターはラベル テキストをハードコーディングされた文字列に戻します。

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

この問題を解決する方法を知っている人はいますか?

4

3 に答える 3

3

特定の行の Designer.cs でコードの自動変更を防止する

using System;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
     {
       public Form1()
         {
           InitializeComponent();
           this.button1.Text = Resources.ButtonText;
         }
     }
}
于 2013-11-19T06:37:37.807 に答える
2

最も簡単なのは、 / からプロパティを取得する独自の / を作成することButtonです。LabelTextResources

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;
    }
}
于 2013-11-19T06:40:26.830 に答える