I use the following code to handle positioning of certain controls in my Form;
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
'Sub detects which arrow key is pressed
Dim strControlName As String
' Get the name of the control
strControlName = Me.ActiveControl.Name
Dim aControl = Me.Controls.Item(strControlName)
If strControlName <> "PrintButton" Then
If keyData = Keys.Up Then
aControl.Location = New Point(aControl.Location.X, aControl.Location.Y - 1)
Return True
End If
'detect down arrow ke
If keyData = Keys.Down Then
aControl.Location = New Point(aControl.Location.X, aControl.Location.Y + 1)
Return True
End If
'detect left arrow key
If keyData = Keys.Left Then
aControl.Location = New Point(aControl.Location.X - 1, aControl.Location.Y)
Return True
End If
'detect right arrow key
If keyData = Keys.Right Then
aControl.Location = New Point(aControl.Location.X + 1, aControl.Location.Y)
Return True
End If
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
I also have a PictureBox that I allow a Drag n Drop image into;
Private Sub pbSig_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragDrop
Dim picbox As PictureBox = CType(sender, PictureBox)
Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
If files.Length <> 0 Then
Try
picbox.Image = Image.FromFile(files(0))
pbSig.ImageLocation = files(0)
Catch ex As Exception
MessageBox.Show("Problem opening file ")
End Try
End If
End Sub
Private Sub pbSig_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Is there a way I can make the PictureBox "movable" using the arrow keys? I can't use a KeyPress event on the Form since I am already using it elsewhere. I was hoping I could set a focus on the PictureBox or allow the user to do a "+Arrow" event.
Also, if I make the PictureBox move, is the dropped image going to move with it?
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Up Then
PictureBox1.Top -= 5
End If
If e.KeyCode = Keys.Down Then
PictureBox1.Top += 5
End If
If e.KeyCode = Keys.Left Then
PictureBox1.Left -= 5
End If
If e.KeyCode = Keys.Right Then
PictureBox1.Left += 5
End If
End Sub
You can use this code to move the PictureBox using Arrow Keys.
Here is what I ended up using. The Mouse just much better sense, plus I get to store it to Settings along with the other settings. I think this is a good solution without getting into any DB work. Opinions?
Private Sub CheckForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'...
pbSig.Location = My.Settings.pbSigLoc
'Allow an image to be dropped
pbSig.AllowDrop = True
End Sub
End Sub
' The next three subs control the moving of the pbSig location using the mouse
Dim startX As Integer
Dim startY As Integer
Dim endX As Integer
Dim endY As Integer
Dim mDown As Boolean
Dim valX As Boolean
Dim valY As Boolean
Private Sub pbSig_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseDown
startX = MousePosition.X
startY = MousePosition.Y
mDown = True
valX = False
valY = False
End Sub
Private Sub Main_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
End Sub
Private Sub pbSig_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseMove
'Check if mouse=down
If mDown = True Then
endX = (MousePosition.X - Me.Left)
endY = (MousePosition.Y - Me.Top)
If valY = False Then
startY = endY - sender.top
valY = True
End If
If valX = False Then
startX = endX - sender.left
valX = True
End If
sender.left = endX - startX
sender.top = endY - startY
End If
End Sub
'If mouseUp=True then End and Save to Settings
Private Sub pbSig_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseUp
My.Settings.pbSigLoc = pbSig.Location
mDown = False
valX = False
valY = False
End Sub
This way all the user needs to do is use their mouse to locate the pB and it's contents and I don't need to call the ProcessCmdKey again. And I still have the arrow keys functionality on the Controls I need it to be.
Related
I want to be able to Drag controls around during runtime.
The code below is an example I found on the web that does exactly what I need it to do. The article dated back to 2009 and was rather dead so I couldn't ask any questions.
The problem with this code is that every control on the Form can be moved around. Even when locking certain controls.
Is there a way to prevent certain controls from being moved?
Public Class Form1
Dim dragging As Boolean
Dim startX As Integer
Dim startY As Integer
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the
''NorthwindDataSet.Employees' table. You can move, or remove it, as needed.
Me.EmployeesTableAdapter.Fill(Me.NorthwindDataSet.Employees)
For Each Control As Control In Me.Controls
AddHandler Control.MouseDown, AddressOf startDrag
AddHandler Control.MouseMove, AddressOf whileDragging
AddHandler Control.MouseUp, AddressOf endDrag
Next
For Each Control As Control In Me.Controls
For Each item In My.Settings.controlLocations
If Split(item, "!")(0) = Control.Name Then
Control.Location = New Point(Split(item, "!")(1), _
Split(item, "!")(2))
End If
Next
Next
End Sub
Private Sub startDrag(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs)
dragging = True
startX = e.X
startY = e.Y
End Sub
Private Sub whileDragging(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MouseEventArgs)
If dragging = True Then
sender.Location = New Point(sender.Location.X + _
e.X - startX, sender.Location.Y + e.Y - startY)
Me.Refresh()
End If
End Sub
Private Sub endDrag(ByVal sender As System.Object, ByVal e As System.EventArgs)
dragging = False
My.Settings.controlLocations.Clear()
For Each Control As Control In Me.Controls
My.Settings.controlLocations.Add(Control.Name & "!" _
& Control.Location.X & "!" & Control.Location.Y)
Next
My.Settings.Save()
End Sub
End Class
The 3 subs do all the work, and there is a blank My.Settings named controlLocations that stores the locations.
EDIT 1
Example:
For Each label1 As Control In Me.Controls
AddHandler label1.MouseDown, AddressOf startDrag
AddHandler label1.MouseMove, AddressOf whileDragging
AddHandler label1.MouseUp, AddressOf endDrag
Next
For Each label1 As Control In Me.Controls
For Each item In My.Settings.controlLocations
If Split(item, "!")(0) = label1.Name Then
label1.Location = New Point(Split(item, "!")(1), Split(item, "!")(2))
End If
Next
Next
Found the solution. Below is the code that will allow the user to move around controls (only the ones they wish to be movable) in a windows form. Just add the handlers for each control behind the subs.
Dim startx As Integer
Dim starty As Integer
Dim endy As Integer
Dim endx As Integer
Dim finalx As Integer
Dim finaly As Integer
Dim mdown As Boolean
Dim valx As Boolean
Dim valy As Boolean
Private Sub Main_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
End Sub
Private Sub MuisDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
startx = MousePosition.X
starty = MousePosition.Y
mdown = True
valx = False
valy = False
End Sub
Private Sub MuisUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
mdown = False
valx = False
valy = False
End Sub
Private Sub MuisMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If mdown = True Then
endx = (MousePosition.X - Me.Left)
endy = (MousePosition.Y - Me.Top)
If valy = False Then
starty = endy - sender.top
valy = True
End If
If valx = False Then
startx = endx - sender.left
valx = True
End If
sender.left = endx - startx
sender.top = endy - starty
End If
End Sub
Example Label1 + PictureBox1
Private Sub MuisDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown, PictureBox1.MouseDown
Private Sub MuisUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseUp, PictureBox1.MouseUp
Private Sub MuisMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove, PictureBox1.MouseMove
So my problem is that I want to have multiple rectangles on a form at a time. However I also want these rectangles to be able to be clicked and dragged across the form.
This is my current code for clicking and dragging a rectangle that was drawn onto the form using the toolbox.
Public Class DragRectangle
Dim Go As Boolean
Dim LeftSet As Boolean
Dim TopSet As Boolean
Dim HoldLeft As Integer
Dim HoldTop As Integer
Dim OffLeft As Integer
Dim OffTop As Integer
Private Sub obj1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RectangleShape1.MouseUp
Go = False
LeftSet = False
TopSet = False
End Sub
Private Sub obj1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RectangleShape1.MouseDown
Go = True
End Sub
Private Sub obj1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RectangleShape1.MouseMove
If Go = True Then
HoldLeft = (Control.MousePosition.X - Me.Left)
HoldTop = (Control.MousePosition.Y - Me.Top)
If TopSet = False Then
OffTop = HoldTop - sender.Top
TopSet = True
End If
If LeftSet = False Then
OffLeft = HoldLeft - sender.Left
LeftSet = True
End If
sender.Left = HoldLeft - OffLeft
sender.Top = HoldTop - OffTop
End If
End Sub
End Class
This works fine for one rectangle, although this requires the rectangles to be pre-drawn onto the form using the toolbox.
What I would like is a rectangle gets drawn by clicking a button on the form, and the newly drawn rectangle can also be clicked and dragged into a new location.
Is this possible?
Thanks for any help
Working example:
Public Class Form1
Private Property Rectangles As New List(Of DrgRectangle)
Private Property curRect As DrgRectangle
Private _x As Integer
Private _y As Integer
Private Sub loadme() Handles Me.Load
'load the rectangle in list
Rectangles.Add(New DrgRectangle With {.Rect = New Rectangle(20, 20, 20, 20)})
End Sub
Private Sub FormMouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
_x = e.X
_y = e.Y
For Each rect In Rectangles
If rect.Rect.Contains(e.X, e.Y) Then
curRect = rect
Exit For
End If
Next
End Sub
Private Sub FormMouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
If curRect IsNot Nothing Then
curRect.Rect = New Rectangle(New Point(curRect.Rect.Location.X + (e.X - _x), curRect.Rect.Location.Y + (e.Y - _y)), curRect.Rect.Size)
Me.Refresh()
End If
End If
_x = e.X
_y = e.Y
End Sub
Private Sub FormPaint(sender As Object, e As PaintEventArgs) Handles me.Paint
For Each rect In Rectangles
e.Graphics.DrawRectangle(Pens.Black, rect.Rect)
Next
End Sub
End Class
Public Class DrgRectangle
Public Rect As New Rectangle
'add more properties as needed
End Class
I have a class that changes the mouse event args of a picturebox and allows the user to resize it during runtime. I am trying to add a checkbox for each picturebox that will maintain the aspect ratio of the picturebox when the checkbox is checked.
I can get it to work if I add the code separately for each checkbox on the form but I want to keep it in a separate class so that it works for any checkbox.
Here is the code for the class to resize the pictureboxes
Public Class ResizeableControl
Public WithEvents mControl As Control
Public mPreserveAspectRatio As Boolean
Dim AtRightEdge As Boolean = False
Dim AtBottomEdge As Boolean = False
Dim InBoxWidth As Boolean = False
Dim InBoxHeight As Boolean = False
Dim DraggingHorizontal As Boolean
Dim DraggingVerticle As Boolean
Dim DraggingCorner As Boolean
Const DragMarginWidth As Integer = 6
Const DragMarginHeight As Integer = 6
Public dragOrigin As Point
Dim MoveBox As Boolean = False
Dim LastPos As Point
Public Sub New(ByVal Control As Control, ByVal preserveAspectRatio As Boolean)
mControl = Control
mPreserveAspectRatio = preserveAspectRatio
End Sub
Private Sub mControl_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseMove
If MoveBox Then
Dim movement As Point = Cursor.Position
' move image by the distance the mouse moved hoizontally and vertically
movement.Offset(-LastPos.X, -LastPos.Y)
mControl.Location = movement
ElseIf DraggingHorizontal Then
Dim movement As Point = New Point(e.X - dragOrigin.X, e.Y - dragOrigin.Y) 'How far did the mouse move? (Distance = newPoint - oldPoint)
If mPreserveAspectRatio = True Then
mControl.Width += movement.X 'Change width of the image by the distance the mouse moved
mControl.Height = Math.Round((Convert.ToDouble(mControl.Width) / 6.0) * 4.0)
Else
mControl.Width += movement.X 'Change width of the image by the distance the mouse moved
End If
dragOrigin = e.Location 'Next time we will measure from the now-current mouse position
ElseIf DraggingVerticle Then
Dim movement As Point = New Point(e.X - dragOrigin.X, e.Y - dragOrigin.Y)
If mPreserveAspectRatio = True Then
mControl.Height += movement.Y 'Change height of the image by the distance the mouse moved
mControl.Width = Math.Round((Convert.ToDouble(mControl.Height) / 4.0) * 6.0)
Else
mControl.Height += movement.Y 'Change height of the image by the distance the mouse moved
End If
dragOrigin = e.Location
ElseIf DraggingCorner Then
Dim movement As Point = New Point(e.X - dragOrigin.X, e.Y - dragOrigin.Y)
If mPreserveAspectRatio = True Then
' Resize the image by the distance the mouse moved hoizontally and vertically
mControl.Height += movement.Y
mControl.Width += movement.X
mControl.Height = Math.Round((Convert.ToDouble(mControl.Width) / 6.0) * 4.0)
mControl.Width = Math.Round((Convert.ToDouble(mControl.Height) / 4.0) * 6.0)
Else
' Resize the image by the distance the mouse moved hoizontally and vertically
mControl.Height += movement.Y
mControl.Width += movement.X
End If
dragOrigin = e.Location
Else
' Is mouse within right six-or-so pixels?
AtRightEdge = e.X > (mControl.Width - DragMarginWidth)
' Is mouse within bottom six-or-so pixels?
AtBottomEdge = e.Y > (mControl.Height - DragMarginHeight)
' Is mouse within the box?
InBoxWidth = e.X < (mControl.Width - DragMarginWidth)
InBoxHeight = e.Y < (mControl.Height - DragMarginHeight)
' Set the cursor accordingly
If (AtBottomEdge And AtRightEdge) Then
mControl.Cursor = Cursors.SizeNWSE
ElseIf (InBoxWidth And InBoxHeight) Then
mControl.Cursor = Cursors.SizeAll
ElseIf (AtBottomEdge) Then
mControl.Cursor = Cursors.SizeNS
ElseIf (AtRightEdge) Then
mControl.Cursor = Cursors.SizeWE
Else
mControl.Cursor = Cursors.Default
End If
End If
End Sub
Private Sub mControl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseDown
' If the user presses the mouse button at the bottom right corner, begin dragging
If (InBoxWidth And InBoxHeight) Then
Dim movement As Point = Cursor.Position
movement.Offset(-mControl.Location.X, -mControl.Location.Y)
LastPos = movement
MoveBox = True
ElseIf (AtBottomEdge And AtRightEdge) Then
dragOrigin = e.Location
DraggingCorner = True
ElseIf AtRightEdge Then
dragOrigin = e.Location
DraggingHorizontal = True
ElseIf AtBottomEdge Then
dragOrigin = e.Location
DraggingVerticle = True
End If
End Sub
Private Sub mControl_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseUp
' Stop dragging
MoveBox = False
DraggingHorizontal = False
DraggingVerticle = False
DraggingCorner = False
End Sub
Private Sub mControl_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mControl.MouseLeave
MoveBox = False
DraggingHorizontal = False
DraggingVerticle = False
DraggingCorner = False
mControl.Cursor = Cursors.Default
End Sub
End Class
I create a new instance of the class for each picturebox (I have just 2 here to keep it simple but have more) when the form loads but then I can't figure out how to change the aspect ratio boolean (mPreserveAspectRatio). I have tried just changing the boolean when the checkbox changes states but that doesn't work. And I think I may need to somehow create a public method to change the boolean but can't wrap my head around it. Here is the latest code I have been trying that doesn't work
Imports WindowsApplication1.ResizeableControl
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mControl As New ResizeableControl(PictureBox1, True)
Dim mControl2 As New ResizeableControl(PictureBox2, True)
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
Dim mControl1 As New ResizeableControl(PictureBox1, True)
Else
Dim mControl1 As New ResizeableControl(PictureBox1, False)
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked Then
Dim mControl1 As New ResizeableControl(PictureBox2, True)
Else
Dim mControl1 As New ResizeableControl(PictureBox2, False)
End If
End Sub
End Class
I figured it out. You pointed me in the right direction Jeff. I did need to add the control to the form. Here is what I did.
Imports WindowsApplication1.ResizeableControl
Public Class Form1
Dim mControl1 As New ResizeableControl(PictureBox1, True)
Dim mControl2 As New ResizeableControl(PictureBox2, True)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Control1 As New ResizeableControl(PictureBox1, True)
Me.mControl1 = Control1
Dim Control2 As New ResizeableControl(PictureBox2, True)
Me.mControl2 = Control2
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
Me.mControl1.mPreserveAspectRatio = True
Else
Me.mControl1.mPreserveAspectRatio = False
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked Then
Me.mControl2.mPreserveAspectRatio = True
Else
Me.mControl2.mPreserveAspectRatio = False
End If
End Sub
End Class
I used this code to maximize and restore my custom form. But when the form is maximized, it still remains draggable, I use a timer to drag the form.
Private Sub btnMaximize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaximize.Click, lblTitle.MouseDoubleClick
Dim maximizeHeight As Integer = Screen.PrimaryScreen.WorkingArea.Height
Dim maximizeWidth As Integer = Screen.PrimaryScreen.WorkingArea.Width
Dim maximizeLocation As Point = New Point(0, 0)
Dim fullscreen As Boolean = False
If Me.Height = maximizeHeight Or Me.Width = maximizeWidth Or Me.Location = maximizeLocation Then
fullscreen = True
Else
fullscreen = False
End If
If fullscreen = True Then
Me.Size = New Size(1000, 500)
Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2
Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2
ElseIf fullscreen = False Then
Me.Location = New Point(0, 0)
Me.Size = New Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
End If
End Sub
Private Sub pnlBar_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblTitle.MouseDown
MoveTmr.Start()
refpositions()
End Sub
Private Sub MoveTmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MoveTmr.Tick
Me.Location = oloc - ocur + System.Windows.Forms.Cursor.Position
refpositions()
End Sub
Private Sub pnlBar_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblTitle.MouseUp
MoveTmr.Stop()
refpositions()
End Sub
Private Sub RszTmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles RszTmr.Tick
Me.Size = appSize - curPos + Cursor.Position
refpositions()
End Sub
make:
Dim fullscreen As Boolean = False
a class variable.
Then edit this code to fit your variables:
Private Sub Mover_Tick(sender As System.Object, e As System.EventArgs) Handles Mover.Tick
If fullscreen = false Then
Dim pt As New Point((Me.Location.X + (MousePosition.X - mPosX)), (Me.Location.Y + (MousePosition.Y - mPosY)))
Me.Location = pt
mPosX = MousePosition.X
mPosY = MousePosition.Y
End If
End Sub
EDIT:
Also implement this:
Private Sub Title_StartDrag(sender As System.Object, e As MouseEventArgs) Handles Title.MouseDown
mPosX = MousePosition.X
mPosY = MousePosition.Y
If e.Button = Windows.Forms.MouseButtons.Left Then
Mover.Start()
End If
End Sub
Private Sub Title_StopDrag(sender As System.Object, e As MouseEventArgs) Handles Title.MouseUp
Mover.Stop()
End Sub
also you may want to make it much simpler by saying
Me.WindowState = FormWindowState.Maximized
I use the Mousedown, Mouseup and Mousemove events to move my forms.
Public Class Form1
Private Is_Dragged As Boolean = False
Private M_DownX As Integer
Private M_DownY As Integer
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
If e.Button = MouseButtons.Left Then
Is_Dragged = True
M_DownX = e.X
M_DownY = e.Y
End If
End Sub
Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
If e.Button = MouseButtons.Left Then
Is_Dragged = False
End If
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If Is_Dragged Then
Dim tmp_pnt As Point = New Point()
tmp_pnt.X = Me.Location.X + (e.X - M_DownX)
tmp_pnt.Y = Me.Location.Y + (e.Y - M_DownY)
Me.Location = tmp_pnt
tmp_pnt = Nothing
End If
End Sub
End Class
and to maximize my form.
Private Sub Btn_Main_Max_Click(sender As Object, e As EventArgs) Handles Btn_Main_Max.Click
Static IsAlreadyResized As Boolean
If Not IsAlreadyResized Then
Me.WindowState = FormWindowState.Maximized
IsAlreadyResized = True
Exit Sub
End If
If IsAlreadyResized Then
Me.WindowState = FormWindowState.Normal
IsAlreadyResized = False
End If
End Sub
You can create a panel in code or visually and place dock.top then you can put a label on the side or in the middle and a picturebox on the left side of the window create the buttons to close minimize and maximize or others and add the friend code above to move the form by the titl bar as in windows
I am working on a simple program which requires me to be able to select a picture box and move it to a new location by dragging it with my mouse. This is all the relevant code I have come up with currently. However, when I run the program it tries to move to where I want it to go then it seems to revert back to its previous location.
Edit: it is in a container. If this is of any relevance.
Variables
Dim startx As Integer
Dim starty As Integer
Dim endy As Integer
Dim endx As Integer
Dim finalx As Integer
Dim finaly As Integer
Dim mdown As Boolean
Dim valx As Boolean
Dim valy As Boolean
Code to make image move
Private Sub picbox_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles picbox.MouseDown
startx = MousePosition.X
starty = MousePosition.Y
mdown = True
valx = False
valy = False
End Sub
Private Sub Main_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
End Sub
Private Sub picbox_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles picbox.MouseMove
'Check if mouse=down
If mdown = True Then
endx = (MousePosition.X - Me.Left)
endy = (MousePosition.Y - Me.Top)
If valy = False Then
starty = endy - sender.top
valy = True
End If
If valx = False Then
startx = endx - sender.left
valx = True
End If
sender.left = endx - startx
sender.top = endy - starty
End If
End Sub
Private Sub picbox_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles picbox.MouseUp
mdown = False
valx = False
valy = False
End Sub
Remove it out of the container. That is probably what is giving you the problems as your code works perfectly for me.
Turn off the Autosize property.
This works for me:
Private _isMoved As Boolean
Private _x As Integer
Private _y As Integer
Private Sub Control_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Control.MouseDown
_isMoved = True
_x = e.Location.X
_y = e.Location.Y
End Sub
Private Sub Control_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Control.MouseMove
If _isMoved Then
Control.Location = New Point(Control.Location.X + (e.Location.X - _x), Control.Location.Y + (e.Location.Y - _y))
End If
End Sub
Private Sub Control_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Control.MouseUp
_isMoved = False
End Sub
Turn of AutoSize, Ensure docking of the picturebox is turned off and ensure that Anchor is Top Left