Results 1 to 3 of 3

Thread: [RESOLVED] Add Label At Mouse Position

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Resolved [RESOLVED] Add Label At Mouse Position

    I am trying to add a label just where my mouse is using a context menu "Add Label" but it goes to a different position. How can i do this. Below is my code.
    VB Code:
    1. Private Sub cmnuAddLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmnuAddLabel.Click
    2.         Dim lbl As Label
    3.         lbl = New Label
    4.         lbl.Text = "My Lable"
    5.         AddHandler lbl.MouseDown, AddressOf MyEventHandler
    6.         lbl.Location = New System.Drawing.Point(Control.MousePosition.X, Control.MousePosition.Y)
    7.         Me.Controls.Add(lbl)
    8.     End Sub
    Last edited by maps; Aug 25th, 2006 at 04:31 AM.

  2. #2
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Add Label At Mouse Position

    You need to capture the mouse position when you initially click the button, try this code.

    VB Code:
    1. Public Class Form2
    2.    
    3. Dim pt As Point
    4.  
    5.     Private Sub cmnuAddLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmnuAddLabel.Click
    6.         Dim lbl As Label
    7.         lbl = New Label
    8.         lbl.Text = "My Lable"
    9.         AddHandler lbl.MouseDown, AddressOf MyEventHandler
    10.         lbl.Location = pt
    11.         Me.Controls.Add(lbl)
    12.     End Sub
    13.  
    14.     Public Sub MyEventHandler(ByVal Sender As Object, ByVal e As MouseEventArgs)
    15.         ' your event code here
    16.     End Sub
    17.  
    18.     Private Sub Form2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    19.         pt = New Point(e.X, e.Y)
    20.     End Sub
    21. End Class

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: Add Label At Mouse Position

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width