Forum Discussion

DanMcG's avatar
DanMcG
Brass Contributor
Jan 22, 2021
Solved

Can I programmatically change properties of selected userform controls when I am designing the form?

I have created code to add 4 new commands to a context menu (MSForms Control) that is shown when I right click on selected controls in a userform. Basically I want to nudge the selected controls in a particular direction (up/down/left/right) a set number of pixels? I realize that the Selection command is only for Excel proper and not VBE. Is there a way of looping through the 'selection set' of selected controls and modify the Top/Left properties when I am constructing the userform?

4 Replies

  • DanMcG's avatar
    DanMcG
    Brass Contributor

    DanMcG

    I've determined that this cannot be done, so how do I close the question? 

    • ExcelHCFUser's avatar
      ExcelHCFUser
      Copper Contributor

      DanMcG 

      ThisWorkbook.VBProject.VBComponents("UserForm1").designer.controls("ComboBox1").left = 0

      and/or

      ThisWorkbook.VBProject.VBComponents("UserForm1").designer.controls("ComboBox1").top = 0

       

      note that ThisWorkbook.VBProject.VBComponents("UserForm1").designer properties and methods are hidden and there is no intellisense available

       

      • DanMcG's avatar
        DanMcG
        Brass Contributor

        ExcelHCFUser 

         

        This can be done by accessing the .Designer property of the selected vbcomponent. Below will move the selected controls a specific direction a number of pixels.

        Sub nudgeHor(distanceInPixels As Long)
            Dim ctl    As Control
            For Each ctl In ThisWorkbook.VBProject.VBComponents(Application.VBE.SelectedVBComponent.Name).Designer.Selected
                ctl.Left = ctl.Left + distanceInPixels
            Next ctl
        End Sub
        Sub nudgeVer(distanceInPixels As Long)
            Dim ctl    As Control
            For Each ctl In ThisWorkbook.VBProject.VBComponents(Application.VBE.SelectedVBComponent.Name).Designer.Selected
                ctl.Top = ctl.Top + distanceInPixels
            Next ctl
        End Sub

Resources