Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2009-06-29
Medium Priority
Last Modified: 2012-05-07
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
Question by:Dov_B
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 24736975
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.
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 24737828
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
        lbl.Parent = PictureBox1
    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


Author Comment

ID: 24738127
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
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
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 2000 total points
ID: 24738159
"...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...   =)

Author Closing Comment

ID: 31597946
woah!!! you did it again !! Thankyou! I have a couple of related questions if you get a chance

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question