Destinquishing between controls

J

jcrouse

I have a test app with one button and a context menu. When I click on the
button, it adds a label with some properties, one being the assignment of
the context menu. If I add 5 buttons then want to delete one I can right
click and choose Delete from the context menu. The problems is that I don't
know the proper verbage to search for (in help) to find the code to tell
which label is the one the received the right click event. I'm not sure if
sender is the proper word but I need to know which label is the sender.
Also, how will I the reference the individual labels is code? What will
their names be (or how can I assign a unique name to each label)? I don't
really want to use a loop because I may add a couple of labels then later
add a couple of more and so on (or possibly delete some). I hope this makes
sense. Here is my code.

Dim intTopCounter As Integer = 20

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim MyLabel As New Label

MyLabel.Text = intTopCounter.ToString

MyLabel.TextAlign = ContentAlignment.MiddleCenter

MyLabel.BackColor = BackColor.Black

MyLabel.ForeColor = ForeColor.White

MyLabel.Top = intTopCounter

MyLabel.Left = 40

MyLabel.ContextMenu = cmnuLabels

intTopCounter = intTopCounter + 30

Me.Controls.Add(MyLabel)

End Sub

Private Sub cmDelete_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmDelete.Click

'Delete Label Code Here

End Sub


Thanks,
John
 
C

Cor Ligthert

John,

You mean this?
\\\
Me.Controls.Remove(Me.ActiveControl)
///
I hope this helps?
Cor
 
J

jcrouse

Hey Cor....Good to hear from you again. Well, that's kind of correct,
however when I click on the button to add the labels the button remains the
active control. Then when I right click on a label and try to remove it, the
button gets removed.

Thanks,
John
 
C

Cor Ligthert

John,

I think that it is because the label get never focus.

I once made this sample special for your however never got any reaction.

You can try it again, because a sample how to do the things you ask now and
some more things I have in added in it.

The only thing it needs is open a new project, delete all code and paste
this what is below in.

I hope this helps.

Cor

\\\
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu
Friend WithEvents mnuAdd As System.Windows.Forms.MenuItem
Friend WithEvents mnuRemove As System.Windows.Forms.MenuItem
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.myToolTip = New System.Windows.Forms.ToolTip(Me.components)
Me.ContextMenu1 = New System.Windows.Forms.ContextMenu
Me.mnuAdd = New System.Windows.Forms.MenuItem
Me.mnuRemove = New System.Windows.Forms.MenuItem
Me.ContextMenu1.MenuItems.AddRange(New
System.Windows.Forms.MenuItem() _
{Me.mnuAdd, Me.mnuRemove})
Me.mnuAdd.Index = 0
Me.mnuAdd.Text = "Add"
Me.mnuRemove.Index = 1
Me.mnuRemove.Text = "Remove"
Me.Name = "Form1"
End Sub
Private components As System.ComponentModel.IContainer
Private mouseX, mouseY As Integer
Private arLabels As New ArrayList
Private myToolTip As ToolTip
Private createdLabels As Integer
Private myMousedown As String
Private Lastlabel As Label
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
CreateLabel("See Tooltip")
End Sub
Private Sub CreateLabel(ByVal LabelText As String)
Me.ClientSize = New System.Drawing.Size(400, 400)
Dim lbl As New Label
createdLabels = +1
lbl.Name = "Label" & createdLabels.ToString
Dim lblY As Integer = 100
lbl.Location = New System.Drawing.Point(100, lblY)
lbl.ForeColor = Color.Red
lbl.BackColor = Color.Transparent
lbl.TextAlign = ContentAlignment.MiddleCenter
lbl.Text = LabelText
lbl.ContextMenu = ContextMenu1
AddHandler lbl.MouseDown, AddressOf Label_MouseDown
AddHandler lbl.MouseUp, AddressOf Label_MouseUp
AddHandler lbl.MouseMove, AddressOf Label_MouseMove
lblY += 30
Me.Controls.Add(lbl)
lbl.BringToFront()
End Sub
Private Sub Label_MouseDown(ByVal sender As Object, ByVal _
e As System.Windows.Forms.MouseEventArgs)
Lastlabel = DirectCast(sender, Label)
myMousedown = Lastlabel.Name
Lastlabel.BringToFront()
mouseX = Cursor.Position.X - Lastlabel.Location.X
mouseY = Cursor.Position.Y - Lastlabel.Location.Y
Lastlabel.Cursor = Cursors.Hand
End Sub
Private Sub Label_MouseUp(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs)
Dim lbl As Label = DirectCast(sender, Label)
myMousedown = ""
lbl.Cursor = Cursors.Default
End Sub
Private Sub Label_MouseMove(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs)
Dim lbl As Label = DirectCast(sender, Label)
Static LastCursor As Point
Dim NowCursor As Point = New Point(Cursor.Position.X,
Cursor.Position.Y)
If Point.op_Inequality(NowCursor, LastCursor) Then
If myMousedown = lbl.Name Then
lbl.Location = New System.Drawing.Point(Cursor.Position.X _
- mouseX, Cursor.Position.Y - mouseY)
End If
LastCursor = Cursor.Position
lbl.Text = "Right Click me"
myToolTip.SetToolTip(lbl, _
lbl.Location.X.ToString & "." & lbl.Location.Y.ToString)
End If
End Sub
Private Sub mnuAdd_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles mnuAdd.Click
CreateLabel("Move me away")
End Sub
Private Sub mnuRemove_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles mnuRemove.Click
Me.Controls.Remove(Lastlabel)
End Sub
End Class
///
 
