468,249 Members | 1,470 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

how to get the name of the button creasted at run time in c#.net


Hi
I've created some buttons at runtime and all these are having unique names
and also created event handler for the buttons created at run time like this

{
btn.Size =new System.Drawing.Size(70,50);
btn.Name ="Btn"+i;
btn.Text ="Newly Created Button" + i ;
btn.Click+=new EventHandler(btn_click);
this.Controls.Add(btn);
}

//////// handling event for newly creaetd buttons

private void btn_click(object sender, EventArgs e)
{
label1.Text= sender.Name;

}

when i tried to run the application then i got error "object does not
contain a definition for 'Name' "

and the same code executed in vb.net and it is display the button which i've
clicked.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 1 To 2
Dim btn As New Button
btn.Name = "B" & i
btn.Text = "First Button"
Me.Controls.Add(btn)
AddHandler btn.Click, AddressOf btn_click

Next
End Sub
Private Sub btn_click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Label1.Text = sender.Name

End Sub

Can any one tell me how to do this in c#.net?

Thanx in advance
Jyothi
Jun 16 '06 #1
2 5584
Yoshitha wrote:
private void btn_click(object sender, EventArgs e)
{
label1.Text= sender.Name;

}
Can any one tell me how to do this in c#.net?

Thanx in advance
Jyothi


Jyothi,

You need to typecast the sender variable to an object which has a name
property. Try something like this:

if (sender is Button)
{
label1.Text = ((Button)sender).Name;
}

I think a Control object has a name too, in which case you could make it
a little more general:

if (sender is Control)
{
label1.Text = ((Control)sender).Name;
}

Hope this helps.

Dan Manges
Jun 16 '06 #2
If you're looking to write code that doesn't generate FxCop or Code-Analysis
warnings, compliant code to do the same thing would look like:

Button button = sender as Button;
if(button != null)
{
label1.Text = button.Name;
}
--
http://www.peterRitchie.com/
"Dan Manges" wrote:
Yoshitha wrote:
private void btn_click(object sender, EventArgs e)
{
label1.Text= sender.Name;

}
Can any one tell me how to do this in c#.net?

Thanx in advance
Jyothi


Jyothi,

You need to typecast the sender variable to an object which has a name
property. Try something like this:

if (sender is Button)
{
label1.Text = ((Button)sender).Name;
}

I think a Control object has a name too, in which case you could make it
a little more general:

if (sender is Control)
{
label1.Text = ((Control)sender).Name;
}

Hope this helps.

Dan Manges

Jun 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by drillbit_99 | last post: by
7 posts views Thread by Stefan Mueller | last post: by
1 post views Thread by aps-asia | last post: by
11 posts views Thread by Jasbird | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.