Launcher doesnt!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I have a solution that has 2 windows forms projects: one a very simplistic
form, and the other a stub to do nothing more than launch the first's form.
I got the vb code and concept from
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet10142001.asp

Ive converted it to c#, and when I step it through the debugger, each line
looks good, returning data that looks valid; the catch never fires unless I
intentionally mess things up, like garble the path to the dll.

HOWEVER - the frmToShow.Show() seems to do nothing. The form doesnt
display, and the constructor doesnt seem to fire.
Help!
Thanks, Mark

[STAThread]
static void Main
{
try
{
// Application.Run(new Form1());

string asmLoadedPath=String.Empty;
asmLoadedPath=@"C:\sConsoleUI.dll";

Assembly asm;
Type asmFormType;
Object frmInstance;
Form frmToShow;

// access the assembly with the Form
asm = Assembly.LoadFrom(asmLoadedPath);
// pull the Form from the assembly
asmFormType=asm.GetType("ConsoleUI.frmMain");
// create an instance of the Form

frmInstance=Activator.CreateInstance(asmFormType);
// cast the instance to a Form
frmToShow=(Form)frmInstance;
// display the form
frmToShow.Show();
}
catch (Exception ex)
{
string errMsg=ex.Message;
MessageBox.Show(errMsg);
}
}
 
The problem may be the

frmToShow.Show()

This is not modal so the form shows and then the application ends,
closing the form.

Try using

Application.Run(frmToShow)

that worked for me.

The rest of the code worked fine though it seems over-engineered at
times (this may have been done deliberately for debugging /
illustrative purposes)

e.g.

string asmLoadedPath=String.Empty;
asmLoadedPath=@"C:\sConsoleUI.dll";

could be replaced by

string asmLoadedPath=@"C:\sConsoleUI.dll";


and there is not real need for frmInstance, you can cast/assign
directly to the form object

frmToShow =
(Form)Activator.CreateInstance(asm.GetType("ConsoleUI.frmMain"));
);


hth,
Alan.
 
Back
Top