N
Nina
I have the code in VB with Handles event that creates clickable labels on a
form. I’m trying to write same code in C#. However it doesn’t work. Please
help me to find a solution for event Handles in C#.
VB Handles
---------------------
Public Class clsLabel
Inherits Control
Event labelClick(ByVal sender As Object, ByVal e As System.EventArgs)
Private Sub clsLabelClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
RaiseEvent labelClick(sender, e)
End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
Friend WithEvents lblEnTick As clsLabel
Private Sub InitializeLabels()
lblEnTick = New clsLabel
End Sub
Private Sub lblclick(ByVal lbl As Object, ByVal e As System.EventArgs)
Dim l As clsLabel = CType(lbl, clsLabel)
End Sub
End Class
C# Handles?
------------------------------------------
class clsLabel : Control
{
public delegate void labelClickEventHandler(object sender, System.EventArgs
e);
public event labelClickEventHandler labelClick;
private void clsLabelClick(object sender, System.EventArgs e)
{
if (labelClick != null)
labelClick(sender, e);
}
}
public partial class Form1 : System.Windows.Forms.Form
{
private void InitializeLabels()
{
lblEnTick = new clsLabel();
lblEnTick.labelClick += lblclick;
}
private void lblclick(object lbl, System.EventArgs e)
{
clsLabel l = (clsLabel)lbl;
}
}
form. I’m trying to write same code in C#. However it doesn’t work. Please
help me to find a solution for event Handles in C#.
VB Handles
---------------------
Public Class clsLabel
Inherits Control
Event labelClick(ByVal sender As Object, ByVal e As System.EventArgs)
Private Sub clsLabelClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
RaiseEvent labelClick(sender, e)
End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
Friend WithEvents lblEnTick As clsLabel
Private Sub InitializeLabels()
lblEnTick = New clsLabel
End Sub
Private Sub lblclick(ByVal lbl As Object, ByVal e As System.EventArgs)
Dim l As clsLabel = CType(lbl, clsLabel)
End Sub
End Class
C# Handles?
------------------------------------------
class clsLabel : Control
{
public delegate void labelClickEventHandler(object sender, System.EventArgs
e);
public event labelClickEventHandler labelClick;
private void clsLabelClick(object sender, System.EventArgs e)
{
if (labelClick != null)
labelClick(sender, e);
}
}
public partial class Form1 : System.Windows.Forms.Form
{
private void InitializeLabels()
{
lblEnTick = new clsLabel();
lblEnTick.labelClick += lblclick;
}
private void lblclick(object lbl, System.EventArgs e)
{
clsLabel l = (clsLabel)lbl;
}
}