Adding event handler in C# ?

R

Rich P

My actual problem is in casting the sender object

In VB.Net I can handle control events for multiple controls like this:

-------------------------------------------
Dim cnt() As Control
cnt = New Control(){btn1, btn2, combo1, combo2}
For Each ct As Control in cnt
AddHandler ct.Click, AddressOf ClickIt()
Next

Private Sub ClickIt(ByVal sender As System.Object, ByVal e As
System.EventArgs)

Dim str1 As String = CType(sender, Control).Name
Console.WriteLine(str1)

End Sub
--------------------------------------------


I believe in C# I can do this

Control cnt[] = New Control(){btn1, btn2, combo1, combo2};
foreach Control cn In cnt
AddHandler cn.Click, AddressOf ClickIt();
next

private void ClickIt(object sender, EventArgs e)
{
String str1 = ? // how to cast sender?

}

Is this the correct way to add event handlers in C#? How to cast the
sender object?

Thanks,

Rich
 
Z

z.sessions

My actual problem is in casting the sender object

In VB.Net I can handle control events for multiple controls like this:

-------------------------------------------
Dim cnt() As Control
cnt = New Control(){btn1, btn2, combo1, combo2}
For Each ct As Control in cnt
   AddHandler ct.Click, AddressOf ClickIt()
Next

Private Sub ClickIt(ByVal sender As System.Object, ByVal e As
System.EventArgs)

Dim str1 As String = CType(sender, Control).Name
Console.WriteLine(str1)

End Sub
--------------------------------------------

I believe in C# I can do this

Control cnt[] = New Control(){btn1, btn2, combo1, combo2};
foreach Control cn In cnt
  AddHandler cn.Click, AddressOf ClickIt();

cn.Click += new System.EventHandler(this.ClickIt);
next

private void ClickIt(object sender, EventArgs e)
{
  String str1 = ? // how to cast sender?

string str1 = ((Control)(sender)).Name;
}

Is this the correct way to add event handlers in C#?  How to cast the
sender object?

Thanks,

Rich

*** Sent via Developersdexhttp://www.developersdex.com***

Check out a form designer module that already has controls with events
for more clues.
 
P

Pavel Minaev

Rich said:
My actual problem is in casting the sender object

In VB.Net I can handle control events for multiple controls like this:

-------------------------------------------
Dim cnt() As Control
cnt = New Control(){btn1, btn2, combo1, combo2}
For Each ct As Control in cnt
AddHandler ct.Click, AddressOf ClickIt()
Next

Private Sub ClickIt(ByVal sender As System.Object, ByVal e As
System.EventArgs)

Dim str1 As String = CType(sender, Control).Name
Console.WriteLine(str1)

End Sub
--------------------------------------------


I believe in C# I can do this

Control cnt[] = New Control(){btn1, btn2, combo1, combo2};
foreach Control cn In cnt
AddHandler cn.Click, AddressOf ClickIt();
next

private void ClickIt(object sender, EventArgs e)
{
String str1 = ? // how to cast sender?

}

Is this the correct way to add event handlers in C#? How to cast the
sender object?

http://msdn.microsoft.com/en-us/library/ms366768.aspx
http://msdn.microsoft.com/en-us/library/0z4503sa.aspx
 
C

Cor Ligthert[MVP]

Rich,

To avoid that we look at two different pieces of code, I have also made your
VB code to VB9, beneath their you see an equivalent for C# 3

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim btn1, btn2 As Button
Dim combo1, combo2 As ComboBox
Dim cnt() As Control = {btn1, btn2, combo1, combo2}
For Each ct As Control In cnt
AddHandler ct.Click, AddressOf Ct_Click
Next
End Sub
Private Sub Ct_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim str1 = DirectCast(sender, Control).Name
Console.WriteLine(str1)
End Sub
///
\\\
private void Form1_Load(object sender, EventArgs e)
{
Button btn1 = new Button(), btn2 = new Button();
ComboBox combo1 = new ComboBox(), combo2 = new ComboBox();
Control[] cnt = {btn1,btn2,combo1,combo2};
foreach (Control ct in cnt)
{
ct.Click += new System.EventHandler(ct_Click);
}
}
private void ct_Click(object sender, EventArgs e)
{
var str1 = (sender as Control).Text;
Console.WriteLine(str1);
}
///

