I am trying to have a button, when clicked, creates a new picturebox control. So that everytime I click it, it adds another new picturebox control. These pictureboxes will all have the same functions like being able to move them around and draw on them. But the button only makes one and no more after that with the following code. What am I missing?
Public Class Form1
Dim xpos As New Integer
Dim ypos As New Integer
Dim pos As New Point
Dim x As Integer
Dim y As Integer
Dim canvas As New PictureBox
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = 0
i = i + 1
canvas.Name = "canvas" & i
canvas.BackColor = Color.White
canvas.BorderStyle = BorderStyle.FixedSingle
canvas.Image = Nothing
canvas.Height = TextBox1.Text
canvas.Width = TextBox2.Text
AddHandler canvas.MouseDown, AddressOf PictureBox1_MouseDown
AddHandler canvas.MouseMove, AddressOf PictureBox1_MouseMove
Controls.Add(canvas)
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
xpos = Cursor.Position.X - canvas.Location.X
ypos = Cursor.Position.Y - canvas.Location.Y
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
pos = MousePosition
pos.X = pos.X - xpos
pos.Y = pos.Y - ypos
canvas.Location = pos
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Dim canvas As New PictureBox
You have multiple bugs in your code but this is the most serious one. The As New syntax ensures that you'll always create a PictureBox object, but it will only ever be one object. And of course, one variable cannot keep track of multiple picture boxes.
What you need to do is create a new one ever time the button is clicked. It is generally a good idea to keep track of the picture boxes you create. So correct code looks like:
Dim canvases As New List(Of PictureBox)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim canvas = New PictureBox
canvases.Add(canvas)
canvas.Name = "canvas" & canvases.Count.ToString()
'' etc...
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim canvas = DirectCast(sender, PictureBox)
xpos = Cursor.Position.X - canvas.Location.X
ypos = Cursor.Position.Y - canvas.Location.Y
End Sub
Note how the sender argument gives you a reference back to the picture box object that's being moused. Do the same thing in any other event handlers.
Increasing the counter whenever a new canvas is created:
Public Class Form1
Dim counter As New Integer
...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
counter += 1
canvas.Name = "canvas" & counter.ToString()
...
Accessing the canvas that the mouse is currently moving over:
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
pos = MousePosition
pos.X = pos.X - xpos
pos.Y = pos.Y - ypos
DirectCast(sender, PictureBox).Location = pos
End If
End Sub
Related
I have a User Control that is Dynamically created. It has to raise a Mouse_Move event & Mouse_Down event.
How to manage events for Multiple User Control that are created dynamically. I was considering using a list of user controls to track the controls. But I do not know how to setup the events properly.
Public Class UserControl1
Public Structure Porportions
Dim width_Percent As Double
Dim Height_percent As Double
Dim X_Location_Percent As Double
Dim Y_Location_Percent As Double
End Structure
Dim Pipe As Porportions
Dim guage1 As Porportions
Dim guage2 As Porportions
Public start_pos As Point
Public move_offset As Point
Public Client_Point As Point
Public Pipe_Source As Excel
Public Pipe_Data As DataSet
Public Pipe_Properties As Pipe
Private Pipe_ID As String
' Public Event Pipe_MouseMove(ByVal sender As Object, ByVal e As System.EventArgs)
Public Event Pipe_MouseMove1(ByVal sender As Object, ByVal e As System.EventArgs)
Public Event Pipe_MouseDown1(ByVal sender As Object, ByVal e As System.EventArgs)
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
RaiseEvent Pipe_MouseMove1(sender, e)
End Sub
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
RaiseEvent Pipe_MouseDown1(sender, e)
End Sub
Public Class Form1
Private pipe_cnt As Integer = 0
Private start_position As Point
Private MoveOffset As Point
Private Mouse_Position As Point
Private WithEvents pp As UserControl1
Private Sub Pipe_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles pp.Pipe_MouseMove1
Dim dx As Integer
Dim dy As Integer
Dim m_loc As Point
Dim scrn As Point
m_loc = New Point(e.Location)
Mouse_Position = New Point(e.X, e.Y)
scrn = PointToScreen(Mouse_Position)
Mouse_Position = PointToClient(Mouse_Position)
dx = start_position.X - Mouse_Position.X
dy = start_position.Y - Mouse_Position.Y
MoveOffset = New Point(dx, dy)
If e.Button = MouseButtons.Left Then
Try
pp.Location = New Point(pp.Left + e.X, pp.Top + e.Y)
pp.Location = New Point(pp.Left + e.X, pp.Top + e.Y)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
Private Sub Pipe_MouseDown1(ByVal sender As Object, ByVal e As System.EventArgs) Handles pp.Pipe_MouseDown1
start_position = New Point(pp.Location)
End Sub
What I understand that you want to use an same event for multiple user controls. There are many methods to achieve this.
Method 1 (Easiest):
Just put handler events after Handles clause and separate them by commas ,. See example:
Private Sub MouseMove_Event(sender As Object, e As MouseEventArgs) Handles Pipe.MouseMove, PictureBox1.MouseMove
MsgBox("MouseMove")
End Sub
Private Sub Click_Event(sender As Object, e As MouseEventArgs) Handles Pipe.Click, PictureBox1.Click
MsgBox("Click")
End Sub
Private Sub MouseDown_Event(sender As Object, e As MouseEventArgs) Handles Pipe.MouseDown, PictureBox1.MouseDown
MsgBox("MouseDown")
End Sub
Method 2 (burden):
Create and collect all controls in a array of controls and then create events in a foreach loop.
Create Sub that gets array of controls and add handlers using foreach loop.
Private Sub CreateHandlers(Controls() As Control)
For Each control As Control In Controls
Me.Controls.Add(control)
AddHandler control.Click, AddressOf Click_Event
AddHandler control.MouseMove, AddressOf MouseMove_Event
AddHandler control.MouseDown, AddressOf MouseDown_Event
Next
End Sub
Your events
Private Sub Click_Event(sender As Object, e As EventArgs)
'Handle Click events here
End Sub
Private Sub MouseMove_Event(sender As Object, e As EventArgs)
'Handle MouseMove events here
End Sub
Private Sub MouseDown_Event(sender As Object, e As EventArgs)
'Handle MouseDown events here
End Sub
Create controls dynamically and just call CreateHandlers(controls) at end
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim pictureBox1 As PictureBox = New PictureBox _
With {
.Size = New Size(100, 100),
.Location = New Point(0, 0),
.BackColor = Color.Black
}
Dim panel1 As Panel = New Panel _
With {
.Size = New Size(100, 100),
.Location = New Point(100, 0),
.BackColor = Color.Red
}
Dim tableLayoutPanel1 As TableLayoutPanel = New TableLayoutPanel _
With {
.Size = New Size(100, 100),
.Location = New Point(200, 0),
.BackColor = Color.Green
}
Dim controls() As Control = {pictureBox1, panel1, tableLayoutPanel1}
CreateHandlers(controls)
End Sub
End Class
So I nailed the ability to drag a picturebox around the Windows form. I need it to hide itself when it's dragged and dropped over another picture. I've tried a few methods but none seem to work and I am now back at square one, the only code I have is so that I can move the picturebox around the form.
I Solved Your Problem
Here Is How The Form Should Look Like
And Here Is The Code I Made:
Private Sub CheckTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckTimer.Tick
CheckTimer.Stop()
CheckTimer.Interval = 1
If PictureBox2.Location = New System.Drawing.Point(TextBox1.Text, TextBox2.Text) Then
PictureBox2.Visible = False
End If
CheckTimer.Start()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = PictureBox1.Location.X
TextBox2.Text = PictureBox1.Location.Y
CheckTimer.Start()
End Sub
Hope This Code Was Helpful To You.
I made an better code than the old one, try it.
Here Is How The Form Should Look Like:
and here is the code :
Public Class Form1
Dim Point As New Point
Dim X, Y As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
Timer1.Interval = 1
Point = Cursor.Position
PictureBox2.Location = New System.Drawing.Point(Point.X - X, Point.Y - Y)
Timer1.Start()
End Sub
Private Sub PictureBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown
X = Cursor.Position.X - PictureBox2.Location.X
Y = Cursor.Position.Y - PictureBox2.Location.Y
Timer1.Start()
End Sub
Private Sub PictureBox2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseUp
Timer1.Stop()
If PictureBox2.Location.X < PictureBox1.Size.Width Then
PictureBox2.Visible = False
End If
End Sub
End Class
I hope this code was useful to you.
Label1 below moves right and left. How would I get it to lag and "catchup" to the mouse pointer?
Private xpos as integer = 10
Private Sub me_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim lbl As New label
lbl.Size = New System.Drawing.Size(xpos, 100)
end sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
lbl1.Location = New Point(MyPictureBox.PointToClient(New Point(MousePosition.X, 0)).X, 100)
I have a form without borders that I would like the user to be able to move. I have not been able to find anything that would allow me to do so.
Is it possible to move a window with the border set to None?
Introduce a Boolean variable which holds the state if the form is currently dragged and variables which hold the starting point of the drag. Then OnMove move the form accordingly. As this has already been answered elsewhere, I just copy&paste it here.
Class Form1
Private IsFormBeingDragged As Boolean = False
Private MouseDownX As Integer
Private MouseDownY As Integer
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown
If e.Button = MouseButtons.Left Then
IsFormBeingDragged = True
MouseDownX = e.X
MouseDownY = e.Y
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp
If e.Button = MouseButtons.Left Then
IsFormBeingDragged = False
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove
If IsFormBeingDragged Then
Dim temp As Point = New Point()
temp.X = Me.Location.X + (e.X - MouseDownX)
temp.Y = Me.Location.Y + (e.Y - MouseDownY)
Me.Location = temp
temp = Nothing
End If
End Sub
End Class
stolen from http://www.dreamincode.net/forums/topic/59643-moving-form-with-formborderstyle-none/
All 'simple' VB answers made my form jump all over the place with multiple screens. So i derived this from same answer in C# and it works like a charm :
Public Const WM_NCLBUTTONDOWN As Integer = 161
Public Const HT_CAPTION As Integer = 2
then
<DllImport("User32")> Private Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As Integer) As Integer
End Function
<DllImport("User32")> Private Shared Function ReleaseCapture() As Boolean
End Function
and finally
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If (e.Button = MouseButtons.Left) Then
ReleaseCapture()
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0)
End If
End Sub
Dim offSetX As Integer
Dim offSetY As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Location = New Point(Cursor.Position.X - offSetX, Cursor.Position.Y - offSetY)
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
offSetX = PointToClient(Cursor.Position).X
offSetY = PointToClient(Cursor.Position).Y
Timer1.Enabled = True
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
Timer1.Enabled = False
End Sub
This is slightly scruffy way of doing it xD
Hope it helps though =]
Another way of doing this is to handle the WM_NCHITTEST message . This allows you to have parts of your form respond to mouse events as the title bar, borders, etc. would for a window with borders. For example, if you have a label on your form and you return HTCAPTION in the WM_NCHITTEST handler, you will be able to move the form by dragging this label just as you can move a regular window by dragging on its title bar. See this Stack Overflow question for example code.
Create a new form with three picture boxes. This code is intended to draw a border when the mouse enters the picture box and remove it when it leaves. It is inconsistent in the results. Sometimes it draws/removes the border, sometimes it doesn't. This code is not complex. Using VS 2012.
Private Sub PictureBox_MouseEnter(sender As Object, e As EventArgs) _
Handles PictureBox1.MouseEnter, PictureBox2.MouseEnter, PictureBox3.MouseEnter
Dim pb As PictureBox = DirectCast(sender, PictureBox)
pb.BorderStyle = BorderStyle.FixedSingle
' Debug.WriteLine("E " & pb.Name)
End Sub
Private Sub PictureBox_MouseLeave(sender As Object, e As EventArgs) _
Handles PictureBox1.MouseLeave, PictureBox2.MouseLeave, PictureBox3.MouseLeave
Dim pb As PictureBox = DirectCast(sender, PictureBox)
pb.BorderStyle = BorderStyle.None
' Debug.WriteLine("X " & pb.Name)
End Sub
I could also reproduce the issue. So, expanding on the comments above about "drawing something else" instead of using the Picturebox's property, let me suggest this quick and dirty approach:
Use a RectangleShape object, the one provided by the VisualBasic Powerpack 3.0 addon. Simply put one of those in the same form your PictureBox is in, and make it invisible (visible = false).
The code is also easy:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.RectangleShape1.Location = New Point(Me.PictureBox1.Left - 1, Me.PictureBox1.Top - 1)
Me.RectangleShape1.Size = New Size(Me.PictureBox1.Width + 1, Me.PictureBox1.Height + 1)
End Sub
Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
Me.RectangleShape1.Visible = True
End Sub
Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
Me.RectangleShape1.Visible = False
End Sub
End Class
Need some help from your Form MouseEnter Event ..
Dim pb As PictureBox = New PictureBox
Private Sub Form1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
pb.BorderStyle = BorderStyle.None
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
pb = PictureBox1
pb.BorderStyle = BorderStyle.FixedSingle
End Sub
I followed KalaNag idea by putting my picturebox inside a panel and handled the event on the pciturebox by doing like this
private void PictureBox_MouseEnter(object sender, EventArgs e)
{
PictureBox control = sender as PictureBox;
(control.Parent as Panel).Width = 20;
(control.Parent as Panel).Height = 20;
(control.Parent as Panel).BorderStyle = BorderStyle.Fixed3D;
}
private void PictureBox_MouseLeave(object sender, EventArgs e)
{
PictureBox control = sender as PictureBox;
(control.Parent as Panel).Width = 18;
(control.Parent as Panel).Height = 18;
(control.Parent as Panel).BorderStyle = BorderStyle.None;
}
I changed the control's size because otherwise, the picturebox keeps flickering when the mouse hovers the borders as the cursor is entering and leaving indefinitely since the borders change the size of the control.
Works like a charm !