{Solved} Allow a user to move a control and right click

Post your Gambas programming questions here.
Post Reply
User avatar
sadams54
Posts: 143
Joined: Monday 9th July 2018 3:43am
Contact:

{Solved} Allow a user to move a control and right click

Post by sadams54 »

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?
Last edited by sadams54 on Tuesday 5th December 2023 7:41pm, edited 1 time in total.
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Allow a user to move a control and right click

Post by vuott »

sadams54 wrote: Monday 4th December 2023 11:44pm1.. is it possible to allow a user to drag a control around my program screen and allow the control to be moved there?
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.
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Allow a user to move a control and right click

Post by BruceSteers »

Application.ActiveControl should do the job for detecting what control was last right-clicked to get a menu...

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
User avatar
cogier
Site Admin
Posts: 1127
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Allow a user to move a control and right click

Post by cogier »

1.. is it possible to allow a user to drag a control around my program screen and allow the control to be moved there?
Here is some example code you can run in a Graphical Program that will show you how to do this.
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?
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.

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
User avatar
sadams54
Posts: 143
Joined: Monday 9th July 2018 3:43am
Contact:

Re: Allow a user to move a control and right click

Post by sadams54 »

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.
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Allow a user to move a control and right click

Post by vuott »

vuott wrote: Tuesday 5th December 2023 3:47am Actually, you could drag a Control without the Drag&Drop...
...infact cogier showed another way to move the Controls.
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
sadams54
Posts: 143
Joined: Monday 9th July 2018 3:43am
Contact:

Re: {Solved} Allow a user to move a control and right click

Post by sadams54 »

yep, I used that alternate as it worked exactly as I wanted.
Post Reply