使用 Windows 窗体承载电子表单






3.40/5 (7投票s)
本文解释了 Windows 窗体如何托管电子表单。
引言
Windows 窗体应用程序可以有效地托管电子表单。 Microsoft Office InfoPath 是使用 Microsoft Office 2003 开发的,现在是 Microsoft Office System 的组成部分。 它的用户界面相对简单,表单设计器通常选择“带有标题的表格”布局,以便在该外部表格中包含表格。 这确定了外观。 然后,可以将控件拖放到表面上。 以下是一个非常基本的表单示例
添加的第一个部分是“带有标题的表格”。 在正下方,示例文本所在的位置,我放置了一个节对象。 当光标在该节上闪烁时,我回到布局并选择了“自定义表格”,我在其中插入了五列三行。 等等。 本文的目的是解释如何将电子表单加载到 Windows 窗体界面上。 我将假设读者已下载并可以使用 Visual Studio 2008 SP 1 的副本,并且用户下载 InfoPath 2007 的试用版。 这是非常值得花时间的。 我们将在示例中加载的表单是下载的状态报告表单。
应该清楚的是,该表单将嵌入到 Windows 窗体中,并且 Windows 窗体将托管 InfoPath 2007 应用程序。 要在 Windows 应用程序中托管 InfoPath 表单,您必须使用 InfoPath FormContent
控件。 因此,首先要启动 InfoPath 2007,然后使用其中一个模板或创建一个类似于上述的简单表单。 保存该表单并将其发布到网络位置,例如 *c:\temp\Status.xsn*。 完成发布向导并关闭 InfoPath。
现在,启动 Visual Studio 2008 并在 C# 中创建一个 Windows 窗体应用程序。 右键单击工具箱,然后选择“选择项”。 浏览到 *%program files\Microsoft Office\Office 12* 目录。 找到 *Microsoft.Office.InfoPath.FormControl.dll* 并双击它。 在“选择工具箱项”对话框中单击“确定”。 将 FormControl
控件拖到 Windows 窗体上。 在 FormControl
下方,拖放一个按钮,该按钮将调用“加载表单”,一个标签,我们将称之为“加载自”,以及标签下方的一个文本框控件。
现在最简单的部分就是摆脱按钮事件处理程序。 因此,我们双击按钮“加载表单”,然后插入事件处理程序
formControl1.NewFromFormTemplate(@"c:\temp\status.xsn");
现在,您将连接表单以接受来自嵌入式表单的 Submit 事件。 在部分类声明之后添加以下内容,以将接口添加到您的表单
Microsoft.Office.Interop.InfoPath.ISubmitToHostEventHandler
此接口声明紧跟在 Form 之后。 右键单击它,然后选择“实现接口”。 这是整个文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Xml;
using System.Xml.XPath;
using Microsoft.Office.InfoPath;
using System.Text;
using System.Windows.Forms;
namespace EmbeddedForm
{
public partial class Form1 : Form,
Microsoft.Office.Interop.InfoPath.ISubmitToHostEventHandler
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
formControl1.NewFromFormTemplate(@"c:\temp\status.xsn");
}
#region ISubmitToHostEventHandler Members
public int SubmitToHostEventHandler(object sender,
string adapterName, out string errorMessage)
{
XPathNavigator xnav= formControl1.XmlForm.MainDataSource.CreateNavigator();
xnav = xnav.SelectSingleNode("/my:myFields/my:SomeOtherValue",
formControl1.XmlForm.NamespaceManager);
textBox1.Text = xnav.InnerXml;
errorMessage = "";
return 1;
}
#endregion
private void Form1_Load(object sender, EventArgs e)
{
formControl1.SetSubmitToHostEventHandler(this);
}
}
}
请注意,我导入了其他命名空间,例如 XML
命名空间和 InfoPath
命名空间。 InfoPath 以 XML 为中心。 这是设计器窗体代码
namespace EmbeddedForm
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources
/// should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources =
new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.formControl1 = new Microsoft.Office.InfoPath.FormControl();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.formControl1)).BeginInit();
this.SuspendLayout();
//
// formControl1
//
this.formControl1.Enabled = true;
this.formControl1.Location = new System.Drawing.Point(12, 33);
this.formControl1.Name = "formControl1";
this.formControl1.OcxState = ((System.Windows.Forms.AxHost.State)
(resources.GetObject("formControl1.OcxState")));
this.formControl1.Size = new System.Drawing.Size(558, 434);
this.formControl1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 500);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Load Form";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(365, 485);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(57, 13);
this.label1.TabIndex = 2;
this.label1.Text = "Load From";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(321, 516);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(595, 548);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.formControl1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.formControl1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private Microsoft.Office.InfoPath.FormControl formControl1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
}
}
我们现在构建解决方案,然后键入 InfoPath 2007 表单的路径:@“*C:\temp\status.xsn*”。
下载 zip 文件时,将其提取到名为 Projects 的文件夹中。 如果您不使用“状态报告”表单,请记住更改源代码中的路径参数。 下载 InfoPath 2007 后,创建一个 temp 文件夹,并将其中一个示例表单放置在该 temp 子文件夹中。 InfoPath 表单文件具有 *.xsn* 文件扩展名。