Make a form active?

H

hayrob

When the user clicks a particular menu item on Word, I
instantiate a form and display it using ShowDialog. The
form is there, but it is not visible since Word remains
on top. How can I ensure the user sees the form?

Ta

Hayrob
 
G

Greg Ellison [MS]

Hello Hayrob,

Thanks for your question. Can you specify which version of Word? Also, I
assume this is a .NET form since you mention the ShowDialog method. Please
specify which version of .NET and which language. Also, is this a .NET
class library or standard Windows application? Do you have a code snippet
that shows us how you reproduce the problem? What does the VBA code look
like that call this method?

Best regards,
Greg Ellison
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? Please visit the Microsoft Security & Privacy Center
(http://www.microsoft.com/security) for the latest news on security updates.
 
H

hayrob

Greg,

Word2003
Yes, it is a .Net form, and I am using VS2003

This is a code snippet - this is in a Menu Button handler
in the OfficeCodeBehind class:
ThisApplication.Visible = False
Dim frm As New ConfirmFinalSaveForm
If frm.ShowDialog = DialogResult.Yes Then
ThisDocument.Save()
Try
FileItemOwner.SetLetterStatusFinal
(mFileItemOwner, mLetter)
Catch ex As Exception
ThisApplication.Visible = True
Throw New ApplicationException("Not able
to access database", ex.InnerException)
End Try
End If
ThisApplication.Visible = True

I also have the save problem with standard .Net dialogs,
eg SaveFileDialog.

Any suggestions?

Thanks

Hayrob
 
G

Greg Ellison [MS]

Hello Hayrob,

Thanks for the update. It appears you are using the Visual Studio Tools for
Office. I have a few additional questions to better understand the problem:

1. Are you creating a code-behind Word document, or is this a code-behind
Word template?
2. Do you actually see the Word application window? Or, is the Word
application window invisible?
3. Do you actually see the form behind the Word window? Or, is both Word
and the form invisible?
4. What happens if you remove the thisApplication.Visible = False? Does
the same problem occur?

While waiting for your reply, I will attempt to create a new code behind
Word document to see if I can recreate the same problem on my computer.

For information and sample code for integrating Office with Visual Basic,
Visual C++, Internet Scripts, and other programming languages, please see
http://msdn.microsoft.com/library/techart/VSOfficeDev.htm. This site
contains the most up-to-date information for using developer tools for
Office integration and extensibility.

Best regards,
Greg Ellison
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? Please visit the Microsoft Security & Privacy Center
(http://www.microsoft.com/security) for the latest news on security updates.

--------------------
 
G

Greg Ellison [MS]

Hello Hayrob,

I created a code-behind Word document using the Visual Studio Tools for
Office. My system is running Windows XP, Office 2003, and Visual Studio
..NET 2003. My code adds a new menu item under Word's Tools menu, and on
the click event of that CommandBarButton I have code to call ShowDialog. I
am not seeing any problem with displaying the form in my test. The form
appears as a modal dialog in Word, in front of the visible Word window as
expected. Below is the code snippet from my OfficeCodeBehind class:

Public Class OfficeCodeBehind

Friend WithEvents ThisDocument As Word.Document
Friend WithEvents ThisApplication As Word.Application
Private WithEvents CBarTestButton As Office.CommandBarButton

' Called when the document is opened.
Private Sub ThisDocument_Open() Handles ThisDocument.Open
SetupCommandButtons()
End Sub

Private Function SetupCommandButtons() As Boolean
Dim CBarTools As Office.CommandBarPopup
CBarTools = ThisApplication.CommandBars("Menu
Bar").Controls("Tools")
Try
CBarTestButton = CBarTools.Controls("Test Button")
Catch ex As Exception
' If the Test button is not there, then add the Test button:
CBarTestButton = CType(CBarTools.Controls.Add( _
Office.MsoControlType.msoControlButton),
Office.CommandBarButton)
CBarTestButton.Style = Office.MsoButtonStyle.msoButtonCaption
CBarTestButton.Caption = "Test Button"
CBarTestButton.Tag = "tgTestButton"
End Try
End Function

Private Sub CBarTestButton_Click(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
Handles CBarTestButton.Click
Dim f As New Form1
f.ShowDialog()
Windows.Forms.MessageBox.Show("You closed the dialog!")
End Sub
End Class

In this case, Form1 is a simple Windows form that has a Textbox control and
CommandButton. When I close the form, I get my message box "You closed
the dialog" to verify that the form was shown modal. Perhaps you can do
the same test above, in a new code-behind Word document like I did and see
if the same problem occurs. Also, I look forward to your answers to the 4
questions below (from my previous post.)

Best regards,
Greg Ellison
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? Please visit the Microsoft Security & Privacy Center
(http://www.microsoft.com/security) for the latest news on security updates.

--------------------
 
H

hayrob

Greg

Thanks again for your response.

1. Code behind Word Document
2 and 3. The Word application window is there; the
problem is that the form I create is not modal!
4. I inserted the thisApplication.Visible = False so that
the form became visible; otherwise it was hidden by the
application window. I have a top level form which
instatiates the Word application. When my program runs,
this form is on top of the form I create in Word.

In answer to your later mail, yes, I have tried what you
suggest, and it works fine. My program is reasonably
complex (and long!), so I cannot easily isolate the
problem. Whatever I am doing, it clearly causes the form
I create to become non modal. I hoped there was a simple
way(?) to force the behaviour I require, even if I'm
doing something to prevent it!

Thanks again.

Hayrob
-----Original Message-----
Hello Hayrob,

Thanks for the update. It appears you are using the Visual Studio Tools for
Office. I have a few additional questions to better understand the problem:

1. Are you creating a code-behind Word document, or is this a code-behind
Word template?
2. Do you actually see the Word application window? Or, is the Word
application window invisible?
3. Do you actually see the form behind the Word window? Or, is both Word
and the form invisible?
4. What happens if you remove the
thisApplication.Visible = False? Does
 
G

Greg Ellison [MS]

Hello Hayrob,

Thanks for providing the additional information about your issue. Here are
some additional troubleshooting steps:

1. Try creating a new blank form and show it as a test, instead of showing
your ConfirmFinalSaveForm form. The code for your menu button handler would
simply be:

Dim frm As New Form1
frm.Show

2. Same as #1 above, but try ShowDialog instead of Show.

Question: Do either of the above show the form on top of the Word window,
or does the form still show behind the Word window?

3. You wrote "I have a top level form which instantiates the Word
application. When my program runs, this form is on top of the form I create
in Word."

As a test, what happens if you just manually start Word by clicking Start,
Programs, Microsoft Office, Microsoft Office Word 2003. Then click File,
Open in Word to open your managed document (.doc) file? When you then
click the menu button handler to run the simple code above to show Form1,
does the form show on top of the Word window or does it still show behind
the Word window?

4. If the above steps do not lead to determining a more specific
resolution, you may want to try using the Activate method of the form to
see if that will force focus to it. To start with, I suggest you use a
Timer in the form to wait a few seconds after the form is shown, then the
Tick event will call the Activate method of the form. Here are the steps
to try this in the Form1:

a. Using the Toolbox, add a Timer control to Form1.
b. Double-click the Timer1 control added to Form1. The Timer1_Tick event
procedure is created.
c. Use the following code for the Form_Load and the Timer1_Tick:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Timer1.Interval = 3000 '3 second interval
Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
Me.Activate()
End Sub

d. Now rebuild and run. Once Form1 is shown (by the menu button handler)
wait about 3 seconds and the Tick event will try to force the form into
focus via calling Activate.

The reason for the timer is to rule out any specific Form event for calling
Activate. The idea is to make sure calling Activate works when the form is
fully shown and ready. If calling Activate works in this case, then for a
more permanent solution you can try calling Activate from some other event,
like the Form Load event.

For information and sample code for integrating Office with Visual Basic,
Visual C++, Internet Scripts, and other programming languages, please see
http://msdn.microsoft.com/library/techart/VSOfficeDev.htm. This site
contains the most up-to-date information for using developer tools for
Office integration and extensibility.

Best regards,
Greg Ellison
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? Please visit the Microsoft Security & Privacy Center
(http://www.microsoft.com/security) for the latest news on security updates.
--------------------
 
H

hayrob

Greg

Thanks for your suggestions. I've been away for a couple
of days, but I removed all the extraneous stuff and
simply used frm.Activate in frm Load event. I now get the
behaviour I want, but I'm not quite sure why! I need to
revisit basic Windows stuff. I remember reading an
article which explained about the process of loading
forms and the sequence of events. Can't remember where.
Don't suppose you can point me in the right direction.

Thanks again for solving my problem.

Hayrob.
 
G

Greg Ellison [MS]

Hello Hayrob,

I'm glad you found success using the Activate method! Thanks for letting
us know.

For more information about form events, take a look at the following MSDN
links:

http://msdn.microsoft.com/library/en-us/vbcon/html/vbconWindowsFormsArchitec
ture.asp
http://msdn.microsoft.com/library/en-us/vbcon/html/vbconEventHandling.asp

Best regards,
Greg Ellison
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? Please visit the Microsoft Security & Privacy Center
(http://www.microsoft.com/security) for the latest news on security updates.

--------------------
 

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