2013-10-16 95 views
0

使用的WinForms項目的ListView控制(Visual Studio 2010中)一個空字符串,我注意到,雖然我已經在列分配有意義的值到名稱及其ColumnHeaders的財產設計者,在運行時調用columnHeaderFoo.Name返回空字符串。這與其他控制的行爲不同,例如,在同一表格上,buttonOK.Name收益率爲"buttonOK"ColumnHeader.Name產生在運行時

此處,我設置的值:

Using the column designer to set the column name

或者在這裏,同樣的結果:

Using the property editor to set the column name

這對於一個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: 

另一個有趣的事實是,沒有一個項目的文件包含我給的「名稱」的值隨時隨地字符串常量的形式;它似乎VS專門用這個值來命名成員變量。

如果我將名稱屬性設置爲運行時,則該值將按預期方式保留。似乎我總是錯誤地認爲表單設計者使用標有「名稱」的字段來分配名稱屬性。

有什麼辦法可以在設計時明確定義控件的名稱(不是變量的名稱)嗎?

+1

分享你的代碼... !!! – Aravind

+0

我使用ListView-ColumnHeaders的Visual Studio設計器來設置名稱(無代碼),然後使用'columnHeaderFoo.Name'嘗試讀出它。 – waldrumpus

+0

清潔和重建項目...一次,並嘗試... – Aravind

回答

0

你在哪裏設置這個

this.button1.Name = "button1"; // This value is Set !!!!! 
    this.columnHeaderTest.Name ----> this value is not set that's why you are getting empty. 
this.columnHeaderTest.Name = "Test"; 

當我SETT這個我得到正確的價值...

private void button1_Click(object sender, EventArgs e) 
    { 
     Console.WriteLine("Button: " + button1.Name); 
     Console.WriteLine("Column: " + columnHeaderTest.Name); 
    } 
columnHeaderTest.Name 
"Test" 


as you showed in screenshot . just change the " columnHeaderTest to Testing " .... this is 
Control Name ... you have define your controls Display name that is 

Testing.Name = "NewColumn"; 

那麼你會得到的名字.... ...檢查這個

+0

你是正確的,當我在它保留的代碼中設置值時。我的問題是爲什麼當我將其設置在設計器中時,它不工作(也許這是通過設計,也許我錯了)? – waldrumpus

+0

Waldrumpus ... ListView是Control和Columheader是它內部的另一個控件..當你創建新的columnheader時,它不會創建ColumnHeader名稱,它只會在列表視圖中創建一個控件,這樣你就不會得到任何名稱屬性,只有我說你必須手動設置名稱...這是Listview columnheader的默認設計 – Aravind