将控制值保存到类对象属性

我有这个用户表单,我有一堆控件,并且控制名称对应于我在OrderPrompts内定义的属性名称。

当用户点击保存button时,我试图将所有这些控制值保存回原始对象OrderPrompts

这里是顺序提示类Option Explicit

 Private pSKU As String Private pWidth As String Private pHeight As String Private pDepth As String Private pLeftSwing As String Private pRightSwing As String Private pLeftFinishedEnd As String Private pRightFinishedEnd As String Private pToeKickHeight As String Private pAdjShelfQty As String Private pLeftStileWidth As String Private pRightStileWidth As String Private pTopRailWidth As String Private pBottomRailWidth As String Private pExtendLeftFFDown As String Private pExtendLeftFFUp As String Private pExtendRightFFDown As String Private pExtendRightFFUp As String Private pExtendTopRail As String Private pExtendBottomRail As String Private pBayHeightCalc As String Private pBay1Height As String Private pBay2Height As String Private pBay3Height As String Private pBay4Height As String Private pBay5Height As String Private pBayWidthCalc As String Private pBay1Width As String Private pBay2Width As String Private pBay3Width As String Private pBay4Width As String Private pBay5Width As String Private pDrawerFrontCalc As String Private pTopDrawerFront As String Private pSecondDrawerFront As String Private pThirdDrawerFront As String Private pBottomDrawerFront As String Public Property Get SKU() As String SKU = pSKU End Property Public Property Let SKU(Value As String) pSKU = Value End Property Public Property Get Width() As String Width = pWidth End Property Public Property Let Width(Value As String) pWidth = Value End Property Public Property Get Height() As String Height = pHeight End Property Public Property Let Height(Value As String) pHeight = Value End Property Public Property Get Depth() As String Depth = pDepth End Property Public Property Let Depth(Value As String) pDepth = Value End Property Public Property Get LeftSwing() As String LeftSwing = pLeftSwing End Property Public Property Let LeftSwing(Value As String) pLeftSwing = Value End Property Public Property Get RightSwing() As String RightSwing = pRightSwing End Property Public Property Let RightSwing(Value As String) pRightSwing = Value End Property Public Property Get LeftFinishedEnd() As String LeftFinishedEnd = pLeftFinishedEnd End Property Public Property Let LeftFinishedEnd(Value As String) pLeftFinishedEnd = Value End Property Public Property Get RightFinishedEnd() As String RightFinishedEnd = pRightFinishedEnd End Property Public Property Let RightFinishedEnd(Value As String) pRightFinishedEnd = Value End Property Public Property Get ToeKickHeight() As String ToeKickHeight = pToeKickHeight End Property Public Property Let ToeKickHeight(Value As String) pToeKickHeight = Value End Property Public Property Get AdjShelfQty() As String AdjShelfQty = pAdjShelfQty End Property Public Property Let AdjShelfQty(Value As String) pAdjShelfQty = Value End Property Public Property Get LeftStileWidth() As String LeftStileWidth = pLeftStileWidth End Property Public Property Let LeftStileWidth(Value As String) pLeftStileWidth = Value End Property Public Property Get RightStileWidth() As String RightStileWidth = pRightStileWidth End Property Public Property Let RightStileWidth(Value As String) pRightStileWidth = Value End Property Public Property Get TopRailWidth() As String TopRailWidth = pTopRailWidth End Property Public Property Let TopRailWidth(Value As String) pTopRailWidth = Value End Property Public Property Get BottomRailWidth() As String BottomRailWidth = pBottomRailWidth End Property Public Property Let BottomRailWidth(Value As String) pBottomRailWidth = Value End Property Public Property Get ExtendLeftFFDown() As String ExtendLeftFFDown = pExtendLeftFFDown End Property Public Property Let ExtendLeftFFDown(Value As String) pExtendLeftFFDown = Value End Property Public Property Get ExtendLeftFFUp() As String ExtendLeftFFUp = pExtendLeftFFUp End Property Public Property Let ExtendLeftFFUp(Value As String) pExtendLeftFFUp = Value End Property Public Property Get ExtendRightFFDown() As String ExtendRightFFDown = pExtendRightFFDown End Property Public Property Let ExtendRightFFDown(Value As String) pExtendRightFFDown = Value End Property Public Property Get ExtendRightFFUp() As String ExtendRightFFUp = pExtendRightFFUp End Property Public Property Let ExtendRightFFUp(Value As String) pExtendRightFFUp = Value End Property Public Property Get ExtendTopRail() As String ExtendTopRail = pExtendTopRail End Property Public Property Let ExtendTopRail(Value As String) pExtendTopRail = Value End Property Public Property Get ExtendBottomRail() As String ExtendBottomRail = pExtendBottomRail End Property Public Property Let ExtendBottomRail(Value As String) pExtendBottomRail = Value End Property Public Property Get BayHeightCalc() As String BayHeightCalc = pBayHeightCalc End Property Public Property Let BayHeightCalc(Value As String) pBayHeightCalc = Value End Property Public Property Get Bay1Height() As String Bay1Height = pBay1Height End Property Public Property Let Bay1Height(Value As String) pBay1Height = Value End Property Public Property Get Bay2Height() As String Bay2Height = pBay2Height End Property Public Property Let Bay2Height(Value As String) pBay2Height = Value End Property Public Property Get Bay3Height() As String Bay3Height = pBay3Height End Property Public Property Let Bay3Height(Value As String) pBay3Height = Value End Property Public Property Get Bay4Height() As String Bay4Height = pBay4Height End Property Public Property Let Bay4Height(Value As String) pBay4Height = Value End Property Public Property Get Bay5Height() As String Bay5Height = pBay5Height End Property Public Property Let Bay5Height(Value As String) pBay5Height = Value End Property Public Property Get BayWidthCalc() As String BayWidthCalc = pBayWidthCalc End Property Public Property Let BayWidthCalc(Value As String) pBayWidthCalc = Value End Property Public Property Get Bay1Width() As String Bay1Width = pBay1Width End Property Public Property Let Bay1Width(Value As String) pBay1Width = Value End Property Public Property Get Bay2Width() As String Bay2Width = pBay2Width End Property Public Property Let Bay2Width(Value As String) pBay2Width = Value End Property Public Property Get Bay3Width() As String Bay3Width = pBay3Width End Property Public Property Let Bay3Width(Value As String) pBay3Width = Value End Property Public Property Get Bay4Width() As String Bay4Width = pBay4Width End Property Public Property Let Bay4Width(Value As String) pBay4Width = Value End Property Public Property Get Bay5Width() As String Bay5Width = pBay5Width End Property Public Property Let Bay5Width(Value As String) pBay5Width = Value End Property Public Property Get DrawerFrontCalc() As String DrawerFrontCalc = pDrawerFrontCalc End Property Public Property Let DrawerFrontCalc(Value As String) pDrawerFrontCalc = Value End Property Public Property Get TopDrawerFront() As String TopDrawerFront = pTopDrawerFront End Property Public Property Let TopDrawerFront(Value As String) pTopDrawerFront = Value End Property Public Property Get SecondDrawerFront() As String SecondDrawerFront = pSecondDrawerFront End Property Public Property Let SecondDrawerFront(Value As String) pSecondDrawerFront = Value End Property Public Property Get ThirdDrawerFront() As String ThirdDrawerFront = pThirdDrawerFront End Property Public Property Let ThirdDrawerFront(Value As String) pThirdDrawerFront = Value End Property Public Property Get BottomDrawerFront() As String BottomDrawerFront = pBottomDrawerFront End Property Public Property Let BottomDrawerFront(Value As String) pBottomDrawerFront = Value End Property 

