Visual Studio toolbox custom controls only visible once - winforms

I know there are a lot of problems like that and you all might think this is a duplicate but NO... this problem is different.
When I add a custom user control to a project and rebuild it, the control is added to the toolbox. If I try to add another one it does not show up in the toolbox. If I close visual studio and reopen it the first control also was removed from the toolbox and will never appear again. From now on the project is broken and no control added, remove + readded will ever show up in the toolbox. So I create a new project and it starts from the beginning.
Before this tip appears I'll say it: Automatically Populate Toolbox is True. Nothing helps against this problem. Build Project/Solution, Rebuild, Clean+Rebuild, Restart of VS, Restart of Computer, Unload and reload the project, set toolbox back to default. Nothing works.
Any ideas??

You have to remember that the designer call your code constructor and some other methods at design time.
If you build the code and the control does not appears and it has not been hidden by an attribute, then probably the code you wrote is not compatible with the designer. For example, the control might throw an exception in design mode because of incorrect code.
The attribute is [ToolboxItem(false)]. If you have a base class that hide it, it will also be hidden in derived class if not specified otherwise.
You will have the same problem if that control is used in another component. You won't see it if it does not compile and run correctly.
The trick to debug that kind of problem, is to attach a debugger from a second instance of Visual Studio to find where in your code you throw an exception.
If the control does not work at run-time, better to fix that first as if it also fix design problem, it is simpler to debug that way.
Otherwise, you have to ensure that your project dependencies are correct. The toolbox will only shows component from DLLs that are referenced by the current project (or from itself I think).

Related

VS2013 Hangs when Adding UserControl to Form

The best way to explain this is to describe how I can reproduce the problem at will.
Open an existing Windows Form file in the designer
Open the toolbox and click a custom UserControl
Click in the form in the designer to add it
VS2013 hangs (does NOT crash); kill VS2013 and relaunch it
Open the form again
Reset the toolbox and re-add the DLL that contains my custom UserControls
Try adding the control to the form--it works just fine!
Close VS2013
Open VS2013
Repeat the task of adding the custom UserControl to a form
VS2013 hangs
So, the moral of the story is that after closing and reopening VS2013, any attempt to add a custom usercontrol will hang VS2013, and the only way to remedy it is to reset the toolbox and re-add the usercontrol DLL; that remedy only lasts until VS2013 is exited and launched again.
Lots of googling has yielded suggestions regarding using a second VS instance to attach to the first and see the exception that is thrown, but there's no exception being thrown; the application just hangs.
The one thing I found that most-closely described my issue turned out to be related to some remote-desktop software that I'm not using.
Anybody got any ideas? I haven't tried reinstalling Visual Studio, as that represents a fairly significant amount of down-time. Last resort sort of thing (and I've read some things that indicated it won't be helpful).
Edit: Apparently sometimes the reset/re-add doesn't make it better.
Edit: Some additional things I should have mentioned:
There is no devenv.exe when I try to use a second VS instance to
debug, so I end up choosing WDExpress.exe
The particular user control is mature and has not changed recently, and is in use on many forms in the project
The issue only occurs when adding a new instance of the control; the hang does not occur simply by attempting to launch the designer for a form that already contains one.
The latest VS2013 update may have fixed it; it's not happening right now and I don't want to anger the gods by trying to reproduce it again until I get my work done.
Hanging Visual Studio with a custom UserControl is very easy to do. A simple example is all it takes, double-click the design surface and write:
private void UserControl1_Load(object sender, EventArgs e) {
while (true) { }
}
Compile and drop the added control from the top of the toolbox and drop it on a form. Show is over.
You probably did not spend enough time using the guidance you already found or we'd have a stack trace to look at. Attaching a debugger is easy enough and you'd be ahead if the code throws an exception. But it doesn't, a hang like this doesn't throw. Exercise debugging this particular simple hang to find out how to do it:
Start VS again, Debug > Attach to Process > select devenv.exe
Important: click the Select button above the process list. You'll need to select the "Debug these code types" radio button and tick "Managed (v4.5, v4.0)" as well as "Native". Now click Attach.
Give it time to load the symbol files, takes quite a while, especially if that was never done before.
Use Debug > Break All.
If you are lucky then it now stops on the exact statement that causes the hang. Likely in this case. This is however not guaranteed, it might also break on one of the many threads that are running inside devenv.exe, which ever happened to be active at the exact moment the debugger broke in.
In which case you need to use Debug > Windows > Threads to select the thread on which the UserControl code is running. Should be the one labeled "Main Thread". Double-click it.
If the editor doesn't switch to the source code file with the hang then use Debug > Windows > Call Stack to get insight in how the hang occurred. Update the question with that trace if you need more help.
In general, prime candidates for code in your UserControl that can cause the hang are its constructor, its Load event and its Paint event. Use the DesignMode property to bypass dangerous code that is not likely to work well when it runs inside the designer instead of the regular process. And beware of the chicken-and-egg problem, if you have a plain bug in your UserControl then it can easily strike first at design time, before you get a chance to debug it like you normally would. Disabling chunks of code may be necessary to increment towards the bug, use DesignMode to get there.
One more detail, this is tagged [c++-cli]. You must select the old debugging engine to debug C++/CLI code. Tools > Options > Debugging > General > tick the "Use Managed Compatibility Mode" checkbox.
The issue seems to have vanished subsequent to updating VS2013 to 12.0.40629.0 Update 5, found by selecting TOOLS > Extensions and Updates, then selecting Updates on the left.
It may not work in all cases of seemingly-inexplicable UserControl-incited hangs in the Designer, but it seemed to solve the problem for me. I'm putting this here (rather than deleting the question) in case my experience can help someone else.

