MS Access 中的.Net 用户控制

Ren*_*uis 5 .net ms-access user-controls interop

可以在Interrop 工具包的帮助下创建可通过 COM 在 VB6/MS Access 表单上使用的 .Net UserControls ,或者作为简单的ActiveX

除了一个主要的问题:调整大小之外,这种方法效果很好。

您无法在运行时调整窗体上的控件的大小。
将控件锚定到窗体的相对两侧,使其在每次调整窗体大小时都会增大,即使缩小窗体也是如此...

似乎没有任何方法可以抑制这种行为:

  • 在 .Net 中,任何通过代码调整 UserControl 大小的尝试都会失败。
  • 从 MS Access 中,用户控件也不能通过代码调整大小。

显然,一种解决方案可能是将.Net Usercontrol 包装在 VB6 usercontrol 中。不幸的是,除了必须使用另一个包装器和更多临时代码之外,VB6 IDE 不再可用......

有什么办法可以解决这个问题吗?

Ste*_*nst 3

我不确定为什么 MS Access 和 VB6 的行为不同,但我找到了使用 .Net Interop 和 C# 的可行解决方案。使用 VB.Net 应该也能工作。

  1. 使用以下条目扩展 IInteropUserControl 接口:

    void ResizeThis(int 宽度, int 高度);

  2. 实现ResizeThis函数:

    public void ResizeThis(int width, int height)
    {
        this.UpdateBounds(Left, Top, width, height);
        this.SetBounds(0, 0, width + 1, height + 1, BoundsSpecified.Width | BoundsSpecified.Height);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在 MS Access 中,使用适当的宽度和高度参数调用 ResizeThis 函数。

  4. 还有另一个奇怪的行为。每次在 Access 表单中进行记录移动时,UserControl 都会缩小一些像素。我通过重写 SetBoundsCore 函数并实现属性 LockResizing 解决了这个问题。当 UserControl 不应调整大小时,此属性将指定为 true。

    protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
    {
        if (_LockResizing)
            throw new Exception();
    
        base.SetBoundsCore(x, y, width, height, specified);
    }
    
    public bool LockResizing
    {
        get { return _LockResizing; }
        set { _LockResizing = value; }
    }
    
    Run Code Online (Sandbox Code Playgroud)

UserControl 使用 MS Access 2010 和 2016 (Office 365) 进行了测试。