[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:
Private Sub cmnuAddLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmnuAddLabel.Click
Dim lbl As Label
lbl = New Label
lbl.Text = "My Lable"
AddHandler lbl.MouseDown, AddressOf MyEventHandler
lbl.Location = New System.Drawing.Point(Control.MousePosition.X, Control.MousePosition.Y)
Me.Controls.Add(lbl)
End Sub
Re: Add Label At Mouse Position
You need to capture the mouse position when you initially click the button, try this code.
VB Code:
Public Class Form2
Dim pt As Point
Private Sub cmnuAddLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmnuAddLabel.Click
Dim lbl As Label
lbl = New Label
lbl.Text = "My Lable"
AddHandler lbl.MouseDown, AddressOf MyEventHandler
lbl.Location = pt
Me.Controls.Add(lbl)
End Sub
Public Sub MyEventHandler(ByVal Sender As Object, ByVal e As MouseEventArgs)
' your event code here
End Sub
Private Sub Form2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
pt = New Point(e.X, e.Y)
End Sub
End Class
Re: Add Label At Mouse Position