在Winforms项目中使用 ListView 控件(Visual Studio 2010),我注意到虽然我已经为其ColumnHeaders的 Name 属性分配了有意义的值,但是列设计器,调用columnHeaderFoo.Name
在运行时返回一个空字符串。这与其他控件的行为不同,例如在同一表单上,buttonOK.Name
会产生"buttonOK"
。
这是我设置值的地方:
或者在这里,结果相同:
这是设计器生成的代码,其中包含一个包含一列的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:
另一个有趣的事实是,项目的任何文件都没有以字符串常量的形式包含我作为“Name”给出的值;似乎VS仅使用此值来命名成员变量。
如果我将 Name 属性设置为运行时,则会按预期保留该值。我似乎总是错误地认为表单设计器使用标记为“Name”的字段来分配 Name 属性。
有没有办法在设计时明确定义控件的名称(而不是变量的名称)?
答案 0 :(得分: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";
当我解决这个问题时,我正确地获得了价值......
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";
然后你会得到这个名字.... ...检查这个