|
-
Mar 20th, 2010, 09:50 PM
#1
Thread Starter
Lively Member
Dynamic Pictureboxs
Hey guys, im trying to make a program that consists of two forms.
One 'toolbox' form, and one 'pallet' form. The toolbox has components that you drag onto the pallet form. Once you drag them onto the pallet form, you should be able to move around the pictureboxes. Unfortunatley, since they are dynamic controls, i have no way of checking what picturebox you click on when there are multiple pictureboxes on the pallet at one time. they are in an arraylist, however i dont know how to check on my mouse_down routine what array index the picturebox is. any ideas?
-
Mar 21st, 2010, 01:42 AM
#2
Re: Dynamic Pictureboxs
You don't need to worry about array indices etc... all events comes with a standard pair of arguments - "sender" and "e", for example
Code:
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
End Sub
sender is a reference to whatever object raised the event, so you can just use "sender" to manipulate the picture box directly (you will probably want to cast from type object to picturebox first though.
Here's an example :
Code:
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim MyPb As PictureBox = CType(sender, PictureBox)
MyPb.Visible = False
End Sub
-
Mar 21st, 2010, 05:23 PM
#3
Thread Starter
Lively Member
Re: Dynamic Pictureboxs
ok this is wat ive been trying to use. problem is, all of my pictureboxs are stored in an arraylist. how do i tell what one im clicking on?
-
Mar 21st, 2010, 05:33 PM
#4
Re: Dynamic Pictureboxs
as you add the pictureboxes, add their index in the arraylist to their .tag property
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2010, 05:34 PM
#5
Re: Dynamic Pictureboxs
for movable / resizable runtime controls have a look at the link in my signature
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2010, 05:51 PM
#6
Thread Starter
Lively Member
Re: Dynamic Pictureboxs
ok so ive tried using this now, and im still running into the same problem. when i click the picturebox, how do i check what tag it is?
-
Mar 21st, 2010, 05:54 PM
#7
Re: Dynamic Pictureboxs
vb Code:
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim MyPb As PictureBox = CType(sender, PictureBox)
msgbox(MyPb.tag.tostring)
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2010, 06:25 PM
#8
Thread Starter
Lively Member
Re: Dynamic Pictureboxs
hmm somethings wrong. i can create new pictureboxes with this:
Code:
Private Sub Label1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.TextChanged
If Label1.Text = "pic1" Then
Dim wall As New PictureBox
wall.Location = MousePosition
wall.BackColor = Color.Silver
wall.Size = New Point(104, 16)
If Form3.OpenFileDialog2.FileName = Nothing Then
If Form3.ColorDialog2.Color = Nothing Then GoTo a
wall.BackColor = Form3.ColorDialog2.Color
a:
Else
Dim img As Image = Image.FromFile(Form3.OpenFileDialog2.FileName)
wall.Image = img
End If
walls.Add(wall)
For Each PictureBox In walls
Dim q As Integer = -1
q = q + 1
i = q
Next
drag = True
wall.Tag = i.ToString
Me.Controls.Add(wall)
AddHandler wall.MouseDown, AddressOf wall_mousedown
End If
End Sub
and then i have the mouse_down sub thats for dynamic controls:
Code:
Dim wstr As String
Private Sub wall_mousedown(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim wall As PictureBox = CType(sender, PictureBox)
wstr = wall.Tag
MsgBox(wstr)
If drag = True Then
drag = False
Label1.Text = ""
Else
drag = True
End If
End Sub
End Class
and finally the mouse_move sub
Code:
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If wstr = Nothing = False Then
If drag = True Then
Dim g As Integer = Val(wstr)
walls(g).Location = MousePosition
End If
End If
End Sub
when i create two new picboxs, it still wants to move the first one when i add the second one.
-
Mar 21st, 2010, 06:41 PM
#9
Re: Dynamic Pictureboxs
Code:
For Each PictureBox In walls
Dim q As Integer = -1
q = q + 1
i = q
Next
drag = True
wall.Tag = i.ToString
i is always 0
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2010, 06:44 PM
#10
Thread Starter
Lively Member
Re: Dynamic Pictureboxs
oooh haha gotts dim g before loop... thanks
-
Mar 21st, 2010, 06:45 PM
#11
Junior Member
Re: Dynamic Pictureboxs
It looks like your "wstr" variable is not global, so I don't see how you're expecting the get the correct value from that. Also, instead of using Form_MouseMove, create a new event for your picturebox control instead: wall_MouseMove, and add your code to that. Then the arguments from wall_MouseMove will tell you which picturebox has been clicked.
-
Mar 21st, 2010, 06:47 PM
#12
Re: Dynamic Pictureboxs
i'll let you take over on this 1 virtueone
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2010, 06:48 PM
#13
Thread Starter
Lively Member
Re: Dynamic Pictureboxs
good idea. thanks lemme try these ideas out
-
Mar 21st, 2010, 06:55 PM
#14
Junior Member
Re: Dynamic Pictureboxs
 Originally Posted by .paul.
i'll let you take over on this 1 virtueone
I was just testing out something very similar today while learning to build dynamic elements and such. I'm sure your responses would be much more educated paul.
-
Mar 21st, 2010, 07:16 PM
#15
Thread Starter
Lively Member
Re: Dynamic Pictureboxs
ok heres my code now;
vb Code:
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Form3.Show() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Form3.Show() Button1.Visible = False End Sub Dim walls As New ArrayList Dim drag As Boolean = False Dim i As Integer = -1 Dim i2 As Integer Private Sub Label1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.TextChanged If Label1.Text = "pic1" Then Dim wall As New PictureBox wall.Location = MousePosition wall.BackColor = Color.Silver wall.Size = New Point(104, 16) If Form3.OpenFileDialog2.FileName = Nothing Then If Form3.ColorDialog2.Color = Nothing Then GoTo a wall.BackColor = Form3.ColorDialog2.Color a: Else Dim img As Image = Image.FromFile(Form3.OpenFileDialog2.FileName) wall.Image = img End If walls.Add(wall) For Each PictureBox In walls q1 = q1 + 1 i = q1 Next drag = True wall.Tag = i.ToString Me.Controls.Add(wall) AddHandler wall.MouseDown, AddressOf wall_mousedown End If End Sub Dim q1 As Integer = -1 Private Sub wall_mousedown(ByVal sender As Object, ByVal e As MouseEventArgs) Dim wall As PictureBox = CType(sender, PictureBox) AddHandler wall.MouseMove, AddressOf wall_mousemove If drag = True Then drag = False Label1.Text = "" Else drag = True End If End Sub Private Sub wall_mousemove(ByVal sender As Object, ByVal e As MouseEventArgs) If drag = True Then Dim wall As PictureBox = CType(sender, PictureBox) wall.Location = New Point(MousePosition.X - 20, MousePosition.Y - 10) End If End Sub End Class
the problems are that when i move the mouse too quickly, it goes outside the picturebox, and the wall_mousemove event stops firing off. Also, it does not distinguish what wall to move, so it moves whatever the mouse is over.
-
Mar 21st, 2010, 07:48 PM
#16
Junior Member
Re: Dynamic Pictureboxs
The MouseMove event should only affect the controls that it is attached to, so I don't know why it would be moving other things.... Make sure the "sender" value matches the control that you want to move, if not, then obviously it wont move the correct control.
Try attaching the mousemove event along with the mousedown event:
AddHandler wall.MouseDown, AddressOf wall_mousedown
AddHandler wall.MouseMove, AddressOf wall_mousemove
Also, try using another method for moving the item, to avoid the out-of-bounds mouse over problem. Reference this recent post for help with that: http://www.vbforums.com/showthread.php?t=608131
Does this help you at all?
-
Mar 21st, 2010, 09:16 PM
#17
Thread Starter
Lively Member
Re: Dynamic Pictureboxs
hmm... a fix for my problems would be somehow clipping the mouseposition when drag = true, to the wall bounds. now, i need to be able to get the location and size of the wall i click on, then use something like a timer to check if the mouseposition is intersecting with the wall.
EDIT:
alright i fixed it. now i need help with one last problem. When im dragging a picturebox, and drag it over another picturebox, the one that im dragging swaps to the one that i mouse over. any way around this would help!
Last edited by manbearpig001; Mar 21st, 2010 at 09:35 PM.
-
Mar 22nd, 2010, 09:49 PM
#18
Junior Member
Re: Dynamic Pictureboxs
Hey manbear, do you mind posting your updated code? For my own benefit, I'd like to know how you're handling the "snapping" to the wall bounds, but it would help me to help you resolve the last problem also. There is probably a couple simple ways to fix your problem...
Set a global variable for the control that is clicked.
So, on your mousedown event, add something like:
itemSelected = CType(sender, PictureBox)
Then on your mousemove event, move your control based of the "itemSelected" value, instead of relying on the "sender" value.
You could also just make the current selected control the front-most item, so that the mousemove should only affect that item (I think):
wall.BringToFront()
Hope this helps you!
-
Mar 22nd, 2010, 09:56 PM
#19
Junior Member
Re: Dynamic Pictureboxs
By the way, this is the program I made recently learning to build and move custom controls....
Code:
Dim Moveable As Boolean
Dim LastPos As Point
Dim newItemID As Integer
Dim itemSelect As Integer
Dim pbArray As New ArrayList
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
newItemID = newItemID + 1
Dim pb As PictureBox
pb = New PictureBox
pb.Name = newItemID
pb.Location = Me.PointToClient(Cursor.Position)
pb.Tag = newItemID
pb.BorderStyle = 2
AddHandler pb.MouseDown, AddressOf ClickPictureMouseDown
AddHandler pb.MouseUp, AddressOf ClickPictureMouseUp
AddHandler pb.MouseMove, AddressOf ClickPictureMouseMove
Controls.Add(pb)
End Sub
Private Sub ClickPictureMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
itemSelect = DirectCast(sender, PictureBox).Tag.ToString
Moveable = True
Dim pos As Point = Cursor.Position
pos.Offset(-DirectCast(sender, PictureBox).Location.X, -DirectCast(sender, PictureBox).Location.Y)
LastPos = pos
DirectCast(sender, PictureBox).BringToFront()
If e.Button = MouseButtons.Right Then
Controls.Remove(DirectCast(sender, PictureBox))
End If
End Sub
Private Sub ClickPictureMouseUp(ByVal sender As Object, ByVal e As EventArgs)
Moveable = False
End Sub
Private Sub ClickPictureMouseMove(ByVal sender As Object, ByVal e As EventArgs)
If Moveable Then
Dim pos As Point = Cursor.Position
pos.Offset(-LastPos.X, -LastPos.Y)
DirectCast(sender, PictureBox).Location = pos
End If
End Sub
This creates a new picturebox each time the user clicks on the form.
It allows the user to click on any added picturebox and darg it around.
Seems so simple, but I was quite satisfied when it worked!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|