473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 5712
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: jklimek | last post by:
I have something like this: <table name="test" id="test"> <tr> <td><input type="button" onClick="showMeParentTableName();"></td> </tr> </table> How can I have my input button show me the...
8
by: drillbit_99 | last post by:
I have an HTML page of thumbnail images. Each image can begin a slideshow of the images on the page by clicking on the image. This opens another HTML page that begins the slideshow using large...
7
by: Stefan Mueller | last post by:
How can I set the Name of a button? I tried xelement = document.createElement("input") xelement.type = "button" xelement.name = "MyButton" but it does not work with the Internet Explorer...
1
by: aps-asia | last post by:
I have one WinForm and 3 buttons on this form. all buttons are private. I set the button2's click event to execute the "Controls.RemoveByKey("button1");". When I executed the button2's click...
2
by: Yoshitha | last post by:
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...
11
by: Jasbird | last post by:
Has the name attribute deprecated? I ask this because ASP.NET 2 warns me against using it, says that it has been deprecated and doesn't use it (on the client) when creating a radio button list....
7
by: John Smith | last post by:
Hello, I have a simple question, I have a vb.net form with several buttons. If I store the name of a button in a variable.. Dim TheName as string TheName = ...
10
by: Rob | last post by:
I am reading a book that says that the "name" property can be altered only at design time and cannot be modified at runtime. Please explain this given the code below... If you click Button3......
3
by: franc sutherland | last post by:
Hello, I have a report which I filter using the me.filter command in the OnOpen event. Me.Filter = "OrderID=" & Forms!variable_form_name! Me.FilterOn = True I want to be able to open that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.