intro: I'm trying to create a custom control for use within autocad through autocad .net API.
I've created the basic design of the UC, and decided to group some elements of it in a custom UC and call it later to the main UC. (basically for code management, organisation... leaving me with a code structure like this:
UC(top)-consisting of few custom child UCs each of those Child UCs consisting of few buttons, and combo boxes.
simple so far...
in those child UCs, I call on a simple ACAD method/object.
the problem appears when I try adding one of those Child UCs to the main UC. I get a "CLR error" (image appended).
https://s32.postimg.org/6gnt3sgz9/why_no_work.jpg
I've tried few methods of solving the problem; 1) Using the Custom Controls instead of UC 2) tried to clear solution, rebuild solution. 3) tried creating a separate Class that calls on ACAD methods from outside the UC code
basically, what I've learned is, that I can (somehow) get the program to work if I run the code from within user created events (such as button click,...), but if I try calling acad command from within UC constructor, or UC_Load event method, I end up getting the CLR error when ever I try adding the childUC (through VSdesigner, and if I add it by code, I end up raising the error whenver I try calling on the designer)
if nothing else, I'd like to know why the code behaves differently depending if the code is called from "UC_load" or "button_clicked".
here's my code:
public partial class child : UserControl
{
public child()
{
InitializeComponent();
//if I initialize doc here, I get the error
}
private void child_Load(object sender, EventArgs e)
{
//if I initialize doc here, I get the error
}
private void button1_Click(object sender, EventArgs e)
{
//it works here, but Im unsure why
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
}
}
"the Document doc =..... is the line that generates the error;
Here's an image of the error, and the solution. The error appears when I klik the child object to the designer, or before I open the designer after I've added the "child object" from the code.
using C# acad 2017, VS2015, x64