Note that there are some explanatory texts on larger screens.

plurals
  1. POColumnHeader.Name yields an empty string at runtime
    primarykey
    data
    text
    <p>Using the <em>ListView</em> control in Winforms projects (Visual Studio 2010), I've noticed that although I have assigned meaningful values to the <em>Name</em> property of its ColumnHeaders in the column designer, calling <code>columnHeaderFoo.Name</code> returns an empty string at runtime. This differs from the behavior of other controls, e.g. on the same form, <code>buttonOK.Name</code> yields <code>"buttonOK"</code>. </p> <p>Here's where I set the value:</p> <p><img src="https://i.stack.imgur.com/sEViu.png" alt="Using the column designer to set the column name"></p> <p>Alternatively here, with the same result:</p> <p><img src="https://i.stack.imgur.com/rfh1d.png" alt="Using the property editor to set the column name"></p> <p>This is the designer-generated code for a form with a ListView containing one column:</p> <pre><code>namespace ColumnDemo { partial class Form1 { /// &lt;summary&gt; /// Erforderliche Designervariable. /// &lt;/summary&gt; private System.ComponentModel.IContainer components = null; /// &lt;summary&gt; /// Verwendete Ressourcen bereinigen. /// &lt;/summary&gt; /// &lt;param name="disposing"&gt;True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.&lt;/param&gt; protected override void Dispose(bool disposing) { if (disposing &amp;&amp; (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Vom Windows Form-Designer generierter Code /// &lt;summary&gt; /// Erforderliche Methode für die Designerunterstützung. /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden. /// &lt;/summary&gt; 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; } } </code></pre> <p>I add some output on button click:</p> <pre><code>private void button1_Click(object sender, EventArgs e) { Console.WriteLine("Button: " + button1.Name); Console.WriteLine("Column: " + columnHeaderTest.Name); } </code></pre> <p>The resulting output is:</p> <pre><code>Button: button1 Column: </code></pre> <p>Another interesting fact is that none of the project's files contain the value I gave as "Name" anywhere in the form of a string constant; it seems VS uses this value exclusively to name the member variable. </p> <p>If I set the <em>Name</em> property as runtime, the value is retained as expected. It seems I always just wrongly assumed that the forms designer uses the field labeled "Name" to assign the <em>Name</em> property.</p> <p>Is there any way to unambiguously define a control's name (not the name of the variable) at design time?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload