StrataFrame Forum

ChildFormDialog error

http://forum.strataframe.net/Topic4078.aspx

By Andria Jensen - 11/2/2006

I have a ChildFormDialog control on a form, and have set up the ChildForm and BO Translations for it.  The child form is coming from a dll and is an inherited form.  In my code I call ChildFormBank.ShowDialog() and get the attached error message every time.

I have included the stack trace for more info.


11/2/2006 9:35:32 AM
Source: mscorlib
Message: Value cannot be null.
Parameter name: type
Stack:    at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(Type type, Object[] args)
   at MicroFour.StrataFrame.UI.Windows.Forms.ChildFormDialog.CreateForm(Object[] Arguments)
   at MicroFour.StrataFrame.UI.Windows.Forms.ChildFormDialog.ShowDialog(Object[] Arguments)
   at MicroFour.StrataFrame.UI.Windows.Forms.ChildFormDialog.ShowDialog()
   at BBS.GUI.frmClientInfo.MaintBankRel_AfterButtonClick() in C:\FactorSoftV3\GUI\ClientInfo\frmClientInfo.vb:line 205
   at BBS.GUI.BaseControls.MaintStrip.button_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at DevExpress.XtraEditors.BaseButton.OnClick(EventArgs e)
   at DevExpress.XtraEditors.BaseButton.OnMouseUp(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at DevExpress.Utils.Controls.ControlBase.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


 

By Trent L. Taylor - 11/2/2006

There's nothing magic done here...we just call an Activator.CreateInstance and pass the type that you specify for the child dialog form.  Once thing that tends to cause an error is when the ChildForm is expecting a parameter in the New and you do not provide that parm in the ShowDialog:

MyChildDialogControl.ShowDialog(YourParms)

By Andria Jensen - 11/2/2006

I understand what you're saying, but what doesn't make sense to me here is that I can instantiate the form which I am using as the child and call a show on it, without using the ChildFormDialog control and it shows fine.  There must be something that acts differently when using this control.  Maybe something to do with the way the translations?  I'm just guessing here...not sure how to go about debugging this.
By Trent L. Taylor - 11/2/2006

First see if you can just call Activator.CreateInstance on it.

Dim loForm As System.WIndows.Forms.Form

loForm = CType(Activator.CreateInstance(GetType(MyFormType)), System.WIndows.Forms.Form)

loForm.SHowDialog()

By Andria Jensen - 11/2/2006

OK, I sort of figured it out.  Not sure what this means and pretty sure it's not supposed to work this way.  I have a local form which I set as the ChildForm, and this works fine...no problems.  This works without creating an instance of the local form.  When I try to set the ChildForm to a form coming from a referenced DLL it gives me the error.  However, if I create an instance of the form (with any name) it works fine.  So basically if it's a form from the same project no instance is needed, but if it's coming from a referenced dll then you must create an instance first.

This code works for forms in same project:

ChildFormDialog1.ShowDialog()

This code works for forms from dlls where ChildFormDialog1 has ChildForm=frmChildForm:

Dim ChildFormDLL as New frmChildForm
ChildFormDialog1.ShowDialog

By Trent L. Taylor - 11/2/2006

Actually, you can use a referenced form from a DLL.  But the problem may be that the full name (i.e. YourNameSpace.YourForm) may not be valid through the reference that is being specified.  You may need to load the assembly.  For example, open your designer file and go to the ChildForm property in the designer file.  You will see the name of the form like something I mentioned above.  See if your activator.CreateInstance works with the full name rather than the type:

Activator.CreateInstance("YourNameSpace.YourFormName")

By Andria Jensen - 11/2/2006

I checked the designer code and it looks like it is already using the fully qualified name for the form, including the namespace BBS.GUI.  This is the code from the designer:

BusinessObjectTranslationItem3.DestinationBusinessObject = "frmCliBroker.CliBrokerBO"
BusinessObjectTranslationItem3.SourceBusinessObject = "frmClientInfo.CliBrokerBO"
Me.ChildFormBrokerRel.BusinessObjectTranslations.AddRange(New MicroFour.StrataFrame.UI.Windows.Forms.BusinessObjectTranslationItem() {BusinessObjectTranslationItem3})
Me.ChildFormBrokerRel.ChildForm = "BBS.GUI.frmCliBroker"
Me.ChildFormBrokerRel.ParentForm = Me

By Trent L. Taylor - 11/2/2006

Does the fully qualified name work in a test scenario when you manually call the Activator.CreateInstance()?
By Chan - 8/26/2007

Hi,

I am facing the same problem.



I tried the code below.





//Workign OK

Payment.UI.Windows.Forms.PaymentForm loForm = new PaymentForm(new object[]

{ JK.Payment.Enums.TxnType.Sale, salesBO.SaleNo, salesBO.Total });



loForm.ShowDialog();



loForm.Dispose();



//Workign OK

loForm = (PaymentForm)Activator.CreateInstance(Type.GetType("JK.Payment.UI.Windows.Forms.PaymentForm"));



loForm.ShowDialog();



loForm.Dispose();



//NOT Workign OK

loForm = (PaymentForm) Activator.CreateInstance(Type.GetType("JK.Payment.UI.Windows.Forms.PaymentForm"),

new object[]

{

JK.Payment.Enums.TxnType.Sale, salesBO.SaleNo,

salesBO.Total

});



loForm.ShowDialog();

loForm.Dispose();





Any advice? Thank you
By StrataFrame Team - 8/27/2007

What sort of error do you get from the second call to Activator.CreateInstance (the one where you pass the starting parameters)?  Is is a MissingMethodException?  Or a TypeLoadException?  Or something else?
By Chan - 8/27/2007

Hi,

I hit error



Constructor on type 'JK.Payment.UI.Windows.Forms.PaymentForm' not found.



However, as the code I posted, it works if I use the code below



//Workign OK



Payment.UI.Windows.Forms.PaymentForm loForm = new PaymentForm(new object[]



{ JK.Payment.Enums.TxnType.Sale, salesBO.SaleNo, salesBO.Total });
By StrataFrame Team - 8/28/2007

Aha, in the code that you posted, you're expecting one parameter that it an object[].  When you call the Activator.CreateInstance, it's excpecting you to pass all of the parameters in an object[], so, you have to put your object[] into the object[] that the CreateInstance method is excpecting.  Like this:

loForm = (PaymentForm) Activator.CreateInstance(Type.GetType("JK.Payment.UI.Windows.Forms.PaymentForm"),
    new object[] {
    new object[] { JK.Payment.Enums.TxnType.Sale, salesBO.SaleNo, salesBO.Total } } );

The object[] that the CreateInstance is expecting is an array of all of the parameters, so the one that you create only needs to contain one item.  But, since that one item is itself an array of object, you'll have to put your parameter object[] inside another object[] to pass to the CreateInstance() method.

By StrataFrame Team - 8/28/2007

If you didn't want to pass all of your parameters as an object[], then you'll want to change your constructor so that it does not expect an object, but just 3 regular parameters.  Then, you can remove the outer object from the CreateInstance() call.
By Chan - 8/28/2007

Ben Chase (08/28/2007)
If you didn't want to pass all of your parameters as an object[],




Hi,

Thank you tip.

I will try it out later.



then you'll want to change your constructor so that it does not expect an object, but just 3 regular parameters. Then, you can remove the outer object from the CreateInstance() call.




I don't think ChildFormDialog support this, doesn it?



Thank you
By StrataFrame Team - 8/29/2007

Yep, the ChildFormDialog is designed to work like that.  If your form is like this:

public class Form1 : System.Windows.Forms.Form
{
    public Form1(string param1, string param2, string param3) { }
}

Then you're call to childFormDialog.ShowDialog() will look like this:

childFormDialog.ShowDialog("param1", "param2", "param3");

The signature of the method is like this:

public DialogResult ShowDialog(params object[] parameters) {}

The keyword being the "params" at the beginning.  It means that you can add as many parameters as you want and separate them with commas and the compiler will pass them all as one big object array.  It's smart like that Smile