使用Interop.Access在Header部分(或正文或页脚)中获取控件

时间:2014-02-25 16:34:35

标签: c# ms-access ms-access-2007

您是否可以建议如何使用C#中的Microsoft.Office.Interop.Access来获取特定Access表单的标题部分的所有控件?

谢谢。

1 个答案:

答案 0 :(得分:0)

所有表单都有一个Detail部分,表格会包含其他部分,例如FormHeaderFormFooterPageHeaderSectionPageFooterSection等。存在于表格中。每个部分都有一个Controls集合。这是一个例子:

static void Main(string[] args)
{
    // this code requires the following COM reference in the project:
    // Microsoft Access 14.0 Object Library
    //
    var objAccess = new Microsoft.Office.Interop.Access.Application();
    objAccess.OpenCurrentDatabase(@"C:\Users\Public\Database1.accdb");

    string formName = "ClientForm";
    objAccess.DoCmd.OpenForm(formName, Microsoft.Office.Interop.Access.AcFormView.acDesign);
    Microsoft.Office.Interop.Access.Form frm = objAccess.Forms[formName];

    Console.WriteLine(String.Format("The FormHeader section of form [{0}] contains the following controls:", formName));
    foreach (Microsoft.Office.Interop.Access.Control ctl in frm.Section["FormHeader"].Controls)
    {
        Console.WriteLine();
        Console.WriteLine(String.Format("    [{0}]", ctl.Name));
        Console.WriteLine(String.Format("        {0}", ctl.GetType()));
    }
    objAccess.DoCmd.Close(Microsoft.Office.Interop.Access.AcObjectType.acForm, formName);
    objAccess.CloseCurrentDatabase();
    objAccess.Quit();

    Console.WriteLine();
    Console.WriteLine("Done.");
    Console.ReadKey();
}

控制台输出:

The FormHeader section of form [ClientForm] contains the following controls:

    [Auto_Logo0]
        Microsoft.Office.Interop.Access.ImageClass

    [Auto_Header0]
        Microsoft.Office.Interop.Access.LabelClass

Done.