.Show() on Form object.

  • Thread starter Thread starter MV
  • Start date Start date
M

MV

Why can't I run .Show() on a System.Windows.Forms object
from a Class? The code works fine if i run it from an
other form.

The form gets visible but no controls are loaded and the
mouse pointer is an hourglass.

..ShowDialog() works fine.
 
Did you use the NEW keyword?
Tom
--
==========================================
= Tom Vande Stouwe MCSD.net, MCAD.net, MCP
= 45Wallstreet.com (www.45wallstreet.com)
= (803)-345-5001
==========================================
= If you are not making any mistakes
..= ..you are not trying hard enough.
==========================================
 
Hello,

MV said:
Why can't I run .Show() on a System.Windows.Forms object
from a Class? The code works fine if i run it from an
other form.

The form gets visible but no controls are loaded and the
mouse pointer is an hourglass.

.ShowDialog() works fine.

Windows version?
Framework version?

Are you sure, the application has a message loop? How do you show the first
form?

Post some code.

Regards,
Herfried K. Wagner
 
This is the case:

My .NET application has a form as Startup Object.

My .NET application opens a Access .adp file and sends an
object to it so it can communicate with my .NET
application.

The Access file calls a sub in the .NET application to
open a form.

If the sub tries to open the form with .Show() it doesn't
work.

My resolution is to open the form with .ShowDialog() in a
thread. This works fine but why cant I just use .Show()?

Example code to open the form with a thread:

'***Open form as a thread
Public sub subOpenForm(ByVal oFormToOpen As
System.Windows.Forms)
Dim oOpenForm As clsOpenForm
Dim tOpenForm As Thread

oOpenForm = New clsOpenForm(oFormToOpen)

tOpenForm = New Thread(AddressOf
oOpenForm.subOpenForm)
tOpenForm.Start()
End Function

Private Class clsOpenForm
Private m_oFormToOpen As System.Windows.Forms =
Nothing

Public Sub New(ByRef oFormToOpen As
System.Windows.Forms)
m_oFormToOpen = oFormToOpen
End Sub

Public Sub subOpenForm()
m_oFormToOpen.ShowDialog()
End Sub
End Class
 
MV said:
This is the case:

My .NET application has a form as Startup Object.

My .NET application opens a Access .adp file and sends an
object to it so it can communicate with my .NET
application.

The Access file calls a sub in the .NET application to
open a form.

If the sub tries to open the form with .Show() it doesn't
work.

My resolution is to open the form with .ShowDialog() in a
thread. This works fine but why cant I just use .Show()?

Becaue Show does not wait until the form is closed, consequently the sub
continues and the end of the thread's main procedure is reached => end of
thread => death of form
 
Hello,

MV said:
This is the case:

My .NET application has a form as Startup Object.

My .NET application opens a Access .adp file and sends an
object to it so it can communicate with my .NET
application.

The Access file calls a sub in the .NET application to
open a form.

If the sub tries to open the form with .Show() it doesn't
work.

My resolution is to open the form with .ShowDialog() in a
thread. This works fine but why cant I just use .Show()?

Example code to open the form with a thread:

There are no commands to execute after calling Show, so the application
quits. Have a look at Application.Run and Application.ExitThread:

http://groups.google.de/groups?selm=u#[email protected]

HTH,
Herfried K. Wagner
 
-----Original Message-----
Hello,



There are no commands to execute after calling Show, so the application
quits. Have a look at Application.Run and Application.ExitThread:
40TK2MSFTNGP11.phx.gbl

HTH,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet


.

....but the application is already running and a main form
is open.

The problem occurs when i try to open a new form from
Access.

This is the scenario:

*Application starts and open the main form.
*The application start Access and open a ADP-file.
*The application create an instance of my adp-facade
object (registered as a COM-component so Access can use
it).
*The application sends a reference to the adp-facade to
Access.
*Access run a sub in the adb-facade to open a form in
the .NET application. Here is the problem! The form does
not get painted and does not get activated properly.

If I run the same code in the adp-facade from the .NET
application it works fine.
 
MV said:
...but i have declared the form object in the scope of the
class. Why should the form die when the sub that runs show
ends? The object with the reference to the form is still
alive.


A thread is the owner of a window. When the thread dies, all windows created
in the thread must be destroyed. It wouldn't make sense to keep the window
alive because only the thread creating the window is allowed to access it.
The form does not die. It just dont get painted and
activated correctly.

Examplecode:

Public Class Class1
Private oForm As Form2
Sub testShowForm()
oForm = New Form2
oForm.Show()
End Sub
End Class


You might still have a reference to the managed Form object, but the
associated window has been destroyed.

So, you have to show the Form modally, or call Application.Run(TheForm)
 
In addition:
Each thread has it's own message queue (if it becomes a UI thread). The
messages sent to a window (mouse clicks, key presses, ...) are put in the
message queue of the thread that created the window. Consequently, a window
can not exist anymore when it's owner thread is destroyed. Application.Run
and Form.Showdialog contain message loops that process this message queue
for the current thread. If there's no message loop, no messages are
processed.
 
Back
Top