GDI+ Create TextBox with text and be able to Drag Drop around pictureBox
Based on a sample found on Mr. John McIlhinney's .NET Developer Blog I'm triyng to modify the sample to achieve this :
- Create Rectangle on a PictureBox via GDI+ and inside a textbox with user desired text. Also, and more important, the user must be able to drag drop/move the Box throught the image.
https://docs.google.com/open?id=0By3...GE5Q0pKWmZBMEE
Here is my code, modifying the sample wit a class called TxRectangle... But the drag drop does'nt works...any help please ???
Thanks to Mr. for your help and advices.
Frank
Re: GDI+ Create TextBox with text and be able to Drag Drop around pictureBox
Just curious, is this homework?
Re: GDI+ Create TextBox with text and be able to Drag Drop around pictureBox
Quote:
But the drag drop does'nt works
Take a look at JMcilhinney's submission on drag and drop in window's forms here
Unfortunately, I personally do NOT download any files. I suggest that you post the relevant code that you're having troubles with.
Re: GDI+ Create TextBox with text and be able to Drag Drop around pictureBox
Not homework, this is a sample project from Mr JMcilhinney's blog. I ve modified it.
I can asure is free virus code. The problem with GDI is that drag and drop is not easy as when you do that with CONTROLS.
Any ideas pleasE ??
Thanks in advance to all for your answers and time dedicated.
Re: GDI+ Create TextBox with text and be able to Drag Drop around pictureBox
Instead of using a List(Of Rectangle) as I do in my blog post, define your own class that has properties for the Rectangle with which to bound the text, the String containing the text and any other formatting information you want to support. You then do everything as I did before but you work with the Rectangle property of each item rather than each item itself being a Rectangle.
Re: GDI+ Create TextBox with text and be able to Drag Drop around pictureBox
Dear Jm.
Is what i ¡ve done on my sample uploaded, as you can see there is a class called TxRectangle.
Code:
Public Class TxRectangle
Private m_rec As Rectangle
Private m_Tx As String
Public Property Rec As Rectangle
Get
Return m_rec
End Get
Set(ByVal value As Rectangle)
m_rec = value
End Set
End Property
Public Property Tx As String
Get
Return m_Tx
End Get
Set(ByVal value As String)
m_Tx = value
End Set
End Property
End Class
All works fine, except the event :
Code:
Private Sub PictureBox1_MouseMove(ByVal sender As Object, _
ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If Control.MouseButtons = Windows.Forms.MouseButtons.Left Then
If Me.drawMode Then
Me.InvalidateRectangle(Me.GetRectangle(Me.startPoint, Me.endPoint))
Me.endPoint = e.Location
Me.InvalidateRectangle(Me.GetRectangle(Me.startPoint, Me.endPoint))
Me.PictureBox1.Update()
Code:
ElseIf Me.selectedBoxIndex <> -1 Then
Dim box As Rectangle = Me.txboxes(Me.selectedBoxIndex).Rec
Dim location As Point = e.Location
Me.InvalidateRectangle(Me.txboxes(Me.selectedBoxIndex))
box.Offset(location.X - Me.startPoint.X, _
location.Y - Me.startPoint.Y)
' Me.txboxes(Me.selectedBoxIndex) = box
'txboxes(Me.selectedBoxIndex).Rec.Offset(location.X - Me.startPoint.X, _
' location.Y - Me.startPoint.Y)
Me.startPoint = location
Me.InvalidateRectangle(Me.txboxes(Me.selectedBoxIndex))
Me.PictureBox1.Update()
End If
End If
End Sub
There are no errors, but the rectangle is not dragged.... PLEASE... please.... hheeeeeellllp sir !!
Frank
Re: GDI+ Create TextBox with text and be able to Drag Drop around pictureBox
Rectangle is a value type, which means that a variable of that type contains the object itself rather than a reference to the object. As such, when you assign a value from one variable to another, you are creating a copy of the object. Here:
Code:
Dim box As Rectangle = Me.txboxes(Me.selectedBoxIndex).Rec
you are making such an assignment so 'box' contains a copy of the Rectangle. That means that here:
Code:
box.Offset(location.X - Me.startPoint.X, location.Y - Me.startPoint.Y)
you are offsetting the copy, which has no effect on the original. Your mistake was commenting out this line:
Code:
' Me.txboxes(Me.selectedBoxIndex) = box
That line copies the copy, which has been offset, back over the original. That whole section of code should loon like this:
Code:
ElseIf Me.selectedBoxIndex <> -1 Then
Dim txbox As TxRectangle = Me.txboxes(Me.selectedBoxIndex)
Dim box As Rectangle = txbox.Rec
Dim location As Point = e.Location
Me.InvalidateRectangle(box)
box.Offset(location.X - Me.startPoint.X, _
location.Y - Me.startPoint.Y)
txbox.Rec = box
Me.startPoint = location
Me.InvalidateRectangle(box)
Me.PictureBox1.Update()
End If
End If
End Sub
Re: GDI+ Create TextBox with text and be able to Drag Drop around pictureBox
YES !! !! Finally achieved !!!!
Thanks to all !!!!!
One more thing i spossible ??
I would like to RESIZE a drawn box.... I know if a box is clicked, but how to "resize it " ??
Re: GDI+ Create TextBox with text and be able to Drag Drop around pictureBox
A big part of programming is using things that you already know in new situations, and this is no different. You already have all you need. To move a box you have to detect a mouse event, determine where it is, if it is on a box and which box it is, then redraw the box with its new bounds. What you're asking for is exactly the same pattern. The only difference will be that the Size of the new Rectangle will be different instead of or as well as the Location, where in the existing example only the Location is different.