VB.NET Move Dynamically Embeded Borderless Form - vb.net

As you can see from the code provided below as well as the title, that I'm having trouble trying to figure out how to move this dynamically embeded borderless form in VB.NET.
Private Sub AddWidget_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddWidget.Click
Dim WidgetForm As Form
WidgetForm = New Form()
WidgetForm.ShowInTaskbar = False
WidgetForm.TopMost = True
WidgetForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
WidgetForm.BackgroundImage = Image.FromFile(Me.OpenFileDialog1.FileName)
WidgetForm.BackgroundImageLayout = ImageLayout.Stretch
WidgetForm.Show()
opac = Me.OpacInt.Text
WidgetForm.Opacity = opac
sizew = Me.WidgetW.Text
sizey = Me.WidgetY.Text
WidgetForm.Size = New System.Drawing.Size(sizew, sizey)
End Sub
I've done this method (code below) a couple times in the past, and it always seems to work for moving a form, but I'm unsure on how to apply this for the dynamically embeded form.
Any help would be greatly appreciated.
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
drag = True
mousex = Windows.Forms.Cursor.Position.X - Me.Left
mousey = Windows.Forms.Cursor.Position.Y - Me.Top
End If
Timer1.Enabled = True
Timer1.Interval = 2500
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If drag Then
Me.Top = Windows.Forms.Cursor.Position.Y - mousey
Me.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
Timer1.Enabled = False
drag = False
End Sub

The dynamic form would perform as same as the normal form. First of all you need to add some events for the dynamic form when you create it
Public WidgetForm As Form
Private Sub AddWidget_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddWidget.Click
WidgetForm = New Form()
WidgetForm.ShowInTaskbar = False
WidgetForm.TopMost = True
WidgetForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
WidgetForm.BackgroundImage = Image.FromFile(Me.OpenFileDialog1.FileName)
WidgetForm.BackgroundImageLayout = ImageLayout.Stretch
WidgetForm.Show()
opac = Me.OpacInt.Text
WidgetForm.Opacity = opac
sizew = Me.WidgetW.Text
sizey = Me.WidgetY.Text
WidgetForm.Size = New System.Drawing.Size(sizew, sizey)
'Add the event here
AddHandler WidgetForm.MouseDown, AddressOf WidgetForm_MouseDown
AddHandler WidgetForm.MouseMove, AddressOf WidgetForm_MouseMove
AddHandler WidgetForm.MouseUp, AddressOf WidgetForm_MouseUp
End Sub
Then you can move the form by the events handler
Private Sub WidgetForm_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
drag = True
mousex = Windows.Forms.Cursor.Position.X - CType(sender, Form).Left
mousey = Windows.Forms.Cursor.Position.Y - CType(sender, Form).Top
End If
Timer1.Enabled = True
Timer1.Interval = 2500
End Sub
Private Sub WidgetForm_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If drag Then
CType(sender, Form).Top = Windows.Forms.Cursor.Position.Y - mousey
CType(sender, Form).Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub WidgetForm_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Timer1.Enabled = False
drag = False
End Sub

Related

How to clear or initialize imagelist for began game again?

I want to make a game for 43 image from imagelist as random and 1 picturebox for display , and I have 43 button , I would to clear imagelist when I click the button "starr" for began the game without reset form.
My code just for first 4 button
Public Class Form1
'Random Class included in .NET
Private RND As New Random
Dim ImageIndexes As New List(Of Integer)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
okkk: 'Populate the array with the Index of each Image in the ImageList
For i As Integer = 0 To ImageList1.Images.Count - 1
ImageIndexes.Add(i)
Next i
End Sub
Private Sub Buttons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click,
Button2.Click, Button3.Click, Button4.Click
CType(sender, Button).Visible = False
Dim ArrayIndex As Integer = RND.Next(0, ImageIndexes.Count)
PictureBox1.Image = ImageList1.Images(ImageIndexes(ArrayIndex))
ImageIndexes.Remove(ArrayIndex)
End Sub
Private Sub starr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles starr.Click
ImageIndexes.Clear()
PictureBox1.Image = Nothing
Button1.Visible = True
Button2.Visible = True
Button3.Visible = True
Button3.Visible = True
Button4.Visible = True
End Sub
End Class

How to move picture boxes in a panel using vb.net

I'm trying to move picture boxes in a panel.
This is my Code:
Private dragging As Boolean
Private beginX, beginY As Integer
Private Sub Control_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
dragging = True
beginX = CType(sender, PictureBox).Location.X
beginY = CType(sender, PictureBox).Location.Y
End Sub
Private Sub Control_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim cntrl As Control = CType(sender, Control)
If dragging = True Then
cntrl.Location = New Point(cntrl.Location.X + e.X - beginX, cntrl.Location.Y + e.Y - beginY)
'Me.Refresh()
End If
End Sub
Private Sub Control_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
dragging = False
End Sub
I can't figure out why this don't work.
The subroutines you have are missing their handlers (ie the handles statement) at the end.
ex:
Private Sub Control_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) HANDLES controlName.MouseUp
dragging = False
End Sub
Try this:
Dim cmd As Boolean = False
Dim sp As Point
Private Sub Form1_Load() Handles MyBase.Load
For Each Control As Picturebox In Me.Controls.OfType(Of Picturebox)
AddHandler Control.MouseDown, Sub(sender As Object, e As MouseEventArgs)
cmd = True
sp = e.Location
End Sub
AddHandler Control.MouseMove, Sub(sender As Object, e As MouseEventArgs)
If cmd Then
Control.Location = Control.Location - sp + e.Location
End If
End Sub
AddHandler Control.MouseUp, Sub(sender As Object, e As MouseEventArgs)
cmd = False
End Sub
Next
End Sub

