2013-01-22 37 views
3

我一直在試圖編寫一些代碼來處理大小調整時調整大小&移動控件。UserForm調整大小代碼不正常

我的方法是爲每個需要被錨定(即頂部/左/寬/高)的控件維度創建一個Dictionary對象,其中鍵爲控件名稱和錨點維度(例如lstAccounts_Height)並將它們添加到一個表單級集合。在表單Resize事件集合迭代完成後,根據需要調整每個控件。

例行添加控件:

Private Sub AddFormResizeControl(ControlName As String, _ 
           Dimension As String) 

Dim strKey      As String 
Dim sngValue     As Single 
Dim dictCtrl     As Dictionary 

    strKey = ControlName & "_" & Dimension 

    Select Case Dimension 
     Case "Left": sngValue = Controls(ControlName).Left 
     Case "Top": sngValue = Controls(ControlName).Top 
     Case "Width": sngValue = Controls(ControlName).Width 
     Case "Height": sngValue = Controls(ControlName).Height 
    End Select 

    Set dictCtrl = New Dictionary 
    dictCtrl.Add strKey, sngValue 

    If colResizeControls Is Nothing Then _ 
     Set colResizeControls = New Collection 
    colResizeControls.Add dictCtrl, strKey 

End Sub 

並添加控件在Initialize事件:

AddFormResizeControl "lst_AccountSelection", "Width" 
AddFormResizeControl "lst_AccountSelection", "Height" 
AddFormResizeControl "cmd_Cancel", "Left" 
AddFormResizeControl "cmd_Cancel", "Top" 
AddFormResizeControl "cmd_Confirm", "Left" 
AddFormResizeControl "cmd_Confirm", "Top" 

而且Resize事件:

Private Sub UserForm_Resize() 

Dim sngHeightAdjust    As Single 
Dim sngWidthAdjust    As Single 

Dim dict      As Dictionary 
Dim strCtrl      As String 

Dim ctrl      As Control 
Dim strDimension    As String 

    If Me.Width < sngFormMinimumWidth Then Me.Width = sngFormMinimumWidth 
    If Me.Height < sngFormMinimumHeight Then Me.Height = sngFormMinimumHeight 

    sngHeightAdjust = (Me.Height - 4.5) - sngFormMinimumHeight 
    sngWidthAdjust = (Me.Width - 4.5) - sngFormMinimumWidth 

    If Not colResizeControls Is Nothing Then 
     For Each dict In colResizeControls 
      strCtrl = dict.Keys(0) 
      Set ctrl = Controls(Left(strCtrl, InStrRev(strCtrl, "_") - 1)) 
      If Right(strCtrl, 5) = "_Left" Then 
       ctrl.Left = dict.Item(strCtrl) + sngWidthAdjust 
      ElseIf Right(strCtrl, 4) = "_Top" Then 
       ctrl.Top = dict.Item(strCtrl) + sngHeightAdjust 
      ElseIf Right(strCtrl, 6) = "_Width" Then 
       ctrl.Width = dict.Item(strCtrl) + sngWidthAdjust 
      ElseIf Right(strCtrl, 7) = "_Height" Then 
       ctrl.Height = dict.Item(strCtrl) + sngHeightAdjust 
      End If 
     Next dict 
    End If 

End Sub 

的問題,我面對在第一次移動事件中有一個小的「跳躍」,因此運行時控件在設計時並不完全一致。我試圖通過將表單的返回高度和寬度更改爲4.5來抵消這種影響,這對我有幫助。

sngFormMinimumHeightsngFormMinimumWidth設置爲起始寬度/高度或Initialize事件中的表單,並且我使用Chip Pearson's code使表單可調整大小。

我猜測窗體上有一些需要調整的邊界(因此4.5s可以幫助解決問題) - 任何人都可以解釋我需要調整的值嗎?

分辨率多虧了BonCodigo提供的鏈接,這個問題現在已經解決了 - 我指的是Me.HeightMe.Width時,我應該已經提到Me.InsideHeightMe.InsideWidth。我現在不需要的4.5調整和「跳」現在已經沒有了

+0

是否有可能向我們展示您的表單?雖然您似乎調整了窗體大小,但似乎並未將控件(如果有的話)定位並調整... – bonCodigo

+0

調整大小/重新定位發生在For Each循環期間 - 相關錨點尺寸增加/降低sngHeightAdjust或sngWidthAdjust – CrazyHorse

回答

2

您可以嘗試改變controls兩個AnchorDock性質,他們應該與他們的parent containers(可能是一個面板)自動調整。

+0

謝謝,但這是Excel和謙虛UserForm沒有這樣的屬性 – CrazyHorse

+0

AndyPope的文章主要是爲Excel。而且很可能你必須跟蹤這種情況下的「調整比例」,並將其應用於控件。 – bonCodigo

+2

道歉,我完全誤讀了你的答案。 Andy Pope的方法與我的相似,但是我看到他使用的是InsideHeight/InsideWidth,我認爲這可能是我所追求的。我現在會做一些測試 – CrazyHorse

相關問題