H

Herfried K. Wagner [MVP]

* "jcrouse said:
I have a test app with one button and a context menu. When I click on the
button, it adds a label with some properties, one being the assignment of
the context menu. If I add 5 buttons then want to delete one I can right
click and choose Delete from the context menu. The problems is that I don't
know the proper verbage to search for (in help) to find the code to tell
which label is the one the received the right click event.

Take a look at the context menu's 'SourceControl' property. This
property will contain a reference to the control the context menu was
shown on.
 
J

jcrouse

Well Cor, I got your code working great. Thanks. I do have a question
though. I also use the mouse_enter and mouse_leave events for each of my
label. Since the labels usually have the same backcolor as the background,
on mouse_enter I write the current backcolor to a variable then change it to
something different (it makes it easier to find the edges to resize). Then
on mouse_leave I set the original backcolor back by retreiving it from the
variable.
I tried adding the following handlers:
AddHandler lbl.MouseEnter, AddressOf Label_MouseEnter

AddHandler lbl.MouseLeave, AddressOf Label_MouseLeave

However, I get error something about deligates not being the same. How do I
add the handlers the use them in the subs?

Thanks and hope to hear from you,
John
 
J

jcrouse

WOW, I think I got it. Only took 2 hours of trying. See, some of us are
fffaaarrrr below your level.

LOL,
John
 
C

Cor Ligthert

John,

2 hours learing time, what is wrong with that.

I looked again at the sample and changed it again a little bit, there is
some orphaned code in it. However I think that will be in every program.

With persons as you I "had" to answer in an hour.

(What I do in those situations you ask is set a label on a form, create the
event with the IDE and than remove the handler in the by the IDE created
code, I am lazy as well)

:)

Cor

\\\
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu
Friend WithEvents mnuAdd As System.Windows.Forms.MenuItem
Friend WithEvents mnuRemove As System.Windows.Forms.MenuItem
Private Sub InitializeComponent()
components = New System.ComponentModel.Container
myToolTip = New System.Windows.Forms.ToolTip(Me.components)
ContextMenu1 = New System.Windows.Forms.ContextMenu
mnuAdd = New System.Windows.Forms.MenuItem
mnuRemove = New System.Windows.Forms.MenuItem
ContextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem()
_
{Me.mnuAdd, Me.mnuRemove})
Me.mnuAdd.Text = "Add"
Me.mnuRemove.Text = "Remove"
End Sub
Private components As System.ComponentModel.IContainer
Private mouseX, mouseY As Integer
Private myToolTip As ToolTip
Private LabelMouseDown As Label
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
CreateLabel("See Tooltip")
End Sub
Private Sub CreateLabel(ByVal LabelText As String)
Me.ClientSize = New System.Drawing.Size(400, 400)
Dim lbl As New Label
lbl.Location = New System.Drawing.Point(100, 100)
lbl.ForeColor = Color.Red
lbl.BackColor = Color.Transparent
lbl.TextAlign = ContentAlignment.MiddleCenter
lbl.Text = LabelText
lbl.ContextMenu = ContextMenu1
AddHandler lbl.MouseDown, AddressOf Label_MouseDown
AddHandler lbl.MouseUp, AddressOf Label_MouseUp
AddHandler lbl.MouseMove, AddressOf Label_MouseMove
Me.Controls.Add(lbl)
lbl.BringToFront()
End Sub
Private Sub Label_MouseDown(ByVal sender As Object, ByVal _
e As System.Windows.Forms.MouseEventArgs)
LabelMouseDown = DirectCast(sender, Label)
LabelMouseDown = LabelMouseDown
LabelMouseDown.BringToFront()
mouseX = Cursor.Position.X - LabelMouseDown.Location.X
mouseY = Cursor.Position.Y - LabelMouseDown.Location.Y
LabelMouseDown.Cursor = Cursors.Hand
End Sub
Private Sub Label_MouseUp(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs)
Dim lbl As Label = DirectCast(sender, Label)
LabelMouseDown = Nothing
lbl.Cursor = Cursors.Default
End Sub
Private Sub Label_MouseMove(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs)
Dim lbl As Label = DirectCast(sender, Label)
Static LastCursor As Point
Dim NowCursor As Point = New Point(Cursor.Position.X,
Cursor.Position.Y)
If Point.op_Inequality(NowCursor, LastCursor) Then
If LabelMouseDown Is lbl Then
lbl.Location = New System.Drawing.Point(Cursor.Position.X _
- mouseX, Cursor.Position.Y - mouseY)
End If
LastCursor = Cursor.Position
lbl.Text = "Right Click me"
myToolTip.SetToolTip(lbl, _
lbl.Location.X.ToString & "." & lbl.Location.Y.ToString)
End If
End Sub
Private Sub mnuAdd_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles mnuAdd.Click
CreateLabel("Move me away")
End Sub
Private Sub mnuRemove_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles mnuRemove.Click
Me.Controls.Remove(ContextMenu1.SourceControl)
End Sub
End Class
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top