OrderPrompts是如何在表单控件中定义的

 Public Property Get OrderPrompts() As clsOrderPromptRow Set OrderPrompts = pOrderPrompts End Property Public Property Let OrderPrompts(Value As clsOrderPromptRow) Set pOrderPrompts = Value End Property 

这里是我点击button的代码

 Private Sub btnSave_Click() Dim Prompt As Control Dim PageIndex As Long For PageIndex = 0 To Me.TabControl.Pages.Count - 1 For Each Prompt In TabControl.Pages(PageIndex).Controls 'MsgBox (TypeName(Prompt)) 'CallByName(Me.ProductPromptMapping, PromptControl.ControlName, VbGet) Select Case TypeName(Prompt) Case "TextBox" CallByName Me.OrderPrompts, Prompt.Name, VbLet, Prompt.Text Case "OptionButton" CallByName Me.OrderPrompts, Prompt.Name, VbLet, Prompt.ControlFormat.Value Case "CheckBox" CallByName Me.OrderPrompts, Prompt.Name, VbLet, CStr(Prompt.Value) Case "ComboBox" CallByName Me.OrderPrompts, Prompt.Name, VbLet, Prompt.Value End Select Next Next MsgBox (OrderPrompts.Width) Me.Hide End Sub 

我遇到的问题是将checkbox,Combobox和OptionButton值保存回对象。 文本框的值工作得很好。 所以我不知道该怎么解决。 我不断收到types不匹配的错误,或者不支持null。

