我有一个动态 MS Access 表单,它根据其他字段中的值隐藏/显示某些字段。 我目前正在使用 field.Visible 和 field.Height 来隐藏字段并缩小间隙。字段的可见性工作正常,但隐藏/显示字段下方的字段无法正确重新定位,并且当字段再次可见时,较低的字段会与之前隐藏的字段重叠。
我尝试过Me.Repaint和Me.Repaint函数,但没有效果。
我发现如果我手动在“表单视图”和“布局视图”之间切换,那么它会正确更新。
是否有一个我可以运行的函数来复制“表单视图”和“布局视图”的切换??
谢谢
请参阅我的 ModernBox,位于 GitHub:VBA.ModernBox。
在此代码部分中,我隐藏了那些未使用的按钮,并在底部随机排列可见按钮:
Private Sub SetButtonSequence()
' Arrange from one to four visible buttons and refresh their captions.
' Maximum count of enabled (visible) buttons including Help button.
Const MaxButtonCount As Long = 3 + 1
' First button index.
Const FirstButton As Long = 0
' Undefined result value for Help button and inactive buttons.
Const MsgBoxResultNone As Long = 0
Dim WindowWidth As Long
Dim WindowExpand As Long
Dim WindowExtend As Long
Dim ActiveButtonCount As Long
Dim HelpButtonCount As ButtonCount
Dim ButtonIndex As Long
Dim LineCount As Integer
' Fill array of localized captions.
Call FillCaptions
' Fill array of button positions.
Call FillPositions
If HelpButton = vbMsgBoxHelpButton Then
' The Help button shall be displayed.
HelpButtonCount = ButtonCount.Help
End If
' Set captions and return values on active (visible) buttons.
Select Case ButtonSequence
Case vbAbortRetryIgnore
ActiveButtonCount = ButtonCount.AbortRetryIgnore
Buttons(FirstButton) = Array(True, Captions(ButtonCaption.ButtonAbort), VbMsgBoxResult.vbAbort)
Buttons(FirstButton + 1) = Array(True, Captions(ButtonCaption.ButtonRetry), VbMsgBoxResult.vbRetry)
Buttons(FirstButton + 2) = Array(True, Captions(ButtonCaption.ButtonIgnore), VbMsgBoxResult.vbIgnore)
NoCancel = True
Case vbOKCancel
ActiveButtonCount = ButtonCount.OKCancel
Buttons(FirstButton) = Array(True, Captions(ButtonCaption.ButtonOK), VbMsgBoxResult.vbOK)
Buttons(FirstButton + 1) = Array(True, Captions(ButtonCaption.ButtonCancel), VbMsgBoxResult.vbCancel)
Case vbOkOnly
' Note: Any click action (except Help) will result in Cancel.
ActiveButtonCount = ButtonCount.OKOnly
Buttons(FirstButton) = Array(True, Captions(ButtonCaption.ButtonOK), VbMsgBoxResult.vbCancel)
Case vbRetryCancel
ActiveButtonCount = ButtonCount.RetryCancel
Buttons(FirstButton) = Array(True, Captions(ButtonCaption.ButtonTryAgain), VbMsgBoxResult.vbRetry)
Buttons(FirstButton + 1) = Array(True, Captions(ButtonCaption.ButtonCancel), VbMsgBoxResult.vbCancel)
Case vbYesNo
ActiveButtonCount = ButtonCount.YesNo
Buttons(FirstButton) = Array(True, Captions(ButtonCaption.ButtonYes), VbMsgBoxResult.vbYes)
Buttons(FirstButton + 1) = Array(True, Captions(ButtonCaption.ButtonNo), VbMsgBoxResult.vbNo)
NoCancel = True
Case vbYesNoCancel
ActiveButtonCount = ButtonCount.YesNoCancel
Buttons(FirstButton) = Array(True, Captions(ButtonCaption.ButtonYes), VbMsgBoxResult.vbYes)
Buttons(FirstButton + 1) = Array(True, Captions(ButtonCaption.ButtonNo), VbMsgBoxResult.vbNo)
Buttons(FirstButton + 2) = Array(True, Captions(ButtonCaption.ButtonCancel), VbMsgBoxResult.vbCancel)
Case Else
' Identical to OKOnly.
' Note: Any click action (except Help) will result in Cancel.
ActiveButtonCount = ButtonCount.OKOnly
Buttons(FirstButton) = Array(True, Captions(ButtonCaption.ButtonOK), VbMsgBoxResult.vbCancel)
End Select
' Add a Help button at far right if requested.
If HelpButtonCount = 1 Then
HelpButtonIndex = ActiveButtonCount
Buttons(HelpButtonIndex) = Array(True, Captions(ButtonCaption.ButtonHelp), MsgBoxResultNone)
ActiveButtonCount = ActiveButtonCount + HelpButtonCount
End If
' Reset remaining buttons.
For ButtonIndex = ActiveButtonCount To MaxButtonCount - 1
Buttons(ButtonIndex) = Array(False, vbNullString, MsgBoxResultNone)
Next
' Set display status for all buttons.
For ButtonIndex = FirstButton To MaxButtonCount - 1
With Me("Button" & CStr(ButtonIndex))
.Visible = Buttons(ButtonIndex)(ButtonProperty.Visible)
.Caption = Buttons(ButtonIndex)(ButtonProperty.Caption)
End With
Next
' Expand the form to make room for multiple buttons.
WindowExpand = Positions(ActiveButtonCount - 1) - Positions(FirstButton)
WindowWidth = Me.WindowWidth + WindowExpand
Me.Move Me.WindowLeft, Me.WindowTop, WindowWidth
' Remove form's close button for certain button combinations.
If NoCancel = True Then
Me!ButtonCancel.Enabled = False
Else
' Reposition Cancel button and form's close button (picture).
Me!ButtonCancel.Left = Me!ButtonCancel.Left + WindowExpand
Me!PictureClose.Left = Me!PictureClose.Left + WindowExpand
Me!PictureCloseActive.Left = Me!PictureCloseActive.Left + WindowExpand
Me!PictureCloseInactive.Left = Me!PictureCloseInactive.Left + WindowExpand
Me!BoxInactive.Left = Me!BoxInactive.Left + WindowExpand
End If
' Extend the form to fit a supersized prompt.
LineCount = PromptLineCount()
If LineCount > 0 Then
' Extend the form and controls (except buttons) to
' make room for multiple prompt lines.
WindowExtend = FormExtend(LineCount)
End If
' Position active buttons.
For ButtonIndex = FirstButton To ActiveButtonCount - 1
With Me("Button" & CStr(ButtonIndex))
.Left = Positions(ButtonIndex)
.Top = .Top + WindowExtend
End With
Next
' Apply tab settings.
Call SetDefaultButton
End Sub
无需重新喷漆。