本文介绍了MS Access中的.Net用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在 Interrop工具包,或作为简单的 ActiveX .

It is possible to create .Net UserControls that can be consumed on a VB6/MS Access form through COM, with the help of the Interrop toolkit, or as a simple ActiveX.

除了主要的痛苦外,此方法效果很好: 调整大小 .

This works pretty well except for one major pain: resizing.

您无法在运行时调整窗体上控件的大小.
将控件锚定在窗体的相对两侧,即使您缩小窗体,每次调整窗体大小时,控件也会不断增大.

You cannot resize the control on the form during runtime.
Anchoring the control to opposite sides of the form makes it grow every time you resize the form, even if you reduce the form...

似乎没有任何方法可以驯服这种行为:

There doesn't seem to be any way to tame this behaviour:

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

显然,一种解决方案可能是将.Net用户控件包装在VB6用户控件中.不幸的是,除了不得不使用另一个包装程序和更多临时代码之外,VB6 IDE不再可用...

Apparently, one solution may be to wrap the .Net Usercontrol in a VB6 usercontrol.Unfortunately, beside the hell of having to use yet another wrapper and more ad-hoc code, the VB6 IDE isn't available any longer...

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

Is there any way to solve this issue?

推荐答案

我不确定为什么MS Access和VB6的行为会有所不同,但是我发现了使用.Net Interop和C#的有效解决方案.有了VB.Net,它也应该可以正常工作.

I'm not sure why MS Access and VB6 behaves differently, but I found a working solution using .Net Interop with C#. With VB.Net it should work as well.

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

  1. Extend the IInteropUserControl interface with the following entry:

void ResizeThis(int width,int height);

void ResizeThis(int width, int height);

实现ResizeThis功能:

Implement the ResizeThis function:

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);
}

  • 在MS Access中,使用适当的宽度和高度参数调用ResizeThis函数.

  • In MS Access call the ResizeThis function with the appropriate width and height parameters.

    还有另一种奇怪的行为.在Access窗体中的每个记录移动上,UserControl都会缩小一些像素.我已经通过重写SetBoundsCore函数并实现了属性LockResizing解决了此问题.当不应该调整UserControl的大小时,此属性将分配为true.

    There is another strange behaviour. On each record movement in the Access form the UserControl shrinks by some pixels. I have solved this issue by overriding the SetBoundsCore function and implementing a property LockResizing. This property will assigned true, when the UserControl should not be resized.

    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; }
    }
    

  • UserControl已通过MS Access 2010和2016(Office 365)进行了测试.

    The UserControl was tested with MS Access 2010 and 2016 (Office 365).

    这篇关于MS Access中的.Net用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-22 07:58