Groups | Blog | Home
all groups > dotnet drawing api > june 2005 >

dotnet drawing api : Moving a graphic on a picturebox


Bob Powell [MVP]
6/14/2005 12:00:00 AM
You're using the Forms events. If you want to interact with child controls
of the form you'll have to subscribe to their events individually.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]

James Westgate
6/14/2005 12:00:00 AM
1. I would use a usercontrol instead of a picturebox which encapsulates the
solution and allows for
2. Which is override the mouseevents eg OnMouseMove in the usercontrol,
which you can then use to check the rectangle against the boundaries of the
usercontrol.
3. Do all your painting in OnPaint

I think Bob has basic samples of this

www.bobpowell.net

James

--
Create interactive diagrams and flowcharts with ERM Diagram at
http://www.crainiate.net

Take the ERM Tour at http://www.flowchartcontrol.com


[quoted text, click to view]

cmac89
6/14/2005 5:12:18 AM
I'm using the following code to draw a rectangle on a form and then
move it to a new position by clicking the mouse. I would now like to
do the same thing but within a picturebox. I've added the picturebox
but the rectangle stops at the edge of the picturebox.
I can't figure out how to change this code to confine the moving
rectangle to within the picturebox

Any help would be brilliant.

Craig.

-------------------------------------------
Dim Rectangle1 As Rectangle = New System.Drawing.Rectangle(20, 100, 15,
15)

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
e.Graphics.FillRectangle(Drawing.Brushes.Black, Me.Rectangle1)
End Sub

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
Me.Invalidate(Me.Rectangle1)
Me.Rectangle1.X = e.X
Me.Rectangle1.Y = e.Y
Me.Invalidate(Me.Rectangle1)
End Sub
-------------------------------------------
cmac89
6/14/2005 1:37:27 PM
Thanks Bob and James,

After Bob's post and a couple of hours away from the machine I've got
it sorted.

Thanks again,

Craig.
AddThis Social Bookmark Button