问题
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:
- From .Net, any attempt at resizing the UserControl through code fails.
- From MS Access, the user control is not resizeable through code either.
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?
回答1:
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.
Extend the IInteropUserControl interface with the following entry:
void ResizeThis(int width, int height);
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); }
In MS Access call the ResizeThis function with the appropriate width and height parameters.
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; } }
The UserControl was tested with MS Access 2010 and 2016 (Office 365).
回答2:
Solving this issue is more complex than anticipated. Every time you thing you're holding a solution, it slips through your hands...
A simple solution is documented in the VB MSDN Forums : Interop UserControl in MSAccess.
Not perfect, but simpler than the one I found.
The main issue is that Access erases an area that is larger that the control.
If the control is anchored on the right and bottom edges, that is not an issue, otherwise, you can use the control in a subform or in a tab control page without showing the tabs so it acts as a container.
回答3:
Old Topic i know but i have an idea/work around that seems to be working right now.
Don't know Why....
With in .net com control
Private _ForceWidth As Integer = 0
Private _ForceHeight As Integer = 0
Public Property ForceWidth As Integer
Get
Return _ForceWidth
End Get
Set(value As Integer)
_ForceWidth = value
End Set
End Property
Public Property ForceHeight As Integer
Get
Return _ForceHeight
End Get
Set(value As Integer)
_ForceHeight = value
End Set
End Property
Private Sub MyControl_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
If ForceWidth > 0 Then
Me.Width = ForceWidth
End If
If ForceHeight > 0 Then
Me.Height = ForceHeight
End If
End Sub
In Acces Form
Private Sub Form_Current()
MyControl1.ForceWidth = 800
MyControl1.ForceHeight = 600
MyControl1.Width = SignatureControl6.Width - 1 'force sending resize message
End Sub
Still in testing, but looks like it might work
来源:https://stackoverflow.com/questions/4365583/net-usercontrol-in-ms-access