Object does not match target type

I have a TableLayoutPanel with a grid of PictureBox controls inside it. I am trying to find a shortcut to change all of them to Label controls instead of manually deleting each and setting new controls in each cell.

I thought I could go to the designer code and find / replace the PictureBox with a shortcut, but now I get

"Object does not match target type"

error in Visual Studio error list. Now I can not view the designer page. Isn't that allowed? If allowed, what is the correct way to do this?

+4
source share
4 answers

If you take a closer look at the generated code:

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"; 

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; 

I suppose that

 ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); 

changed by you to something like:

 ((System.ComponentModel.ISupportInitialize)(this.label1)).BeginInit(); 

which does not work, and leads to designer problems. Object does not match target type.

So, apply your changes, delete the lines, for example:

 ((System.ComponentModel.ISupportInitialize)(this.label1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.label1)).EndInit(); 

and I think you are good to go.

+12
source

Do not change the designer code. This material is automatically generated. Not only can your changes cause unexpected behavior, but they can also be overwritten.

I would try to make changes or 2 to your form, or whatever your designer, and we hope that he will restore everything that he encodes.

+1
source

You can remove all image fields in the constructor, and then add all the labels to the _load event (or other convenient event). So, next time it will be easier to change.

+1
source

As shown in Haxx, you will have to clear the additional initialization required by the PictureBox as well. The received error is an error while executing the interface. In your case, as Haxx suggested, the Label control does not implement the ISupportInitialize interface.

Unlike most, I’m not afraid to change the designer code in the interests of expediency, for what you do, this is normal. Just know your objects, register before you do this, and do not put non-standard code there!

0
source

All Articles