pKulandrei

Hello everyone,

I'm working on a Word 2007 add-in and is using VS 2005 (C#). The add-in contains windows forms that are displayed whenever a button was clicked on the ribbon. I'm loading the forms using the below line of code.


myForm.ShowDialog();


When a command button was clicked on the currently loaded windows form, it will unload itself and load a new form. Below is the lines of code I'm using.


cmdButton_Clicked(/*function parameters here*/)
{
/*** other lines of codes exist here ***/


this.Hide(); //Hide the currently loaded form
showMyOtherForm(); //Function to show my other form
}

void ShowMyOtherForm()
{
/*** other lines of codes exist here ***/

myOtherForm.ShowDialog();
}

The code executes without error, the currently loaded form is hiding and myOtherForm is also loading but it's not modal. I want to to show myOtherForm to be modal and it's parent is MS Word. Please let me know how to accomplish this.

Thanks in advance!



Re: Visual Studio Tools for Office Word 2007 Add-in -- Modal Forms

Phil Hoff - MSFT

Rather than hide the first form and attempt to show the second form modally from the first (now hidden) form, try this:

1. In your first form's button click handler:

1(a) Set a publically accessible flag to true (or some value indicating that the second form should be shown).

1(b) Close the first form.

2. In your Ribbon button event handler:

2(a) Show the first form.

2(b) Check the flag on the first form.

2(c) If the flag is true, create and show the second form.

The following example assumes that button1_Click is attached to the "starting" button (i.e. your Ribbon button, though I used a custom task pane to prototype this as it was quicker) and that there are two form classes, Form1 and Form2.

Code Snippet

private void button1_Click(object sender, EventArgs e)

{

Form1 form = new Form1();

form.ShowDialog();

if (form.ShowForm2)

{

Form2 form2 = new Form2();

form2.ShowDialog();

}

}

public partial class Form1 : Form

{

public bool ShowForm2;

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

ShowForm2 = true;

Close();

}

}

If you need to switch between the two forms (e.g. Form1 -> Form2 -> Form1), instead of using a flag you could have both forms derive from a base MyCustomForm class which exposes a property NextForm. The NextForm value would be set by each form to the next form to be displayed (or null, if no more forms should be displayed). The Ribbon button click handler would create the first form, then loop while NextForm was non-null, calling ShowDialog() on NextForm.

-Phil





Re: Visual Studio Tools for Office Word 2007 Add-in -- Modal Forms

pKulandrei

Thanks Phil!



Re: Visual Studio Tools for Office Word 2007 Add-in -- Modal Forms

Tadwick

Phil,

I have a similar but simpler scenario. I have a single form launched by a toolbar button that I want to limit to a single instance. I used an application setting to store a flag for 'FormIsLoaded' and I set the flag on when the form is first loaded and switch it off when the form closes or the application shuts down. However, I am not sure how to activate the form when the toolbar button is clicked because I get an error that the name 'fm' does not exist in the current context presumably because the compiler thinks it hasn't been instantiated.

What should I do Tx, Tad

Code Snippet

private void ButtonClick(Office.CommandBarButton ctrl, ref bool cancel)

{

if (Properties.Settings.Default.FormIsLoaded)

{

fm.Activate();// produces error : name 'fm' does not exist in the current context

}

else

{

Properties.Settings.Default.FormIsLoaded = true;

Form1 fm = new Form1();

fm.Show();

}

}





Re: Visual Studio Tools for Office Word 2007 Add-in -- Modal Forms

Tadwick

oops, I didn't declare the form properly - Tad