Cor
 
T

Tom Shelton

Rich,

To avoid that we look at two different pieces of code, I have also made your
VB code to VB9, beneath their you see an equivalent for C# 3

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim btn1, btn2 As Button
Dim combo1, combo2 As ComboBox
Dim cnt() As Control = {btn1, btn2, combo1, combo2}
For Each ct As Control In cnt
AddHandler ct.Click, AddressOf Ct_Click
Next
End Sub
Private Sub Ct_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim str1 = DirectCast(sender, Control).Name
Console.WriteLine(str1)
End Sub
///
\\\
private void Form1_Load(object sender, EventArgs e)
{
Button btn1 = new Button(), btn2 = new Button();
ComboBox combo1 = new ComboBox(), combo2 = new ComboBox();
Control[] cnt = {btn1,btn2,combo1,combo2};
foreach (Control ct in cnt)
{
ct.Click += new System.EventHandler(ct_Click);
}
}
private void ct_Click(object sender, EventArgs e)
{
var str1 = (sender as Control).Text;
Console.WriteLine(str1);
}
///

Cor

I think, I might make this more like...

private void Form1_Load ( object sender, EventArgs e )
{
Control[] controls =
{
new Button(){Name = "Button1", Text = "Button1", Location = new Point(4, 0)},
new Button(){Name = "Button2", Text = "Button2", Location = new Point(4, 50)},
new ComboBox(){Name = "Combobox1", Text="Combobox1", Location = new Point(4, 100)},
new ComboBox(){Name = "Cobobox2", Text = "Combobox2", Location=new Point(4, 150)}
};

Array.ForEach ( controls, control => control.Click += Control_Click );
this.Controls.AddRange ( controls );
}

private void Control_Click ( object sender, EventArgs e )
{
// probably should be testing for null conditions here :)
MessageBox.Show ( ( (Control)sender ).Text );
}
 
C

Cor Ligthert[MVP]

Yea,

But you made the same mistake as me.

It had to be

MessageBox.Show ( ( (Control)sender ).Name );

But therefore I could see my message was read

-:)

Cor

Tom Shelton said:
Rich,

To avoid that we look at two different pieces of code, I have also made
your
VB code to VB9, beneath their you see an equivalent for C# 3

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim btn1, btn2 As Button
Dim combo1, combo2 As ComboBox
Dim cnt() As Control = {btn1, btn2, combo1, combo2}
For Each ct As Control In cnt
AddHandler ct.Click, AddressOf Ct_Click
Next
End Sub
Private Sub Ct_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim str1 = DirectCast(sender, Control).Name
Console.WriteLine(str1)
End Sub
///
\\\
private void Form1_Load(object sender, EventArgs e)
{
Button btn1 = new Button(), btn2 = new Button();
ComboBox combo1 = new ComboBox(), combo2 = new ComboBox();
Control[] cnt = {btn1,btn2,combo1,combo2};
foreach (Control ct in cnt)
{
ct.Click += new System.EventHandler(ct_Click);
}
}
private void ct_Click(object sender, EventArgs e)
{
var str1 = (sender as Control).Text;
Console.WriteLine(str1);
}
///

Cor

I think, I might make this more like...

private void Form1_Load ( object sender, EventArgs e )
{
Control[] controls =
{
new Button(){Name = "Button1", Text = "Button1", Location =
new Point(4, 0)},
new Button(){Name = "Button2", Text = "Button2", Location =
new Point(4, 50)},
new ComboBox(){Name = "Combobox1", Text="Combobox1",
Location = new Point(4, 100)},
new ComboBox(){Name = "Cobobox2", Text = "Combobox2",
Location=new Point(4, 150)}
};

Array.ForEach ( controls, control => control.Click +=
Control_Click );
this.Controls.AddRange ( controls );
}

private void Control_Click ( object sender, EventArgs e )
{
// probably should be testing for null conditions here :)
MessageBox.Show ( ( (Control)sender ).Text );
}
 

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