I have 2 questions...
1.. is it possible to allow a user to drag a control around my program screen and allow the control to be moved there?
2.. If I put a right click or context menu on a group of controls how can I tell which control spawned the right click menu?
{Solved} Allow a user to move a control and right click
{Solved} Allow a user to move a control and right click
Last edited by sadams54 on Tuesday 5th December 2023 7:41pm, edited 1 time in total.
Re: Allow a user to move a control and right click
If by "screen" you mean the Container, where the Control is located, you can simply use the "Drag&Drop":
Public Sub Form_Open()
Label1.Background = Color.Red
Me.Drop = True
End
Public Sub Label1_MouseDrag()
Label1.Drag("")
End
Public Sub Form_DragMove()
' When moving the "Label", the mouse pointer remains at the point in the "Label" where you clicked:
With Label1
.X = Drag.X - Mouse.StartX
.Y = Drag.Y - Mouse.StartY
End With
End
Actually, you could drag a Control without the Drag&Drop...
Europaeus sum !
Amare memorentes atque deflentes ad mortem silenter labimur.
Amare memorentes atque deflentes ad mortem silenter labimur.
- BruceSteers
- Posts: 1608
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Allow a user to move a control and right click
Application.ActiveControl should do the job for detecting what control was last right-clicked to get a menu...
To move controls... (alternative)
Note: you can only move controls if the parent container has Container.Arrangement = Arrange.None ,
If the container has Arrangement set it will try to place the controls itself and not let you do it.
Public Sub ContextMenu_Click()
Select Application.ActiveControl.Name
Case "Button1"
Print "Button 1 clicked context menu"
End Select
End
To move controls... (alternative)
Public Sub MoveableControl_MouseMove()
If Not Mouse.Left Then Return
' Last.X is controls current X pos , (Mouse.X - Mouse.StartX) gives the distance to move it keeping pointer at the controls click position.
Last.Move(Last.X + (Mouse.X - Mouse.StartX), Last.Y + (Mouse.Y - Mouse.StartY))
End
Note: you can only move controls if the parent container has Container.Arrangement = Arrange.None ,
If the container has Arrangement set it will try to place the controls itself and not let you do it.
If at first you don't succeed , try doing something differently.
BruceS
BruceS
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Allow a user to move a control and right click
Here is some example code you can run in a Graphical Program that will show you how to do this.1.. is it possible to allow a user to drag a control around my program screen and allow the control to be moved there?
I have added a menu so you can test this. The Last command will tell you what the control was. Look at the program Title as you click on each control.2.. If I put a right click or context menu on a group of controls how can I tell which control spawned the right click menu?
hPictureBox As PictureBox
hMenu As Menu
Public Sub Form_Open()
SetUpMenu
With Me
.W = 800
.H = 500
.Arrangement = Arrange.None
.Background = Color.Red
End With
For iLoop As Integer = 0 To 2
With hPictureBox = New PictureBox(Me) As "PictureBoxes"
.h = 128
.w = 128
.x = 20 + (iLoop * 150)
.y = 20 + (iLoop * 150)
.Name = "PictureBox" & Str(iLoop + 1)
.Picture = Picture["icon:/128/access"]
.mode = PictureBox.Contain
.PopupMenu = "hMenu"
End With
Next
End
Public Sub SetUpMenu()
Dim hMenuItem As Menu
Dim sTemp As String
Dim sMenuName As String[] = ["One", "Two"]
hMenu = New Menu(Me) As "hMenu"
hMenu.Hide
For Each sTemp In sMenuName
hMenuItem = New Menu(hMenu) As "MyMenu"
hMenuItem.text = sTemp
Next
End
Public Sub PictureBoxes_MouseMove()
Last.Raise
Last.X += Mouse.ScreenX - Last.ScreenX - (Last.W / 2)
Last.Y += Mouse.ScreenY - Last.ScreenY - (Last.H / 2)
End
Public Sub PictureBoxes_MouseDown()
Me.Title = Last.Name
End
Re: Allow a user to move a control and right click
thank you. I do not need the context menu since the drag option worked so perfectly. But everything was perfect and as always you guys are awesome.
Re: Allow a user to move a control and right click
...infact cogier showed another way to move the Controls.
Europaeus sum !
Amare memorentes atque deflentes ad mortem silenter labimur.
Amare memorentes atque deflentes ad mortem silenter labimur.
Re: {Solved} Allow a user to move a control and right click
yep, I used that alternate as it worked exactly as I wanted.