How can I make text so that I can drag it with the mouse at run time and place it on different parts of a picture box

How can I drag text on to a picture box so that the user can label different parts of the picture without typing vb.net 2008 express
Dov_BAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DeathraceCommented:
Hi,
You said text, which control label or textbox you are using. Right now i can suggest one link as per my understanding. Check the below one.
http://www.codeproject.com/KB/cpp/vbnetdragdrop.aspx
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Instead of drawing the text in the Paint() event, we could create actual Label controls and add them to the PictureBox:
Public Class Form1
 
    Public Const HTCAPTION As Integer = &H2
    Public Const WM_NCLBUTTONDOWN As Integer = &HA1
 
    Public Declare Auto Function ReleaseCapture Lib "user32.dll" () As Integer
    Public Declare Ansi Function DragControl Lib "user32.dll" Alias "SendMessageA" (ByVal handle As IntPtr, Optional ByVal Message As Integer = WM_NCLBUTTONDOWN, Optional ByVal HitTest As Integer = HTCAPTION, Optional ByVal wParam As Integer = 0) As Integer
 
    Private startPt As Point
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        NewLabel("Some Thing", New Point(25, 100))
        NewLabel("Another Thing", New Point(150, 100))
        NewLabel("Thing One", New Point(250, 300))
    End Sub
 
    Private Sub NewLabel(ByVal txt As String, ByVal location As Point)
        Dim lbl As New Label
        lbl.Text = txt
        lbl.Location = location
        lbl.BackColor = Color.Transparent
        AddHandler lbl.MouseDown, AddressOf lbl_MouseDown
        AddHandler lbl.MouseMove, AddressOf lbl_MouseMove
        Me.Controls.Add(lbl)
        lbl.Parent = PictureBox1
        lbl.BringToFront()
    End Sub
 
    Private Sub lbl_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            startPt = New Point(e.X, e.Y)
        End If
    End Sub
 
    Private Sub lbl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim lbl As Label = CType(sender, Label)
            lbl.Location = New Point(lbl.Location.X + (e.X - startPt.X), lbl.Location.Y + (e.Y - startPt.Y))
        End If
    End Sub
 
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dov_BAuthor Commented:
extremely cool
say before I code the project I dont know how many labels I will need is there a way of dynamicly adding these labels at run time?
also is there a way I can do the same dragging of the labels around the picture box
but drag around smaller pictures I asked this question here
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_24530191.html#a24737976
someone sent me links but I am having a heck of a time figuring out how to follow those projects is there a way of doing it the same way as the tex
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
"...is there a way of dynamicly adding these labels at run time?"

Yes...I already took that into account.  Just call NewLabel() whenever you want a new label...

"...is there a way I can do the same dragging of the labels around the picture box...but drag around smaller pictures..."

Yes...you can do the exact same thing I did with Labels but with PictureBoxes.  Just create them at run-time as I did and wire up the MouseDown()/MouseMove() events.

Study the NewLabel() method to see what I'm doing in there...   =)
0
Dov_BAuthor Commented:
woah!!! you did it again !! Thankyou! I have a couple of related questions if you get a chance
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.