通过类在3个列表框中的1个中移动单个选定项

时间:2017-01-31 13:14:49

标签: vba

为可怕的标题道歉,这不是最简单的描述。我目前有一个带有3个列表框的表单,并且与每个2个命令按钮相邻,允许列表框中的所选项目向上或向下移动。一切正常,但我想将命令按钮的数量减少到一对,并使用当前选择的项目在列表框上操作,但我不知道它是否可能。我的代码是:

表单声明(每对向上/向下按钮有3个实例):

Private m_clsListMoveUpDown1                 As CListbox_UpDown
Private m_clsListMoveUpDown2                 As CListbox_UpDown
Private m_clsListMoveUpDown3                 As CListbox_UpDown

然后通过以下方式实例化类的3个实例(在表单中)

Set m_clsListMoveUpDown1 = New CListbox_UpDown
With m_clsListMoveUpDown1
Set .MoveDownButton = Me.Btn_MoveDown1
Set .MoveUpButton = Me.Btn_MoveUp1
Set .UpDownList = Me.LB_Sheet1
End With

Set m_clsListMoveUpDown2 = New CListbox_UpDown
With m_clsListMoveUpDown2
Set .MoveDownButton = Me.Btn_MoveDown2
Set .MoveUpButton = Me.Btn_MoveUp2
Set .UpDownList = Me.LB_Sheet2
End With

Set m_clsListMoveUpDown3 = New CListbox_UpDown
With m_clsListMoveUpDown3
Set .MoveDownButton = Me.Btn_MoveDown3
Set .MoveUpButton = Me.Btn_MoveUp3
Set .UpDownList = Me.LB_Sheet3
End With

最后,一些类声明:

Public WithEvents MoveUpButton               As MSForms.CommandButton
Public WithEvents MoveDownButton             As MSForms.CommandButton
Public UpDownList                            As MSForms.ListBox

我没有包括2No中的任何一个。在课程中子程序(向上或向下移动所选项目)但如果有帮助可以做。简而言之,它们包含:

With Me.UpDownList
'lines of code using arrays to move items up or down
End With

我完全理解为什么单对向上/向下按钮目前仅在由例如指定的列表框上操作的原因。 Set .UpDownList = Me.LB_Sheet1但我不知道如何关联其他2个列表框,以便如果其中任何一个具有所选项目,则让按钮在该列表框上运行。

我尝试使用Set .UpDownList = Me.LB_Sheet1 or Me.LB_Sheet2 or Me.LB_Sheet2,但是出现了类型不匹配错误,我不确定还有什么可以尝试。

1 个答案:

答案 0 :(得分:0)

我使用了类似的课程

Private frmParent As MSForms.UserForm
Private lstListBoxUnderControl As MSForms.ListBox

Public WithEvents UP As MSForms.CommandButton
Public WithEvents DOWN As MSForms.CommandButton

Public Property Set PARENT_FORM(frm As MSForms.UserForm)
    Set frmParent = frm
End Property

Public Property Set LISTBOX_CONTROLLING(lb As MSForms.ListBox)
    Set lstListBoxUnderControl = lb
End Property

Private Sub Class_Initialize()
    '
End Sub

Private Sub DOWN_Click()
    '   Down Code
    lstListBoxUnderControl.BackColor = vbRed
End Sub

Private Sub UP_Click()
    '   Up Code
    lstListBoxUnderControl.BackColor = vbGreen
End Sub

以类似的形式进行设置

Dim c As New clsUpdateController

Private Sub ListBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Set c.LISTBOX_CONTROLLING = Me.ActiveControl
End Sub
Private Sub ListBox2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Set c.LISTBOX_CONTROLLING = Me.ActiveControl
End Sub

Private Sub ListBox3_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Set c.LISTBOX_CONTROLLING = Me.ActiveControl
End Sub
Private Sub ListBox4_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Set c.LISTBOX_CONTROLLING = Me.ActiveControl
End Sub
Private Sub ListBox5_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Set c.LISTBOX_CONTROLLING = Me.ActiveControl
End Sub
Private Sub UserForm_Initialize()
    Set c.PARENT_FORM = Me
    Set c.DOWN = Me.cmdDown
    Set c.UP = Me.cmdUP
    Me.ListBox1.AddItem "test"
    Me.ListBox2.AddItem "test"
    Me.ListBox3.AddItem "test"
    Me.ListBox4.AddItem "test"
End Sub