473,414 Members | 1,757 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,414 software developers and data experts.

ShowDialog

Hi All,

I'm having a problem displaying a form as a modal dialog
window, when I call the code below from one part of my
program (a right-click menu) it works fine, but when I
call it from within another Sub, the new form displays
but I can click back to the main form so the dialog
window no longer has the focus.

Here is the code I am using, does anyone know why this
would happen?

Dim fAbout As frmAbout = New frmAbout()
fAbout.ShowDialog()
fAbout.Dispose()

Thanks,
Steve
Nov 20 '05 #1
6 2658
"Steve" <ms**@zuna.co.uk> schrieb
I'm having a problem displaying a form as a modal dialog
window, when I call the code below from one part of my
program (a right-click menu) it works fine, but when I
call it from within another Sub, the new form displays
but I can click back to the main form so the dialog
window no longer has the focus.

Here is the code I am using, does anyone know why this
would happen?

Dim fAbout As frmAbout = New frmAbout()
fAbout.ShowDialog()
fAbout.Dispose()


You should call Close instead of Dispose. Otherwise events like Closing or
Closed don't fire. Dispose is automatically called as an reaction on the
Form being closed.

Back to your problem: I can't reproduce it. Do you show the modal form in a
different thread?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
* "Steve" <ms**@zuna.co.uk> scripsit:
I'm having a problem displaying a form as a modal dialog
window, when I call the code below from one part of my
program (a right-click menu) it works fine, but when I
call it from within another Sub, the new form displays
but I can click back to the main form so the dialog
window no longer has the focus.

Here is the code I am using, does anyone know why this
would happen?

Dim fAbout As frmAbout = New frmAbout()
fAbout.ShowDialog()
fAbout.Dispose()


Dispose doesn't make sense here. The form will be disposed
automatically. What exactly do you mayn by "within another Sub"? Are
you showing the form in a different thread (that's not recommended!).

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

The dialog is displayed incorrectly from a subroutine
that is envoked as the result of a barcode scanner
sending data to a serial port, which I guess could be on
a different thread to the main program?

Is there a way of getting the main form thread to display
this dialog window instead?

Steve
-----Original Message-----
* "Steve" <ms**@zuna.co.uk> scripsit:
I'm having a problem displaying a form as a modal dialog window, when I call the code below from one part of my
program (a right-click menu) it works fine, but when I
call it from within another Sub, the new form displays
but I can click back to the main form so the dialog
window no longer has the focus.

Here is the code I am using, does anyone know why this
would happen?

Dim fAbout As frmAbout = New frmAbout()
fAbout.ShowDialog()
fAbout.Dispose()
Dispose doesn't make sense here. The form will be

disposedautomatically. What exactly do you mayn by "within another Sub"? Areyou showing the form in a different thread (that's not recommended!).
--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
.

Nov 20 '05 #4
"Steve" <an*******@discussions.microsoft.com> schrieb

The dialog is displayed incorrectly from a subroutine
that is envoked as the result of a barcode scanner
sending data to a serial port, which I guess could be on
a different thread to the main program?

Is there a way of getting the main form thread to display
this dialog window instead?

"I guess could be on a different thread".. I'd say you should confirm this
first. I assume this is the only reason why the app behaves like it does, so
you should marshal showing the modal Form to the main UI thread in order to
make it modal for the whole app (assuming there should only be one UI
thread). You can show it in the main thread by calling a procedure using the
Invoke or BeginInvoke methods of any control (inclduing Form) created in the
main thread. You can pass a Delegate pointing to the procedure to be called
to the Invoke/BeginInvoke methods. The procedure will be executed in the
other thread. The MethodInvoker Delegate might be useful in this context.
There you can modally show the Form. But first, as I said, you should make
sure that it is currently really shown in a new thread, e.g. by having a
look at the thread windows.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #5
"I guess could be on a different thread".. I'd say you should confirm thisfirst. I assume this is the only reason why the app behaves like it does, soyou should marshal showing the modal Form to the main UI thread in order tomake it modal for the whole app (assuming there should only be one UIthread). You can show it in the main thread by calling a procedure using theInvoke or BeginInvoke methods of any control (inclduing Form) created in themain thread. You can pass a Delegate pointing to the procedure to be calledto the Invoke/BeginInvoke methods. The procedure will be executed in theother thread. The MethodInvoker Delegate might be useful in this context.There you can modally show the Form. But first, as I said, you should makesure that it is currently really shown in a new thread, e.g. by having alook at the thread windows.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

.


Thanks Armin, I got it working from you reply.

The window obviously was being displayed in a different
thread to the main UI, so I just added the following
delegate to the form:

Private Delegate Sub DisplayDialog(ByVal sMessage As
String)

And then I could call this with BeginInvoke:

Dim myArgs(0) As String
myArgs(0) = "The carton could not be found in the data
file"
Dim ar1 As IAsyncResult = BeginInvoke(New DisplayDialog
(AddressOf DisplayMessage), myArgs)

Steve
Nov 20 '05 #6
<an*******@discussions.microsoft.com> schrieb
Thanks Armin, I got it working from you reply.

The window obviously was being displayed in a different
thread to the main UI, so I just added the following
delegate to the form:

Private Delegate Sub DisplayDialog(ByVal sMessage As
String)

And then I could call this with BeginInvoke:

Dim myArgs(0) As String
myArgs(0) = "The carton could not be found in the data
file"
Dim ar1 As IAsyncResult = BeginInvoke(New DisplayDialog
(AddressOf DisplayMessage), myArgs)

Thx for your feedabck. Code looks good. Glad I could help you.
--
Armin

Nov 20 '05 #7

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

Similar topics

3
by: Richard L Rosenheim | last post by:
I would like to detect when a form is invoked as the result of a ShowDialog call. Anyone have any ideas or suggestions on how to do that? TIA, Richard Rosenheim
2
by: Bill Burris | last post by:
When you crate a Windows application the wizard adds a line like: Application.Run( new MyMainForm() ); Out of curiosity I replaced this with: MyMainForm myForm = new MyMainForm();...
5
by: Josh Golden | last post by:
3 forms. form 1, when button clicked instantiates form 2 which opens but is not shown. form 2 instantiates form 3 as showdialog. form 3 finishes, raises an event that form 2 catches. during the...
4
by: Dennis Sjogren | last post by:
Greetings! First, I'm not 100% sure where to post this question. I use VB.NET for this project, but it's really a design question (a question on which method to use when solving this problem). ...
6
by: Samuel R. Neff | last post by:
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....
1
by: No Sheds | last post by:
Hi I have an MDI application. I have a MDI child window within this application that shows another form using ShowDialog. This works fine, except that the final form shown with ShowDialog...
4
by: trialproduct2004 | last post by:
Hi all I am new to vb.net application. I am using showdialog property of form to display form. First time it is working properly. But next time it is giving me error that ' object reference not...
8
by: WvH | last post by:
Hi, When I create a new application, with just one button, then this code works as expected: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
2
by: **Developer** | last post by:
I have a little more info about this problem I can't seem to solve! I know it'll take a minute to study the lising but if you like to sove puzzles there is one there. I have a Form (FV&C)...
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...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...

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.