トップ回答者
フォームに配置した拡張コントロール(TextBox)のReadOnlyプロパティが保存されない

質問
-
いなぁ です。 お世話になります。
フォームのデザイン画面に配置した拡張コントロールの
ReadOnlyプロパティを変更しても保存されません。例として、プロジェクトに
public partial class Form1 : Form
public partial class Component1 : System.Windows.Forms.TextBox
があります。このTextBoxの拡張コントールをReadOnlyプロパティをtrueに設定します。
Component1.Designer.csを確認すると
private void InitializeComponent()
{
this.SuspendLayout();
this.ReadOnly = true;
this.ResumeLayout(false);
}
と記載されています。その後、Form1のデザイン画面を開いてComponent1を貼り付けて
component11の名前で保存します。
component11のReadOnlyをfalseに変更しても
保持されず、実行したり再度デザイン画面を開くと
component11のReadOnlyがtrueに戻ってしまいます。もちろんForm1.csに
component11.ReadOnly = false;
とコーディングすれば反映されますが...拡張コントロールに何か設定が必要なのでしょうか?
環境は、Windows7 32bit, Microsoft Visual C# 2008 です。
回答
-
TextBoxクラスのReadOnlyプロパティのDefaultValue属性値はfalseです。
この為、デザイナ上でプロパティ値をfalseにしても、Designer.csに出力されません。
※デザイナ上でプロパティをtrueにした場合
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(20, 238);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(100, 19);
this.textBox1.TabIndex = 0;
※デザイナ上でプロパティをfalseにした場合
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(20, 238);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 19);
this.textBox1.TabIndex = 0;上記の結果、規定クラスのInitializeComponentメソッドを呼ばれた後に変更されない為、trueになります。
継承コントロールのクラスに、下記のプロパティを足してみて下さい。
///-------------------------------------------------------------------------------- /// <summary> /// テキスト ボックスのテキストが読み取り専用かどうかを示す値を取得または設定します。 /// </summary> ///-------------------------------------------------------------------------------- [Browsable(true), Category("動作"), DefaultValue(true), Description("テキスト ボックスのテキストが読み取り専用かどうかを示す値を取得または設定します。")] public new bool ReadOnly { get { return base.ReadOnly; } set { base.ReadOnly = value; } }
これで予定通りの動作をするようであれば、これが原因だと思われます。- 回答としてマーク いなぁ 2013年1月23日 9:05
すべての返信
-
TextBoxクラスのReadOnlyプロパティのDefaultValue属性値はfalseです。
この為、デザイナ上でプロパティ値をfalseにしても、Designer.csに出力されません。
※デザイナ上でプロパティをtrueにした場合
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(20, 238);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(100, 19);
this.textBox1.TabIndex = 0;
※デザイナ上でプロパティをfalseにした場合
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(20, 238);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 19);
this.textBox1.TabIndex = 0;上記の結果、規定クラスのInitializeComponentメソッドを呼ばれた後に変更されない為、trueになります。
継承コントロールのクラスに、下記のプロパティを足してみて下さい。
///-------------------------------------------------------------------------------- /// <summary> /// テキスト ボックスのテキストが読み取り専用かどうかを示す値を取得または設定します。 /// </summary> ///-------------------------------------------------------------------------------- [Browsable(true), Category("動作"), DefaultValue(true), Description("テキスト ボックスのテキストが読み取り専用かどうかを示す値を取得または設定します。")] public new bool ReadOnly { get { return base.ReadOnly; } set { base.ReadOnly = value; } }
これで予定通りの動作をするようであれば、これが原因だと思われます。- 回答としてマーク いなぁ 2013年1月23日 9:05