dll class project - loading form instance

  • Thread starter Thread starter NachoShaw
  • Start date Start date
N

NachoShaw

Guest
Hey

Hoping someone could help me to understand the right way of setting up and loading an instance of a form from my dll class project.

Background. Its an addin for Inventor. We create a dll that gets loaded as inventor starts. A button in the menu is created and when clicked, it should load the main form in the addin. When the addin form is closed, its disposed until the next time its needed.

Up until today, my Project has been a stand alone and i never had any issues with forms or instances but seeing as for whatever reason, it will only run on my machines but not any of the office machines, ive had to rethink it and decided to change it to a class project so that i can load it into inventor as a dll addin instead.

My main form has a navigation frame that lets me select between 5 UserControls i have set up as sub forms (page frames). Each UserControl has a purpose like settings, Processing, Data Review etc. I load these into the navigation frame from the mainform. They do however need to access some of the mainforms properties and functions. for example: to navigate back to another page frame which is like

frmMain.NavFrame.Selected = frmMain.UsrProcessPage

The addin had a Class 'AddinGlobal' that gets called when loaded. I figured this is where i would create my from instance in readiness for being loaded. Something like

Public Mainform As frmMain

In the Menu Button, i added this

AddinGlobal.MainForm = New frmMain

But this isnt working. I also tried declaring it as new but that just failed the addin as we are not allowed to preload a form, it needs to be instantiated on demand. I then found online a method that looked a little complicated and required me to have 2 dll classes; the Inventor one and a single one not directly mounted to Inventor. This code was

Private Sub LoadForm_OnExecute(ByVal Context As NameValueMap)
Dim name As String = "frmMain"
Dim type As Type = Assembly.LoadFrom(MyProject.Application.Info.DirectoryPath & "\Addin.dll").GetType(name)
Dim args As Object() = New Object() { AddinGlobal.Inventor }
Dim objectValue As Form = DirectCast(RuntimeHelpers.GetObjectValue(Activator.CreateInstance(type, args)), Form)
objectValue.ShowDialog(New WindowWrapper(DirectCast(Globals.g_inventorApplication.MainFrameHWND, IntPtr)))
objectValue.Dispose
GC.Collect
End Sub

which appears to call the frmMain from the 2nd dll. This could work as i can instantiate the mainform without risk of it intreuding into Inventor

My questions;

When working with forms in a dll class, what is the best method to load / manage the forms? As i need to reference it before its actually instantiated, what is the method that would allow me to do that?


I know its probably a mundane question to some but its still in my learning scope



Thanks
















Im a self taught VB.Net guy who writes code for Autodesk Inventor. I may not know the terminology but i try so please be patient. Im not a kid so please dont treat me like one :)

Continue reading...
 
Back
Top