WinForms Bug? Why my UserControl.Load is being called when I drag and drop the Control on a Form? - winforms

this problem is annoying me and I took too much time to get it.
I'm very new to WinForms, so, maybe I'm missing something.
Whenever I drag'n'drop my UserControl to the form, it's Load event is being called (not the constructor).
it is very easy to reproduce.
Edit: I'm using .net core 3.1
1 - Create a new Project
2 - Create a new UserControl
3 - Add a Load Event and force an exception inside this method
4 - Build and try to add the control to the mainForm, the exception will raise.
The method is being called even with my project stoped.
For now, I just comment the method, build and add the Control to my forms, then I uncomment again and evertything works normally. There is no exception in Runtime and I can see the control in the desing. The only problem is to drag it to the form.

Related

Best Starting Point for WPF Revit Add in

I am wanting to rewrite one of my Revit Add ins so that it utilizes WPF with MVVM because I like the look and functionality of WPF better than Windows Forms.
I have used the Revit Template Wizzard from Jeremy Tammik for the Form based add in, but adding a WPF user control seems not to work (a run time error that the xaml resource cannot be found).
I found a WPF MVVM revit add in example (AddMaterials, here is the github link, which will add materials from an Excel spreadsheet) but it does not follow what I am expecting to see at the top level.
Revit Add ins have an app.cs file which tells Revit how to register and access the DLL (ribbon panel buttons etc).
A windows WPF app will have app.xaml as the top level entry point.
The Add Materials project has neither which tells me that it must be
a class library, however the views are not using UserControls
rather they are Windows which I prefer. However Visual Studio does not
let you add a Window for a Class Library type project.
The third issue is easily solved by simply copying windows from a WPF application project into a class library project. But I don't really understand how the class library will instantiate in Revit without following the app.cs code from the template. Is anyone else creating add-ins this way, and if so can you let me in on any tricks or discussions that will help? Has anyone created a WPF Revit addin template for Visual Studio?
When I add a WPF window and try to instantiate it I get an error that it cannot find the xaml resource (System.IO.IOException: Cannot locate resource 'xxxx.xaml'). I have tried to fix this according to advice found when googling for this error, but to no avail. I am thinking it comes from being in a form based project, and that I may have to just start with a new project without the form stuff.
I have now verified that indeed you can start with the Revit AddIn Wizzard and use WPF . . . I started from scratch and copied in a window created in another project and got it to run (after adding the various references, namespaces, etc). So my problem seems to just be with the original project which already had a bunch of form stuff added.
Yes, I'm using WPF to create Revit Addins. It works well. You can easily create your own WPF template from the SDK samples:
Start with one of the Autodesk-provided SDK samples. I used the "DockableDialogs" sample. I know this one works, your mileage may vary with the others. If you're looking for windows rather than docked panes in the UI, another sample (perhaps the AddMaterials sample) is probably simpler.
I used Visual Studio to turn the sample into a template. File - Export Template -> select "DockableDialogs" or other WPF sample project.
Create a new project based on the template you just created. This was the easiest method I could find to get the WPF internal bits wired up correctly.
I'm not specifically familiar with the AddMaterials project, but to clarify your bullet points.
Revit Addins - It's not the file name (app.cs) but rather they must extend IExternalApplication or IExternalCommand. If you are creating a xaml interface (rather than just running a command from a ribbon button) you'll use 'IExternalApplication' as your entry point. Look for something like this in the sample:
public class ThisApplication : IExternalApplication ...
I don't used a top level app.xaml, but instead have page.xaml pages which are called by the Revit app. In my case these are Pages rather than Windows, which extend the IDockablePaneProvider class. These must be registered with the application which can then can be show, hide, etc your Panes. I imagine this is simpler with Windows, but haven't done it myself. For the dockable panes, your xaml.cs should start out something like:
public partial class MainPage : Page, Autodesk.Revit.UI.IDockablePaneProvider ...
Yes, the project is a class library in the sense that it is a collection of classes, at least one of which extends IExternalApplication or IExternalCommand. Remember that you're not creating a standalone application, but adding functionality to an existing Windows application (Revit). Revit will instantiate the ThisApplication class and then call its .OnStartup() method when the Revit application starts. This shouldn't stop you from adding .xaml or .cs files to the project, though. I can do it using VS Community 2015 using Ctrl-Shift-A.
Hopefully this gets you started - I've been able to implement a WPF UI in Revit without any prior WPF experience, and I'm not even a real programmer, so it's definitely possible. Good Luck!
addendum
If you want to add WPF elements to an existing revit addin, you can follow the instructions here: How can I connect xaml and xaml.cs files
Ultimately I found it easier to migrate my addin code into a template made from a working sample, you may want to try this approach as well.

