Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object does not match target type

I have a TableLayoutPanel with a grid of PictureBox controls within it. I'm trying to find a shortcut way to change them all to Label controls instead of manually deleting each one and placing new controls in each cell.

I thought I could go into the designer code and find/replace PictureBox with Label, but now I get an

"Object does not match target type"

error in Visual Studio's error list. I can't view the designer page now either. Is this not allowed? If it is allowed, what's the right way to do it?

like image 544
gonzobrains Avatar asked Jan 29 '13 20:01

gonzobrains


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

My guess is that the

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

is changed by you into something like:

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

which doesn't work, and results in designer issues. Object does not match target type.

so, apply the changes you already did, remove the lines like:

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

and I think you're good to go.

like image 127
bas Avatar answered Oct 06 '22 05:10

bas


Don't change designer code. That stuff is automatically generated. Not only can your changes cause unexpected behavior, but they can get over-written as well.

I would attempt to make a change or 2 to your form, or whatever your designer is behind, and hope it regenerates all it's code.

like image 44
Sam I am says Reinstate Monica Avatar answered Oct 06 '22 06:10

Sam I am says Reinstate Monica


You can delete all the picture boxes in the designer, then add all the labels in the _load event (or another convenient event). That way it will be easier to change next time.

like image 25
xpda Avatar answered Oct 06 '22 05:10

xpda