使用 InfoPath 2003 物件模型初始化和清除程式碼
根據預設,如果您為與 InfoPath 2003 相容的表單範本專案建立 FormCode.cs 或 FormCode.vb 檔案,則檔案會包含表單程式設計邏輯的所有來源程式碼。專案的範本會在 FormCode.cs 或 FormCode.vb 檔案中產生類別,與下列範例中的類別非常類似,因為在以下的範例中,您可以定義初始化和清除程式碼,以及表單事件的處理常式。FormCode.cs 與 FormCode.vb 檔案會套用組件層級的 System.ComponentModel.DescriptionAttribute 屬性,此屬性會將類別識別為實作事件處理常式的唯一類別。InfoPathNamespace 屬性 (由 InfoPathNamespaceAttribute 型別實作) 會套用至類別以識別類別內使用的 XML DOM 選取命名空間。在 InfoPathNamespace 中參照的命名空間則由 InfoPath 專案系統所維護。
在表單開啟時,FormCode 類別本身還提供 _Startup
和 _Shutdown
方法,以便用來為標準 InfoPath 功能以外的任何所需元件,執行初始化和清除常式。
重要
請勿在 _Startup
和 _Shutdown
方法內呼叫 InfoPath 物件模型的成員。在這些方法中,您應該只能初始化和呼叫外部元件的成員。
using System;
using Microsoft.Office.Interop.InfoPath.SemiTrust;
// Office integration attribute. Identifies the startup class for the form. Do not
// modify.
[assembly: System.ComponentModel.DescriptionAttribute(
"InfoPathStartupClass, Version=1.0, Class=Template1.FormCode")]
namespace Template1
{
// The namespace prefixes defined in this attribute must remain synchronized with
// those in the form definition file (.xsf).
[InfoPathNamespace(
"xmlns:my='https://schemas.microsoft.com/office/infopath/2003/myXSD/2004-03-29T22-27-27'")]
public partial class FormCode
{
private XDocument thisXDocument;
private Application thisApplication;
public void _Startup(Application app, XDocument doc)
{
thisXDocument = doc;
thisApplication = app;
// You can add additional initialization code here.
}
public void _Shutdown()
{
}
}
}
Imports System
Imports Microsoft.Office.Interop.InfoPath.SemiTrust
Imports Microsoft.VisualBasic
' Office integration attribute. Identifies the startup class for the form. Do not
' modify.
<Assembly: System.ComponentModel.DescriptionAttribute( _
"InfoPathStartupClass, Version=1.0, Class=Template1.FormCode")>
Namespace Template1
' The namespace prefixes defined in this attribute must remain synchronized with
' those in the form definition file (.xsf).
<InfoPathNamespace( _
"xmlns:my='https://schemas.microsoft.com/office/infopath/2003/myXSD/2004-03-29T22-36-40'")> _
Public Class FormCode
Private thisXDocument As XDocument
Private thisApplication As Application
Public Sub _Startup(app As Application, doc As XDocument)
thisXDocument = doc
thisApplication = app
' You can add additional initialization code here.
End Sub
Public Sub _Shutdown()
End Sub
End Class
End Namespace
_Startup 方法
除了提供一個位置寫入其他元件的初始化程式碼,_Startup
方法也會初始化 thisXDocument
和 thisApplication
變數。您可以在表單程式碼中使用這些變數來存取 InfoPath 物件模型中 XDocument 和 Application 類別的成員。初始化兩個變數的必要程式碼,會由 Visual Studio 專案範本自動產生。
private XDocument thisXDocument;
private Application thisApplication;
public void _Startup(Application app, XDocument doc)
{
thisXDocument = doc;
thisApplication = app;
// You can add additional initialization code here.
}
Private thisXDocument As XDocument
Private thisApplication As Application
Public Sub _Startup(app As Application, doc As XDocument)
thisXDocument = doc
thisApplication = app
' You can add additional initialization code here.
End Sub
下列範例會示範按鈕的簡單事件處理常式,該按鈕使用 thisXDocument
變數來存取 UIObject 型別的 Alert 方法。
[InfoPathEventHandler(MatchPath="CTRL1_5", EventType=InfoPathEventType.OnClick)]
public void CTRL1_5_OnClick(DocActionEvent e)
{
// Write your code here.
thisXDocument.UI.Alert("Hello!");
}
<InfoPathEventHandler(MatchPath:="CTRL1_5", EventType:=InfoPathEventType.OnClick)> _
Public Sub CTRL1_5_OnClick(ByVal e As DocActionEvent)
' Write your code here.
thisXDocument.UI.Alert("Hello!")
End Sub
如需如何建立事件處理常式的詳細資訊,請參閱操作方法:使用 InfoPath 2003 物件模型新增事件處理常式。
_ShutDown 方法
在關閉表單時,_Shutdown
方法會是最後呼叫的方法。您可以在需要此方法以清除或完成表單中所使用的元件時,在此方法中撰寫任何程式碼。
public void _Shutdown()
{
}
Public Sub _Shutdown()
End Sub
初始化和清除程式碼範例
下列程式碼示範如何在 _Startup
方法中初始化 Microsoft SQL Server™ 資料庫的連線,並在 _Shutdown
方法中關閉連線。為了使此範例能正確運作,您必須先按一下 [專案] 功能表中的 [加入參考],然後選取 [.NET] 索引標籤中的 System.Data.dll 元件,以設定 .NET Framework 的 System.Data 組件參照。另外請注意,在表單程式碼檔案的頂端已加入 using System.Data.SqlClient
(Imports System.Data.SqlClient)
) 指示詞以減少按鍵次數。
注意
根據表單的部署方式與安全性原則的定義方式而定,包含連至 SQL Server 資料庫之表單程式碼的 InfoPath 表單使用者,可能需要各種安全性權限。如需安全性的詳細資訊,請參閱關於 Managed 程式碼表單範本的安全性模型以及操作方法:設定 Managed 程式碼表單範本的安全性設定。
using System;
using System.Data.SqlClient;
using Microsoft.Office.Interop.InfoPath.SemiTrust;
// Office integration attribute. Identifies the startup class for the form. Do not
// modify.
[assembly: System.ComponentModel.DescriptionAttribute(
"InfoPathStartupClass, Version=1.0, Class=Template1.FormCode")]
namespace Template1
{
// The namespace prefixes defined in this attribute must remain synchronized with
// those in the form definition file (.xsf).
[InfoPathNamespace(
"xmlns:my='https://schemas.microsoft.com/office/infopath/2003/myXSD/2004-03-05T20-56-13'")]
public partial class Template1
{
private XDocument thisXDocument;
private Application thisApplication;
private SqlConnection sqlConnect;
public void _Startup(Application app, XDocument doc)
{
thisXDocument = doc;
thisApplication = app;
// Initialize variable for SQL Server connection.
sqlConnect= new SqlConnection("server=localhost;Trusted_Connection=yes;database=Northwind");
}
public void _Shutdown()
{
// Close SQL Server connection at shut down.
sqlConnect.Close();
}
}
}
Imports System
Imports System.Data.SqlClient
Imports Microsoft.Office.Interop.InfoPath.SemiTrust
Imports Microsoft.VisualBasic
' Office integration attribute. Identifies the startup class for the form. Do not
' modify.
<Assembly: System.ComponentModel.DescriptionAttribute( _
"InfoPathStartupClass, Version=1.0, Class=Template1.FormCode")>
Namespace Template1
' The namespace prefixes defined in this attribute must remain synchronized with
' those in the form definition file (.xsf).
<InfoPathNamespace( _
"xmlns:my='https://schemas.microsoft.com/office/infopath/2003/myXSD/2004-03-08T18-47-33'")> _
Public Class Template1
Private thisXDocument As XDocument
Private thisApplication As Application
Private sqlConnect As SqlConnection
Public Sub _Startup(app As Application, doc As XDocument)
thisXDocument = doc
thisApplication = app
' Initialize variable for SQL Server connection.
sqlConnect = New SqlConnection _("server=localhost;Trusted_Connection=yes;database=Northwind")
End Sub
Public Sub _Shutdown()
' Close SQL Server connection.
sqlConnect.Close()
End Sub
End Class
End Namespace