Changing Controls' look and feel in WinForms without checking out the form in the source control

I have a winform with a lot many UI components in it. At the moment, the form is checked-in source control. The controls' look and feel is changed at the runtime and it is done in the OnLoad event of the form. The problem that I am encountering at the moment is that when form is getting loaded and controls' look and feel is getting changed, the form.cs is getting checked out automatically in source control. I am eager to know if there is another way of achieving the intended functionality i.e, form must not be checked out if controls' look and feel is changed on runtime.
its not at runtime this happens but when you open your forms in the "visual ???" designer of visual studio.
before changing any properties in a load or shown event check this first :
using System.ComponentModel;
...
if (!DesignMode && !(LicenseManager.UsageMode == LicenseUsageMode.Designtime))
{
// change runtime properties here
}
This ensures that this code is only executed at runtime, not at desingtime when opening forms in the visual designer

Error while loading designer in wpf form

My application is working fine, all of a sudden while loading a designer in wpf form, im getting errors.
Object reference not set to instance of an object.
As if it is in a loop. After pressing the enter button for sometime. Im getting this error.
Microsoft Visual Studio XAML UI Designer has stopped working.
And after this, im getting this.
System.Runtime.Remoting.RemotingException
[9980] Designer process terminated unexpectedly!
The number in square brackets is changing evrytime.
Im using Visual Studio 2012. Im not yet running the application, Im just switching to designer mode from code behind. If I compile and run the application, it runs fine.
Kindly help.
Edit:
Here are the three errors in single image. (I cant post morethan one link)
xamlerrors
There's an error either in the code-behind or DataContext (if you're using a ViewModel). After you fix that error, click on Click here to reload the designer. What's happening is that the designer is trying to load everything up during design time and since there's an error, it's unable to, as if the program was actually running. That null reference exception can be a bit misleading in terms of trying to figure out the cause, because it'll be thrown if you have an error in your code-behind or ViewModel.
Things to check:
Is your View referencing the correct ViewModel?
Is your code-behind portion of the View matching the View name? Some people copy their Views, but forget to change the Class name in the code-behind.
Do you have any errors in your ViewModel?
What about models? If they're loaded during design time and contain errors, this could cause the above exception.
I noticed that those kind of errors are almost everytime caused by
an error in the parameterless constructor of the view
an error in the constructor of the view model which is attached to the view
Keep in mind that when the designer loads, it calls the parameterless constructor of the view (there has to be one for the designer!). If there are "complex things" done there which can only performed correctly at runtime, there will likely be errors at design time.
The same is true for the constructor of the view model which is called from the view.
Check whether design mode is active
For example you should not load data from a repository in a constructor.
If you do it in the constructor, at least check whether the design mode is active or not like explained in this answer here about the GetIsInDesignMode attached property and only do the complex logic in the constructor if the design mode is not active.
Debugging the problem
You also have the possiblity to debug the problem. In order to do this, you have to open a second instance of Visual Studio and debug the designer process of your original Visual Studio instance from there.
The process is described in detail here: Debugging an Exception (only) occurring in design view

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.

Drag and drop between HTML 5 and Silverlight 4

Is it possible to drag an HTML 5 object, e.g. an <a draggable="true">, into a Silverlight 4 control and accept it? We've attempted to build a simple prototype using an HTML 5 example and a trivial Silverlight control but the cursor changes to the no-entry sign whenever we hover over the Silverlight control. We do, however, get drag entry events firing in Silverlight.
Our control happily accepts files dragged from the desktop as expected. We think we've got the HTML 5 drag events set up correctly, and I can possibly get our test published somewhere in case that will help. We've successfully implemented dragging inside a Silverlight control but we now need to integrate with non-Silverlight page components.
Is this actually possible to set up or are we just doing something wrong? Thanks for any advice!
Silverlight Drag drop from external sources currently supports only file lists.
You may however be able to get the <object> tag hosting the Silverlight plugin to respond to a dragging. If so you can use the Silveright HTML Bridge to handle these DOM events. Its a bit of a long shot though.
Short answer, as Anthony says is No.
You can emulate the functionality by using the
the drag entry event and mouse button up event, then create a silverlight control that most closely matches the HTML one you were dragging, and hide the original html control (which should snap back to its original position)...
Its a little messy, but it works...
Incidently, as far as i'm aware, dragging between 2 silverlight instances on the same page would require a similar technique...

Resources