对象与目标类型不匹配

gon*_*ins 6 .net c# label picturebox winforms

我有一个控制TableLayoutPanel网格PictureBox.我正在尝试找到一种快捷方式将它们全部更改为Label控件,而不是手动删除每个控件并在每个单元格中放置新控件.

我以为我可以进入设计器代码并使用Label查找/替换PictureBox,但现在我得到了一个

"对象与目标类型不匹配"

Visual Studio的错误列表中的错误.我现在也无法查看设计器页面.这是不允许的?如果允许,那么正确的方法是什么?

bas*_*bas 15

如果您仔细查看生成的代码:

label1:

this.label1 = new System.Windows.Forms.Label();
// 
// label1
// 
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(134, 163);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
Run Code Online (Sandbox Code Playgroud)

pictureBox1:

this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
// 
// pictureBox1
// 
this.pictureBox1.Location = new System.Drawing.Point(97, 75);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(100, 50);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
Run Code Online (Sandbox Code Playgroud)

我的猜测是

((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
Run Code Online (Sandbox Code Playgroud)

由你改变成:

((System.ComponentModel.ISupportInitialize)(this.label1)).BeginInit();
Run Code Online (Sandbox Code Playgroud)

这不起作用,并导致设计师的问题. Object does not match target type.

所以,应用你已经做过的更改,删除如下行:

((System.ComponentModel.ISupportInitialize)(this.label1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.label1)).EndInit();
Run Code Online (Sandbox Code Playgroud)

而且我觉得你很高兴.

  • 不,请查看BeginInit上的文档"发信号通知初始化正在启动的对象".对于没有实现`ISupportInitialize`接口的控件,这是不可能的(当然也不需要). (2认同)