Object does not match target type
Asked Answered
O

4

7

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?

Oxheart answered 29/1, 2013 at 20:37 Comment(6)
This is why you don't edit designer files directly...Traveled
@Traveled - that, and all of your changes will be erased next time you add/resize/do-anything-to a control.Spherule
meh, I can't count the times I quickly changed something in the designer code. If you know what you are doing, there is no problem. You probably missed something in the designer code. just check your changes carefully, I bet it's something small you looked overTrichinosis
I was trying to find a quick "find and replace" method of changing all of the controls on a form from one type to another without having to resort to manually deleting each control on the designer and creating new ones.Oxheart
what I would do, revert your changes back to what you had (with the picture box). then remove one picturebox and replace it with a label via the designer. then see what the differences are in the code behind. that way you can see if you can think of an ez way to apply the changes in the code behind, or that you are stuck with applying the changes 1 by 1 in the designerTrichinosis
check my example at the bottom. a label does not implement ISupportInitialize which causes the error you get. So delete the lines where you try to cast a label to ISupportInitialize and your designer will behave :)Trichinosis
T
17

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.

Trichinosis answered 29/1, 2013 at 20:55 Comment(6)
Do labels not require calls to BeginInit()? If this is the case, can you explain? Thanks!Oxheart
Nope, check the documentation on BeginInit "Signals the object that initialization is starting.". That's not possible (and of course not needed either) for controls that do not implement the ISupportInitialize interface.Trichinosis
The errors went away and I can see the designer form again but it doesn't quite look the way I expected it. But the error did go away!Oxheart
ah, maybe you also need to add a text :). label1.Text = "bla". And the other thing that might be a thingy is that autosize is normally set to true for labels. label1.AutoSize=true;. Then it looks quite OK here at least. :)Trichinosis
I want the labels to dock and fill. The labels were placed within cells of a TableLayoutPanel, but when I look at them in the designer I no longer see the "boundary lines" that I normally did around each cell.Oxheart
let us continue this discussion in chatTrichinosis
A
1

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.

Atlee answered 29/1, 2013 at 20:39 Comment(0)
C
1

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.

Columbian answered 29/1, 2013 at 20:44 Comment(0)
M
0

As Haxx illustrated, you will have to clean-up the extra initialization PictureBox requires as well. The error you received is a interface casting error. In your case, as Haxx guessed, the Label control doesn't implement the ISupportInitialize interface.

Unlike most, I am not afraid of changing designer code in the interest of expediency, for what you are doing, it is ok to do so. Just know your objects, check-in prior to doing so, and don't put custom code in there!

Multiphase answered 29/1, 2013 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.