ShowDialog

S

Steve

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
 
A

Armin Zingler

Steve said:
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?
 
H

Herfried K. Wagner [MVP]

* "Steve said:
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!).
 
S

Steve

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
 
A

Armin Zingler

Steve said:
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.
 
G

Guest

"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

.

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
 
A

Armin Zingler

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top