I'm trying to learn how to add and move objects in VB6 through the code module. SO far i've figured out how to insert an object but have no idea how to change the position.

This is what i have so far:

Option Explicit
Private WithEvents cmdOk As CommandButton


Private Sub cmdOk_Click()
Unload Me
End Sub

Private Sub Form_Load()
Set cmdOk = Form1.Controls.AdD("VB.CommandButton", "CmdOK")
cmdOk.Visible = True
cmdOk.Caption = "OK"
End Sub

Dani AI

Generated

Good start by — and thanks to and for the basics. The suggestions in the thread cover the common ways to reposition a runtime control; the notes below add a few practical, reliable techniques that are especially useful when creating multiple controls or when running on different screen/DPI settings.

VB6 uses twips by default, so arithmetic can look odd if you think in pixels. Two common approaches: set the form’s ScaleMode to pixels before doing layout, or convert pixel coordinates to twips using Screen.TwipsPerPixelX and Screen.TwipsPerPixelY. Example of the conversion approach (positions given in pixels, then converted for placement):

' place at pixel coords (pxX, pxY)
Dim pxX As Long, pxY As Long
pxX = 120: pxY = 40
myCtrl.Left = pxX * Screen.TwipsPerPixelX
myCtrl.Top  = pxY * Screen.TwipsPerPixelY

If multiple runtime controls and event handling are needed, use a small class with WithEvents and keep instances in a Collection. That avoids the limitations of a single WithEvents variable and is an alternative to design-time control arrays:

' Class module named CBtnHandler
Public WithEvents Btn As CommandButton

Private Sub Btn_Click()
    MsgBox "Clicked: " & Btn.Caption
End Sub
' Form code: keep handlers so they stay alive
Dim handlers As Collection

Private Sub Form_Load()
    Set handlers = New Collection
End Sub

' after adding a control (Controls.Add), create a CBtnHandler, set its Btn and add to handlers

Practical cautions: prefer Me.Controls.Add when adding from inside the same form (avoids cross-instance mistakes), ensure runtime control names are unique, remove or Destroy controls you no longer need, and recompute positions in Form_Resize for responsive layouts. For many small buttons a design-time control array plus Load/Unload is still the simplest for event handling; for fully dynamic UIs use the class-wrapper approach shown above.

Recommended Answers

All 5 Replies

You have two options...

use the...
.Left
.Top
.Width
.Height

Properties or you could use the...

.Move

Method

Good Luck

cmdOk.move left,top,width,height
' or
    cmdOk.left=left
    cmdOk.top=top
    cmdOkwidth=width
    cmdOk.height=height

but if you want to move this control in anotehr form or module you must use public

I'm trying to learn how to add and move objects in VB6 through the code module. SO far i've figured out how to insert an object but have no idea how to change the position.

This is what i have so far:

Option Explicit
Private WithEvents cmdOk As CommandButton


Private Sub cmdOk_Click()
Unload Me
End Sub

Private Sub Form_Load()
Set cmdOk = Form1.Controls.AdD("VB.CommandButton", "CmdOK")
cmdOk.Visible = True
cmdOk.Caption = "OK"
End Sub

Thanks to both. Another question. what is the best way to center said object.

ctrl.left=(form1.scalewidth-ctrl.width)/2
ctrl.top=(form1.scaleheight-ctrl.height)/2

thank you omoridi

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.