-1

An issue I faced is - I have the custom UserControl which I would like to add to my TabControl as an additional tab, so to make it I just found my UserControl in the toolbox and pulled it to my TabControl and immediately I get an error popup which says

Failed to load toolbox item. It will be removed from the toolbox

After research, I found a few answers like these

https://stackoverflow.com/a/44628024/5709159

and this one

https://social.msdn.microsoft.com/Forums/vstudio/en-US/77e10b58-43cc-4aab-919f-888f14f99571/x64-class-library-of-user-controls?forum=csharpgeneral

They are talking about the mismatch between 32 and 64-bit processing, but I really can't get the idea about how to fix it? Like where I need to click or what I need to recompile to make it work? If someone could translate me what is written at those links to the more clear language I'll appreciate it.

EDIT

I am trying to add a completely new UserControl, which means I create a new UserControl, and as is I open the toolbox and pull it in my tab that was created in tab control as a result I get an error above. So, there are no constructors or early access to parent issues.

Regarding CPU I don't use AnyCPU I use x64 could it be an issue? And also in order to check, I can't switch to AnyCPU because my project is not compatible with AnyCPU.

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121
  • Before you jump to that. Double check your user control for possible bugs. Like trying to do something in the constructor while the handle has not been created yet. Like trying to access objects (in `OnPaint` for example) while the related objects have not been created yet (nulls). Like trying to _early_ access the parent control while its `null`. Like .... etc. – dr.null Jan 12 '21 at 16:17
  • As the above. Plus, you need to specify if this UserControl is part of another Solution or the same Solution where it's used and what is the Profile used to build it. If you set `AnyCPU`, you'll be fine. Visual Studio should warn you in case the bitness is different (and, IIRC, this also prevents the Solution from building). – Jimi Jan 12 '21 at 16:19
  • @dr.null edited my question, looks like it should not be an issue – Sirop4ik Jan 13 '21 at 07:54
  • @Jimi edited my question, looks like it could be possible that problem in architecture that I use, so does it mean that there is no way to use toolbox if I use x64? – Sirop4ik Jan 13 '21 at 07:56
  • I think we need one more edit to help us understand and try to repro the issue. Which Framework? The structure of your solution (see the second comment)? Maybe some code from that control? Are you calling any `win32` methods that don't respect the `IntPtr.Size`? .... – dr.null Jan 13 '21 at 10:38
  • Do you have external packages that force you to build as `x64`? Is it enough to set the Platform Target (`Project->Properties->Build->Platform target`) to `x64`? If so, build your User Control with the `AnyCPU` profile, open up the ToolBox and pin it, drag the UserControl `dll` onto the ToolBox lower area, then drop one instance into a Form, the Reference is added automatically. – Jimi Jan 13 '21 at 15:18

3 Answers3

0

In your project properties, try changing the Platform target in the build tab to x86. It worked for me when I had a similar problem.

0

In my case, it was the other way round: I implemented a class derived from ElementHost as a wrapper around a WPF control, in order to be able to integrate it into Winforms. When I added the ElementHost to the form programmatically, everything went fine, but when I wanted to drag&drop it from the VS toolbox, I got the "Failed to load toolbox item" message.

The reason was that I used the x64 version of Visual Studio, which seems to require the ElementHost derived class being in an "Any CPU" assembly. So the solution was to set the assembly containing the control to "Any CPU".

Heinz Kessler
  • 1,610
  • 11
  • 24
0

For me the trick was to switch the Platform target to x86 (Project properties -> Build), then start the project to rebuild it, stop it again and switch back to Any CPU. After that I can add any UserControl in any way I like.

Spider IT
  • 73
  • 1
  • 10