任何帮助表示赞赏。 提前致谢。

简单的答案是对所有控件使用Prompt.Object.Value

在这里输入图像说明

 Public Sub PrintControls() Dim Prompt As Control Dim PageIndex As Long For PageIndex = 0 To Me.TabControl.Pages.Count - 1 For Each Prompt In TabControl.Pages(PageIndex).Controls Debug.Print "Name:", Prompt.Name, "Value:", Prompt.Object.Value Next Next End Sub 

我真的很喜欢你的概念,所以我写了我自己的类来保存和加载控制值。 我的课保存对控件及其值的引用以方便检索。 下载工作簿

单击保存button时,对多页上每个控件的引用将被保存到脚本字典中。 然后将Dictionary添加到ArrayList中,并将ArrayList中的Dictionary的索引添加到ListBox中。 单击ListBox时,将保存当前设置,并加载对应于Listbox值的以前的设置。

在这里输入图像说明

类:SettingsDictionaryClass

 Option Explicit Private ControlSetting As Object Private mTabControl As MSForms.MultiPage Sub Init(TabControl As MSForms.MultiPage) Set mTabControl = TabControl Set ControlSetting = CreateObject("System.Collections.ArrayList") End Sub Function Save() As Long Dim subDict As Object: Set subDict = CreateObject("Scripting.Dictionary") Dim ctrl As MSForms.Control Dim PageIndex As Long With mTabControl For PageIndex = 0 To .Pages.Count - 1 For Each ctrl In .Pages(PageIndex).Controls subDict.Add ctrl, ctrl.Object.Value Next Next End With Save = ControlSetting.Count ControlSetting.Add subDict End Function Sub LoadValues(ListIndex As Long, Optional SaveCurrent As Boolean) Dim ctrl Dim Settings As Object If SaveSetting Then Save Set Settings = ControlSetting.Item(ListIndex) For Each ctrl In Settings ctrl.Value = Settings(ctrl) Next End Sub 

用户窗体:用户窗体1

 Option Explicit Private SettingsDictionary As SettingsDictionaryClass Private Sub btnSave_Click() lboSettings.AddItem SettingsDictionary.Save End Sub Private Sub lboSettings_Click() SettingsDictionary.LoadValues lboSettings.Value, True End Sub Private Sub UserForm_Initialize() Set SettingsDictionary = New SettingsDictionaryClass SettingsDictionary.Init TabControl End Sub