473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Nested ShowDialog doesn't make the second dialog truely modal??


I'm having weird results with a form that is already displayed modally
(via ShowDialog) displaying a second form via ShowDialog. The last
form is not modal even though it's called with ShowDialog.

For example, given three forms:

Startup
Pop1
Pop2

and this scenario:

Sub Main
Application.Run(new Startup())
End Sub

Class Startup
Sub Whatever
Dim f as new Pop1
f.ShowDialog()
End Sub
End Class

Class Pop1
Sub Whatever
Dim f as new Pop2
f.ShowDialog()
End Sub
End Class
I would expect that when Pop2 is displayed via the second ShowDialog
call, Pop2 should become modal as related to Pop1, but that's not the
case. Both forms Pop1 and Pop2 are modal as related to Startup but
clicking on Startup activates Pop1 and Pop1 is fully interactive.

I can work around the problem by disabling Pop1 before showing Pop2,
but it seems odd that I have to do that. Is there something I'm
misunderstanding about how ShowDialog works? Is there another,
better, workaround?

Thanks,

Sam

Nov 21 '05 #1
6 4888
Hi

It seems that I can not reproduce the problem on my side.
Here is my reproduce code.
If I have any misunderstanding, please feel free to post here.

[startup class]
Public Class Class1
Public Shared Sub Main()
Application.Run(New Form3)
End Sub
End Class

[Form3]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fm As New Form4
fm.ShowDialog()
End Sub

[Form4]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fm As New Form ' We note it as A
fm.ShowDialog()
End Sub

If we did not close A, neither Form4 nor Form3 can not activated.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #2
Hi

It seems that I can not reproduce the problem on my side.
Here is my reproduce code.
If I have any misunderstanding, please feel free to post here.

[startup class]
Public Class Class1
Public Shared Sub Main()
Application.Run(New Form3)
End Sub
End Class

[Form3]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fm As New Form4
fm.ShowDialog()
End Sub

[Form4]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fm As New Form ' We note it as A
fm.ShowDialog()
End Sub

If we did not close A, neither Form4 nor Form3 can not activated.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #3
Peter,

Thank you for looking into this. I didn't realize it earlier but the
problem is related to threading. Here's the situation (and
reproducible code follows):

Form 1 does a show dialog which launces a threadpool operation which
broadcasts an event which form1 (yes, the original form) catches and
calls another showdialog.

I was told (and research didn't disprove, but couldn't confirm) that
the act of broadcasting an event also handled multi-threading and that
when you broadcast an event the call is made on the appropriate
thread, just like a Control.Invoke.

That appears to be the problem, 'cause if i change the event broadcast
to be a direct Control.Invoke, then the problem goes away. Of course
this isn't an option in the real application (too tight coupling) but
it can lead to a solution.

If there is another option or workaround or if there is something I'm
doing wrong, I'd appreciate the feedback. Also, if you or anyone has
links to documentation on how events are related to multi-threading
(to show my coworker that gave the apparently incorrect information),
that would be helpful.

Thanks,

Sam
Imports System.Threading
Imports System.Windows.Forms

Public Class ShowDialogTest

Public Shared Sub Main()
Application.Run(New Form1)
End Sub
End Class

Public Class Form1
Inherits Form

Private Shared _counter As Integer = 0

Public Sub New()
_counter += 1
Text = "Form " + _counter.ToString()
AddHandler TriggerNew, AddressOf Form1_TriggerNew
End Sub

Private Sub Form1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
ThreadPool.QueueUserWorkItem(AddressOf TriggerStart)
End Sub

Public Event TriggerNew As EventHandler

Private Sub TriggerStart(ByVal state As Object)
RaiseEvent TriggerNew(Me, EventArgs.Empty)
' if we change it to not use an event but use Invoke instead,
' then ShowDialog problem is fixed
'Invoke(New EventHandler(AddressOf Form1_TriggerNew))
End Sub

Private Sub Form1_TriggerNew(ByVal sender As Object, ByVal e As
EventArgs)
Dim f As New Form1
f.ShowDialog()
End Sub
End Class


On Thu, 27 Jan 2005 02:43:05 GMT, v-******@online.microsoft.com
("Peter Huang" [MSFT]) wrote:
Hi

It seems that I can not reproduce the problem on my side.
Here is my reproduce code.
If I have any misunderstanding, please feel free to post here.

[startup class]
Public Class Class1
Public Shared Sub Main()
Application.Run(New Form3)
End Sub
End Class

[Form3]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fm As New Form4
fm.ShowDialog()
End Sub

[Form4]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fm As New Form ' We note it as A
fm.ShowDialog()
End Sub

If we did not close A, neither Form4 nor Form3 can not activated.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


Nov 21 '05 #4
Peter,

Thank you for looking into this. I didn't realize it earlier but the
problem is related to threading. Here's the situation (and
reproducible code follows):

Form 1 does a show dialog which launces a threadpool operation which
broadcasts an event which form1 (yes, the original form) catches and
calls another showdialog.

I was told (and research didn't disprove, but couldn't confirm) that
the act of broadcasting an event also handled multi-threading and that
when you broadcast an event the call is made on the appropriate
thread, just like a Control.Invoke.

That appears to be the problem, 'cause if i change the event broadcast
to be a direct Control.Invoke, then the problem goes away. Of course
this isn't an option in the real application (too tight coupling) but
it can lead to a solution.

If there is another option or workaround or if there is something I'm
doing wrong, I'd appreciate the feedback. Also, if you or anyone has
links to documentation on how events are related to multi-threading
(to show my coworker that gave the apparently incorrect information),
that would be helpful.

Thanks,

