共用方式為


Access a form region at run time

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Applies to
The information in this topic applies only to the following project types and versions of Microsoft Office. For more information, see Features available by Office application and project type.

Project type

- VSTO Add-in projects

Microsoft Office version

- Outlook 2010

Use the Globals class to access form regions from anywhere within your Outlook project. For more information about the Globals class, see Global access to objects in Office projects.

Applies to: The information in this topic applies to VSTO Add-in projects for Outlook. For more information, see Features available by Office application and project type.

Access form regions that appear in a specific Outlook Inspector window

To access all form regions that appear in a specific Outlook Inspector, call the FormRegions property of the Globals class and pass in an Inspector object that represents the Inspector.

The following example gets the collection of form regions that appear in the Inspector that currently has focus. This example then accesses a form region in the collection named formRegion1 and sets the text that appears in a text box to Hello World.

Private Sub Access_Form_Regions_ByInspector()
    Dim formRegions As WindowFormRegionCollection = Globals.FormRegions _
        (Globals.ThisAddIn.Application.ActiveInspector())
    formRegions.FormRegion1.textBox1.Text = "Hello World"
End Sub
private void Access_Form_Regions_ByInspector()
{
    WindowFormRegionCollection formRegions = 
        Globals.FormRegions
            [Globals.ThisAddIn.Application.ActiveInspector()];
    formRegions.FormRegion1.textBox1.Text = "Hello World";
}

Access form regions that appear in a specific Outlook Explorer window

To access all form regions that appear in a specific Outlook Explorer, call the FormRegions property of the Globals class and pass in an Explorer object that represents the Explorer.

The following example gets the collection of form regions that appear in the Explorer that currently has focus. This example then accesses a form region in the collection named formRegion1 and sets the text that appears in a text box to Hello World.

Private Sub Access_Form_Regions_ByExplorer()
    Dim formRegions As WindowFormRegionCollection = Globals.FormRegions _
        (Globals.ThisAddIn.Application.ActiveExplorer())
    formRegions.FormRegion1.textBox1.Text = "Hello World"
End Sub
private void Access_Form_Regions_ByExplorer()
{
    WindowFormRegionCollection formRegions =
        Globals.FormRegions
            [Globals.ThisAddIn.Application.ActiveExplorer()];
    formRegions.FormRegion1.textBox1.Text = "Hello World";
}

Access all form regions

To access all form regions that appear in all Explorers and all Inspectors, call the FormRegions property of the Globals class.

The following example gets the collection of form regions that appear in all Explorers and all Inspectors. This example then accesses a form region named formRegion1 and sets the text that appears in a text box to Hello World.

Friend Sub Access_All_Form_Regions()
    Dim formRegion As Microsoft.Office.Tools.Outlook.IFormRegion
    For Each formRegion In Globals.FormRegions
        If TypeOf formRegion Is FormRegion1 Then
            Dim formRegion1 As FormRegion1 = _
                CType(formRegion, FormRegion1)
            formRegion1.textBox1.Text = "Hello World"
        End If
    Next formRegion
End Sub
internal void Access_All_Form_Regions()
{
    foreach (Microsoft.Office.Tools.Outlook.IFormRegion formRegion 
        in Globals.FormRegions)
    {
        if (formRegion is FormRegion1)
        {
            FormRegion1 formRegion1 = (FormRegion1)formRegion;
            formRegion1.textBox1.Text = "Hello World";
        }
    }

}

Access controls on a form region

To access controls on a form region by using the Globals class, you must make the controls accessible to code outside of the form region code file.

Form regions designed in the form region designer

For C#, change the modifier of each control that you want to access. To do this, select each control in the form region designer and change the Modifiers property to Internal or public in the Properties window. For example, if you change the Modifier property of textBox1 to Internal, you can access textBox1 by typing Globals.FormRegions.FormRegion1.textBox1.

For Visual Basic, you do not need to change the modifier.

Imported form regions

When you import a form region that was designed in Outlook, the access modifier of each control on the form region becomes private. Because you cannot use the form region designer to modify an imported form region, there is no way to change the modifier of a control in the Properties window.

To enable access to a control from outside the form region code file, create a property in the form region code file to return that control.

For more information about how to create properties in C#, see How to: Declare and use read write properties (C# programming guide).

For more information about how to create properties in Visual Basic, see How to: Create a property (Visual Basic).

See also