Drag any label in the form using the mouse

I need to drag any label that's on my form.
I am using this code, the problem is it only moves the 1 label label1
I can copy paste several time and it works, but I am using a code to ad new labels to the form and that code does not see them.
How can I click and drag any label on the form?
Public Class Form1
Dim lbl As Label
Dim Off As Point
Private Sub obj1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles label1.MouseDown
Off.X = MousePosition.X - sender.Left
Off.Y = MousePosition.Y - sender.Top
End Sub
Private Sub obj1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles label1.MouseMove
If e.Button = MouseButtons.Left Then
sender.Left = MousePosition.X - Off.X
sender.Top = MousePosition.Y - Off.Y
End If
End Sub
End Class
"i want to drag any label that is located in the form"
Instead of hard-wiring the handler with the "Handles xxx.yyy" clauses, wire them up at run-time in the Load() event of the Form:
Public Class Form1
Dim Off As Point
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
WireLabels(Me)
End Sub
Private Sub WireLabels(ByVal cont As Control)
For Each ctl As Control In cont.Controls
If TypeOf ctl Is Label Then
AddHandler ctl.MouseDown, AddressOf obj1_MouseDown
AddHandler ctl.MouseMove, AddressOf obj1_MouseMove
ElseIf ctl.HasChildren Then
WireLabels(ctl)
End If
Next
End Sub
Private Sub obj1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Off.X = MousePosition.X - sender.Left
Off.Y = MousePosition.Y - sender.Top
End Sub
Private Sub obj1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then
sender.Left = MousePosition.X - Off.X
sender.Top = MousePosition.Y - Off.Y
End If
End Sub
End Class
Obviously you'd use AddHandler for any Labels created at run-time as well.
Change your code to this:
Private Sub obj1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Off.X = MousePosition.X - sender.Left
Off.Y = MousePosition.Y - sender.Top
End Sub
Private Sub obj1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then
sender.Left = MousePosition.X - Off.X
sender.Top = MousePosition.Y - Off.Y
End If
End Sub
And in your form load have this:
AddHandler label1.MouseDown, Addressof obj1_MouseDown
AddHandler label1.MouseMove, Addressof obj1_MouseMove
And in your label creation code:
dim aLabel as label = new Label
AddHandler aLabel.MouseDown, Addressof obj1_MouseDown
AddHandler aLabel.MouseMove, Addressof obj1_MouseMove
...
you need to add the handlers to the objects after they are created, otherwise the form has no idea what to do with the events.

how to create a picturebox in vb 2008 and move it?

I just want to ask a really important question in vb2008 :
I'm working on 2D level designer that I just put all the images and the collision rectangles in his place then the program generate the right code.
the problem is : I make a button and in his click event it add a new picture box , when it add I have to move it with the mouse , I have the "move" code but like you know it's a new picture box so I can't write the code before the picture box add.(if you doesn't understand I can explain my situation again)
for more clear this is the code :
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim i As Integer
newPictureBox.Image = Image.FromFile("C:\Users\hp\Desktop\ground.bmp")
newPictureBox.Name = "image" & (i)
newPictureBox.Visible = True
newPictureBox.Top = 200
newPictureBox.Width = 100
newPictureBox.Height = 50
newPictureBox.Left = 100 + goToRight
newPictureBox.SizeMode = PictureBoxSizeMode.AutoSize
'add control to form
Controls.Add(newPictureBox)
goToRight = goToRight + newPictureBox.Width
i += 1
End Sub
and this is the "move picturebox" code(it's for an existed picturbox):
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim endPoint As Integer = Val(TextBox1.Text)
Label1.Text = "X: " & MousePosition.X - (Location.X + 8)
Label2.Text = "Y: " & MousePosition.Y - (Location.Y + 29)
RadioButton1.Left = endPoint + RadioButton2.Left
Panel1.Left = 0
'Move picture code :
If IcanMove = True Then
PictureBox1.Left = MousePosition.X - (Location.X + 8) - differenceX
PictureBox1.Top = MousePosition.Y - (Location.Y + 29) - differenceY
End If
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
IcanMove = True
differenceX = (MousePosition.X - (Location.X + 8)) - PictureBox1.Left
differenceY = (MousePosition.Y - (Location.Y + 29)) - PictureBox1.Top
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
IcanMove = False
End Sub
thanks you for reading :)
You need to add event handlers for the newly added PictureBox, here is how to do that.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim NewPictureBox As PictureBox = New PictureBox
NewPictureBox.Parent = Me
NewPictureBox.Location = New Point(100, 100)
Me.Controls.Add(NewPictureBox)
'you can use existing event handlers like your PictureBox1_MouseDown
AddHandler NewPictureBox.MouseUp, AddressOf PictureBox_MouseUp
AddHandler NewPictureBox.MouseDown, AddressOf PictureBox_MouseDown
End Sub
Private Sub PictureBox_MouseDown(sender As Object, e As MouseEventArgs)
MessageBox.Show("Triggered MouseDown Event")
End Sub
Private Sub PictureBox_MouseUp(sender As Object, e As MouseEventArgs)
MessageBox.Show("Triggered MouseUp Event")
End Sub
(if you also remove the PictureBox's before closing the form be sure to remove the handlers for them)
For more info check AddHandler and RemoveHandler

Form with custom title bar & formborderstyle = None remains draggable when maximized

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