Sam
Imports System.Threading
Imports System.Windows.Forms

Public Class ShowDialogTest

Public Shared Sub Main()
Application.Run(New Form1)
End Sub
End Class

Public Class Form1
Inherits Form

Private Shared _counter As Integer = 0

Public Sub New()
_counter += 1
Text = "Form " + _counter.ToString()
AddHandler TriggerNew, AddressOf Form1_TriggerNew
End Sub

Private Sub Form1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
ThreadPool.QueueUserWorkItem(AddressOf TriggerStart)
End Sub

Public Event TriggerNew As EventHandler

Private Sub TriggerStart(ByVal state As Object)
RaiseEvent TriggerNew(Me, EventArgs.Empty)
' if we change it to not use an event but use Invoke instead,
' then ShowDialog problem is fixed
'Invoke(New EventHandler(AddressOf Form1_TriggerNew))
End Sub

Private Sub Form1_TriggerNew(ByVal sender As Object, ByVal e As
EventArgs)
Dim f As New Form1
f.ShowDialog()
End Sub
End Class


On Thu, 27 Jan 2005 02:43:05 GMT, v-******@online.microsoft.com
("Peter Huang" [MSFT]) wrote:
Hi

It seems that I can not reproduce the problem on my side.
Here is my reproduce code.
If I have any misunderstanding, please feel free to post here.

[startup class]
Public Class Class1
Public Shared Sub Main()
Application.Run(New Form3)
End Sub
End Class

[Form3]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fm As New Form4
fm.ShowDialog()
End Sub

[Form4]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fm As New Form ' We note it as A
fm.ShowDialog()
End Sub

If we did not close A, neither Form4 nor Form3 can not activated.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


Nov 21 '05 #5
Hi

Based on my knowledge, Winform control including windows form is not
thread-safe, so in the multithread scenario, we will invoke the call on
the winform thread(i.e. the main thread that show the form) or we will get
unexpected problem. Event handle is kind of callback mechanism, we can
consider event as kind of delegate(function point), the event handler
function will be run on the caller thread. If the event handler function
have nothing to do with UI, that will be fine, but if it has something with
UI(e.g. change the form.text, then here may cause problem and that is why
we suggested call the control.invoke, so that the call will be called on
the thread where the control lie in, i.e. the main thread.

Here are three articles, you may have a look.

Safe, Simple Multithreading in Windows Forms, Part 1
http://msdn.microsoft.com/library/de...us/dnforms/htm
l/winforms06112002.asp

Safe, Simple Multithreading in Windows Forms, Part 2
http://msdn.microsoft.com/library/de...us/dnforms/htm
l/winforms08162002.asp

Safe, Simple Multithreading in Windows Forms, Part 3
http://msdn.microsoft.com/library/de...us/dnforms/htm
l/winforms01232003.asp

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #6
Hi

Based on my knowledge, Winform control including windows form is not
thread-safe, so in the multithread scenario, we will invoke the call on
the winform thread(i.e. the main thread that show the form) or we will get
unexpected problem. Event handle is kind of callback mechanism, we can
consider event as kind of delegate(function point), the event handler
function will be run on the caller thread. If the event handler function
have nothing to do with UI, that will be fine, but if it has something with
UI(e.g. change the form.text, then here may cause problem and that is why
we suggested call the control.invoke, so that the call will be called on
the thread where the control lie in, i.e. the main thread.

Here are three articles, you may have a look.

Safe, Simple Multithreading in Windows Forms, Part 1
http://msdn.microsoft.com/library/de...us/dnforms/htm
l/winforms06112002.asp

Safe, Simple Multithreading in Windows Forms, Part 2
http://msdn.microsoft.com/library/de...us/dnforms/htm
l/winforms08162002.asp

Safe, Simple Multithreading in Windows Forms, Part 3
http://msdn.microsoft.com/library/de...us/dnforms/htm
l/winforms01232003.asp

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: MrNobody | last post by:
I am using the no-arg ShowDialog() method hoping that the window would not be modal to any other window like the other ShowDialog(IWin32Window) method does, but when this opens it somehow becomes...
4
by: Paul Aspinall | last post by:
Can anyone advise how to display a form on top, and modal, without using ShowDialog?? Thanks
5
by: Mike | last post by:
Hi, I want to show form as a modal dialog and use such code: Dim frm_Zip As New frmZIP frm_Zip.ShowDialog() but instead of be as modal dialog, it is modeless - I can switch between forms. ...
11
by: osmarjunior | last post by:
I have a sequence of commands like this: Form1 frm = new Form1(); frm.LoadData(); Boolean confirm = (frm.ShowDialog() == DialogResult.OK); The LoadData method loads information from...
1
by: SammyBar | last post by:
Hi all, I'm having troubles with a Symbol 9000 device (Compact Framework v 1.1) when activating the barcode scanner from a window. The problem is related to the Activated event of the form which...
5
by: Miro | last post by:
I will try my best to ask this question correctly. I think in the end the code will make more sence of what I am trying to accomplish. I am just not sure of what to search for on the net. I...
2
by: NickP | last post by:
Hi there, I was wondering if there was any way to stop the hiding of a dialog from returning from the ShowDialog function? i.e. If you create your own dialog and put some code in that hides it...
1
by: Frank Rizzo | last post by:
I am not grokking the difference between Form.ShowDialog() and Form.ShowDialog(this). I have a form (parent form) that kicks off a modal dialog using Form.ShowDialog(). The modal dialog has a 3rd...
14
by: shark | last post by:
Hi, Does Form.ShowDialog() start new thread ? If yes how is solved cross-thread operations? Thx
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.