Displaying a form from a class library: Help needed!

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

Guest

Hello,

I built a very basic form (System.Windows.Forms.Form) in a project that I
compiled as a class library. Everything worked fine.

I then created another project and I want to display that form in it.
Problem is that when I create an instance of the form and then try to set it
as being visible, the form is blank. There is no controls on it, nothing.
It looks like the creation process is not done. It looks frozen. I think I
might have to create the form and have it run on another thread but I'm not
sure and before going into these kinds of modifications, I wanted to be sure
that that was my problem.

The code that instantiates the form is very simple:

class RunTest
{
[STAThread]
static void Main(string[] args)
{
MyFormNamespace.MyForm o = new MyFormNamespace.MyForm();

Console.WriteLine(o.Test());

Console.ReadLine();
}
}

The only thing needed is a reference to the project (or dll assembly)
containing the source. The 'Test()' method returns a string and is only used
for debugging purposes.

Any ideas? Do I need to create/run the form's process on a seperate thread?
How can I do this easily?

Thanks a lot in advance,

Skip
 
Skip,

You can't do that. In order to show a form, at least the first form,
you need to set up a windows message loop. This is handled by the static
Run method on the Application class.

You need to do this:

class RunTest
{
[STAThread]
static void Main(string[] args)
{
MyFormNamespace.MyForm o = new MyFormNamespace.MyForm();

// Run the application, this call will block until the form is
closed.
Application.Run(o);

// This call might fail, if the Test method relies on the control
state, and not state stored in the object.
Console.WriteLine(o.Test());

// This line won't do anything if this is compiled into a winexe.
Console.ReadLine();
}
}

Hope this helps.
 
Back
Top