473,545 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
misunderstandin g about how ShowDialog works? Is there another,
better, workaround?

Thanks,

Sam

Nov 21 '05 #1
6 4896
Hi

It seems that I can not reproduce the problem on my side.
Here is my reproduce code.
If I have any misunderstandin g, 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim fm As New Form4
fm.ShowDialog()
End Sub

[Form4]
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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 misunderstandin g, 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim fm As New Form4
fm.ShowDialog()
End Sub

[Form4]
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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.Threadin g
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.ToStri ng()
AddHandler TriggerNew, AddressOf Form1_TriggerNe w
End Sub

Private Sub Form1_Click(ByV al sender As Object, ByVal e As
System.EventArg s) Handles MyBase.Click
ThreadPool.Queu eUserWorkItem(A ddressOf TriggerStart)
End Sub

Public Event TriggerNew As EventHandler

Private Sub TriggerStart(By Val 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(Ad dressOf Form1_TriggerNe w))
End Sub

Private Sub Form1_TriggerNe w(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.m icrosoft.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 misunderstandin g, 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(B yVal sender As System.Object, ByVal e As
System.EventAr gs) Handles Button1.Click
Dim fm As New Form4
fm.ShowDialog()
End Sub

[Form4]
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventAr gs) 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.Threadin g
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.ToStri ng()
AddHandler TriggerNew, AddressOf Form1_TriggerNe w
End Sub

Private Sub Form1_Click(ByV al sender As Object, ByVal e As
System.EventArg s) Handles MyBase.Click
ThreadPool.Queu eUserWorkItem(A ddressOf TriggerStart)
End Sub

Public Event TriggerNew As EventHandler

Private Sub TriggerStart(By Val 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(Ad dressOf Form1_TriggerNe w))
End Sub

Private Sub Form1_TriggerNe w(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.m icrosoft.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 misunderstandin g, 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(B yVal sender As System.Object, ByVal e As
System.EventAr gs) Handles Button1.Click
Dim fm As New Form4
fm.ShowDialog()
End Sub

[Form4]
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventAr gs) 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(functi on 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/winforms0611200 2.asp

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

Safe, Simple Multithreading in Windows Forms, Part 3
http://msdn.microsoft.com/library/de...us/dnforms/htm
l/winforms0123200 3.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(functi on 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/winforms0611200 2.asp

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

Safe, Simple Multithreading in Windows Forms, Part 3
http://msdn.microsoft.com/library/de...us/dnforms/htm
l/winforms0123200 3.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
3856
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 modal to my main window- even though I don't see how it could even be getting a reference to my main window. What I need is to open a new window...
4
9993
by: Paul Aspinall | last post by:
Can anyone advise how to display a form on top, and modal, without using ShowDialog?? Thanks
5
2536
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. Does anybody had the same problem? OS: Win2000 Prof
11
4190
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 database. The problem is, when I was debugging the application, it doesn't show the form. It just pass through the ShowDialog() method, and my flag confirm...
1
8057
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 carries the task of the initializing the scanner. It looks like this event is not called after the creation of one of the forms. Let me describe...
5
10873
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 have a form that has a button. ( this form is a child form of a parent form ( main form ). Anway...in this child form I have a button, and if clicked...
2
2114
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 temporarily during it's use, the ShowDialog function will return the second it is hidden, this is the same for ".Hide()" as well as sending...
1
2298
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 party control on it. The weird thing is that I can click on parent form's icon in the taskbar and it will bring up the parent form (obscuring the...
14
6775
by: shark | last post by:
Hi, Does Form.ShowDialog() start new thread ? If yes how is solved cross-thread operations? Thx
0
7468
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7808
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7423
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7757
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5329
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.