WPF: VS2012 designer shows "The component 'XXX' does not have a resource identified by the URI YYY"

As the title suggests, this error is thrown by the designer, which means the designer can't display my UserControl correctly which in turn means I can't navigate smoothly from element to element in this UserControl to make modification. Really annoying.
XXX is the name of my UserControl, while the URI YYY is actually XXX's path. So I don't know understand it can't find itself as resource. I googled this error, but most of them happened in the runtime. In my case it doesn't happen at all when I execute it. The description of this error is really not explanatory enough, because I am not sure who in the CLR is loading the file itself as a resource.
Another thing might be worth mentioning is, this error only happens after I build my application project, which the error UserControl resides in. After I click to clean the project, the designer can display the whole stuff (but obviously I can't clean the project every time before I make any change, since the building takes time)
It is possible that the control's .g.cs or .g.i.cs file has been corrupted. Try to clean, close visual studio and restart it. I think that helped for me in several cases especially when I copy paste controls from one solution to another.
Just delete subfolders in
%LOCALAPPDATA%\Microsoft\VisualStudio\12.0\Designer\ShadowCache
If you refactor a UserControl down a lower level project that the UserControl was previously referencing then you will see this error.
Removing the assembly reference from the namespace fixes the issue.
<UserControl xmlns:ui="clr-namespace:MyCompany.Core.UI;assembly=MyCompany.Core.UI"
should be
<UserControl xmlns:ui="clr-namespace:MyCompany.Core.UI"
The designer is not smart enough to highlight this.
I ran into the same issue, it compiled and ran fine, but the XAML editor/Designer complained. In my case, I found the solution to be that my user control was declared within a namespace in the XAML (x:Class="myNamespace.myUserControl") but not in the code behind. Adding the namespace declaration in the code behind solved my issue.
There is a possibility to debug Visual Studio Designer in the second attached Visual Studio.
See How to troubleshoot and debug Visual Studio design mode errors for details.
To display the UserControl, the designer has to instantiate the user control. Look for possible Null Reference Exceptions.
In my case the user control had an NRE due to a dependency not being injected. I added handling for the NRE and no more issue.

Why is Visual Studio 2010's solution explorer so friggin' jumpy?

I am getting the following error on when I try to compile:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.WinFX.targets(269,9):
error MC6000: Project file must
include the .NET Framework assembly
'WindowsBase, PresentationCore,
PresentationFramework' in the
reference list.
This project is the Web backend for a Silverlight Business Application with very little modification. When I do add these references, everything breaks. I've tried removing every single reference from my references until all I had left was a reference to System. I still get the error though. In fact it is the only error that shows up.
Edit
Figured it out, and I have a new question: I accidentally moved (dragged) a XAML file from my Silverlight front-end project. Visual Studio is very finicky and will drag something at the drop of a hat. When I click an item, even if I release the mouse right away, sometimes it will drag the item following the mouse. Is there a way to make it less jumpy?
If you copied or moved an item from another project, its "Build Action" property might be set to "Page". This happened to me, and changing it to "Resource" (because that's what the item was) fixed the problem.
I've copied this answer from another site. It was the problem I was facing.
http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/7bf38ed6-43f8-492e-a304-0842e6c7c6b2/

VS2010 Winforms Usercontrol changes not showing in parent form

I've created a series of usercontrols. Dragged one of the controls to the main form. It shows OK. Went back and made a change to the design of the user control. Rebuilt project. Cleaned project. manually removed all project binaries including the obj folder. Change still doesn't show on the usercontrol hosted on the main form. Dragging a new instance of the control from the tool box only shows the old version of the control.
Completely removed the controls from the toolbox and adding them again fixed the problem, but surely I don't have to do this everytime I make a change? All user controls are in the same assembly as the main form. Tool>Options>Windows Forms Designer>Auto toolbox Populate is set to true. Any ideas most appreciated. Thx
Do not add the control to the toolbox yourself. This puts a copy of the assembly in a private directory. Yes, this copy does not get updated automatically, you get the old control.
Rely on the "Auto toolbox populate" setting. It automatically adds any controls you have in your loaded project(s) to the toolbox. The added controls are at the top of the toolbox.
I agree with answer 1 that that is what is supposed to happen, and often does. But it doesn't always work and will work partially sometimes.
For example my current Solution.
Project 1 namespace ToolsTester - This is just a temporary form to test the view tools in the 2nd project.
Project 2 namespace UltimateFileFinder - This has three views.
The first 2 views (user controls) UltFilesView and UltSlideShow appeared as usual on the toolbar so I could drag them onto ToolsTester to try them out.
Then I added another user control UltPanel, this will not go onto the toolbox. Though it compiles without errors.
I have come across this problem a few times, sometimes removing and reasserting the reference fixes it. Other times I would have to remove the test project and create a new one.
I tested Visual Studio (full version) before release and this was one of the areas I reported back, so the problem was known pre-release.
This project I am on now I know that I will need to remove the test project and create a new one, in this case the tester is only a few simple caller and event responses, that is not always the case, there are times when the tester project can be quite complex because it is mimicing the target that the tools will be part of.

How to manually reload the Visual Studio designer for WPF

Is there any way to force the WPF designer to reload or refresh, without rebuilding the entire project or solution?
If there's an error, I get an option in the designer view to refresh the designer. But if I don't have an error, how can I get the designer to refresh after I've made a change?
I'm a little late but this is the best solution1 I've found so far: whenever the designer does stupid stuff, I just kill it's process.
Press Ctrl+Shift+Esc
Navigate to the Processes tab.
Kill XDesProc.exe
This also fixes issues for the properties window (like when it gets jammed and you can't type stuff into it).
1 This is a solution for designer issues. Your issues may also be caused by compilation problems, in which case just right click on the solution in the solution explorer, and clean it. The reason behind it is that sometimes the compilation loses synchronicity with the generated files from XAML, and cleaning the solution just deletes those intermediate files; it's like a reset so your compilation can start off with a clean slate.
To do it fast:
Comfortably it's usually the last one if sorted alphabetically.
When it is, it's almost like a ritual for me to quickly pop up the task manager, click any process, press End, Delete, Enter (done), Esc (exit task manager). Instead of restarting VS and waiting for all the loads & unloads, you can do that in 1-2 seconds.
In newer versions of Visual Studio there is an icon on the bottom of the designer to "Disable Project code". If you toggle this off and on it will reload the designer.
You can add this to the Tools menu in Visual Studio.
Once configured, use Tools..XAML Designer Restart:
Alt+T then L
I tried configuring it for Alt+T then X but this clashed with Tools..Choose ToolboX Items.
Update
These days, I prefer to just hit Ctrl+Shift+Esc to bring up the process manager, then X to skip to XDesProc.exe then Delete to kill the rogue process(es).
The Visual Studio designer attempts to keep the rendered view in sync with the XAML. That's the advertised behavior.
The first thing you should check is that there are no errors in the errors window. It may be something as simple as a missing angle bracket.
The second thing to check is whether you have any code (other than your code-behind) which needs to be compiled for the designer to render your XAML correctly. This includes any of your own datatypes that you instantiate in XAML, any custom controls you have written (e.g. MyTextBlock derived from TextBlock), or any classes directly or indirectly in support of design-time data. If so, you need to rebuild your project.
The last thing to check for is possible bugs in the designer. In spite of the advertised behavior, the designer may get out-of-sync due to bugs. In that close, close the XAML window and re-open it. Other tricks that might work are selecting the XAML tab and then the Design tab, or maximizing the XAML pane.
As far as rebuilding your application goes, you don't need to do this as a habit. You only need to recompile it when the above conditions apply. Once they don't apply, you can just edit the XAML. Another way to say this is that if you haven't modified code, you shouldn't need to rebuild (modulo bugs).
I'm not sure, but I think a build will refresh your view in that situation.
There is any event handled in that XAML file, then mostly it will not display the design preview from Visual Studio. If you want to see the design from Visual Studio, try with Command Binding instead of event, you will see the preview.
I'm not sure how this works in WPF editing, but with ASP.NET pages when the design view wont update i can do 2 things
Exit Visual Studio and restart
Go into source view (not split), type something and remove it (not by undoing, just delete or backspare) and save it. Then return to design view, usually the view has been updated then.
When you add a new row of code or a new object, XAML designer is sync but I encountered non-sync behavior when a property of an object is changed.
A tricky way is that when you change a property you only need to remove a ">" character from end of an instruction then retype it.
On the toolbar in the XAML designer, choose the "Disable project code" button to reload the designer link which stays on the right side of "Turn on snapping to snaplines".
Disable project code in the designer
If it is disabled, you can try to check the configuration manager and change processors to "Any CPU".
For projects that target ARM or X64 processors, Visual Studio cannot run project code in the designer, so the Disable project code button is disabled in the designer. Check this:
Debug or disable project code in XAML Designer
For information, I had the same issue with the XAML Designer of Visual Studio Community 2017, i.e. sometimes the designer doesn't show anything, the easiest solution is then to close the XAML file and reopen it.
I also frequently get the exception "An Unhandled Exception has occurred - Click here to reload the designer - Details: The XAML Designer has exited unexpectedly" (the click restarts the designer successfully).
Note that, in this VS version, the process of the XAML designer is not named XDesProc.exe, but UwpSurface.exe. If you prefer or have to kill the process, then the designer shows the same exception as above, and you may restart it.
use process hacker and kill the WpfSurface process (blend only)
Update for designer refresh/reload Visual Studio 2022 Xamarin, taskkill /IM java.exe

Resources