473,221 Members | 1,971 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,221 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 5707
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.