Winforms プロジェクト (Visual Studio 2010) でListViewコントロールを使用すると、列デザイナーでその ColumnHeadersのNamecolumnHeaderFoo.Name
プロパティに意味のある値を割り当てたにもかかわらず、実行時に呼び出しが空の文字列を返すことに気付きました。buttonOK.Name
これは、同じフォームのyieldsなど、他のコントロールの動作とは異なります"buttonOK"
。
値を設定した場所は次のとおりです。
別の方法として、同じ結果が得られます:
これは、1 つの列を含む ListView を持つフォーム用にデザイナーが生成したコードです。
namespace ColumnDemo
{
partial class Form1
{
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Verwendete Ressourcen bereinigen.
/// </summary>
/// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Vom Windows Form-Designer generierter Code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeaderTest = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeaderTest});
this.listView1.Location = new System.Drawing.Point(12, 12);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(494, 149);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(411, 167);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(95, 43);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(518, 221);
this.Controls.Add(this.button1);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeaderTest;
private System.Windows.Forms.Button button1;
}
}
ボタンのクリック時に出力を追加します。
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("Button: " + button1.Name);
Console.WriteLine("Column: " + columnHeaderTest.Name);
}
結果の出力は次のとおりです。
Button: button1
Column:
もう 1 つの興味深い事実は、プロジェクトのどのファイルにも、"Name" として指定した値が文字列定数の形式で含まれていないことです。VS はこの値を排他的に使用してメンバー変数に名前を付けているようです。
Nameプロパティをランタイムとして設定すると、値は期待どおりに保持されます。フォーム デザイナーが "Name" というラベルの付いたフィールドを使用してNameプロパティを割り当てると、私はいつも間違って想定していたようです。
設計時にコントロールの名前 (変数の名前ではない) を明確に定義する方法はありますか?