| | |
click events for rutime generated controls
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 22
Reputation:
Solved Threads: 1
hi,
Can any1 help me...
problem:
I hav 2 create some buttons and labels during runtime...(already done)..
nw i wnt to fire sm events for each such generated controls...
main objective is to pass argumnts to a function that create a new form n pass the value to it,,,
just like generating a form with friends name when we click frinds name on another page(eg: Gtalk)
Can any1 help me...
problem:
I hav 2 create some buttons and labels during runtime...(already done)..
nw i wnt to fire sm events for each such generated controls...
main objective is to pass argumnts to a function that create a new form n pass the value to it,,,
just like generating a form with friends name when we click frinds name on another page(eg: Gtalk)
Last edited by peter_budo; 17 Days Ago at 4:56 am. Reason: Removing quote, not necessary
0
#2 19 Days Ago
Try it,
C# Syntax (Toggle Plain Text)
private void Form1_Load(object sender, EventArgs e) { Button b = new Button(); b.Click += new EventHandler(b_Click); } void b_Click(object sender, EventArgs e) { // put your code }
Failure is not fatal, but failure to change might be. - John Wooden
0
#4 19 Days Ago
What values do you need to pass? Unless you right a custom event handler, the click event has a defined argument list.
You can check which button was clicked inside the event handler by converting Sender back to a button:
Alternaively, if each button has different behaviour then give each one a different handler:
You can check which button was clicked inside the event handler by converting Sender back to a button:
C# Syntax (Toggle Plain Text)
Button btnClicked = (Button)sender; if(btnClicked.Name=="Button1") //do something
Alternaively, if each button has different behaviour then give each one a different handler:
C# Syntax (Toggle Plain Text)
private void Form1_Load(object sender, EventArgs e) { Button b1 = new Button(); b1.Click += new EventHandler(b1_Click); Button b2 = new Button(); b2.Click += new EventHandler(b2_Click); } void b1_Click(object sender, EventArgs e) { // put your code } void b2_Click(object sender, EventArgs e) { // put your code }
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
•
•
Join Date: Oct 2009
Posts: 22
Reputation:
Solved Threads: 1
im building windows application
it generate buttons during run time
code:
nw hw can i write click events b4 runtime....
it will show error since no such buttons are defined???
it generate buttons during run time
code:
C# Syntax (Toggle Plain Text)
int x=50,y=50; for(int i=0;i<10;i++) { Button field = new Button(); ActiveForm.Controls.Add(field); field.Name = "button"+i.ToString(); field.Text = i.ToString(); field.Size = new Size(100,23); field.Location = new Point(x, y); y = y +30; }
nw hw can i write click events b4 runtime....
it will show error since no such buttons are defined???
Last edited by peter_budo; 17 Days Ago at 4:58 am. Reason: Removing quote tags
1
#7 19 Days Ago
I think we have some thread crossing going on here cos that code you posted came from another thread i've replied to :p
I'm not entirely sure what you are asking though. You said you create 10 buttons, but now you say you dont know how many buttons will be created. My response to the other thread shows how to create a runtime-determined number of buttons. The code here shows how to add event handlers.
Can you please be more specific about what you need clarifying?
I'm not entirely sure what you are asking though. You said you create 10 buttons, but now you say you dont know how many buttons will be created. My response to the other thread shows how to create a runtime-determined number of buttons. The code here shows how to add event handlers.
Can you please be more specific about what you need clarifying?
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
•
•
Join Date: Oct 2009
Posts: 22
Reputation:
Solved Threads: 1
k... here it is wat i wnt to do...
im working on a lan chatting system..
there r 10 systems on a LAN
i detect all computers on the Lan....
if they are available(on) i wil create corresponding button...
on clicking that i wil connected to that particular system...
so there sud be events to check which button is clicked...
and so wil b the corresponding action..
these buttons r created dynamically(based on no of computers available)... co i cnt name thm in advnce... thats y used a loop....
nw if the no of comp in Lan incrases i wil create more buttons n the same process for each one...
hav u got wat i mean...
pls help....
im working on a lan chatting system..
there r 10 systems on a LAN
i detect all computers on the Lan....
if they are available(on) i wil create corresponding button...
on clicking that i wil connected to that particular system...
so there sud be events to check which button is clicked...
and so wil b the corresponding action..
these buttons r created dynamically(based on no of computers available)... co i cnt name thm in advnce... thats y used a loop....
nw if the no of comp in Lan incrases i wil create more buttons n the same process for each one...
hav u got wat i mean...
pls help....
Last edited by peter_budo; 17 Days Ago at 5:02 am. Reason: Removing quote tag
0
#9 18 Days Ago
I posted this in a similar thread:
You can adapt this to your requirements. Load the currently active clients into an array/collection then use the length of the collection to determine the number iterations in the loop
C# Syntax (Toggle Plain Text)
string[] Descriptions = { "One", "Two", "Another Description", "Desc." }; for( int i = 1; i<=Descriptions.Length;i++) { Button btn = new Button(); btn.Name = "btn" + i.ToString(); //btn1, btn2, etc btn.Text = Descriptions[i - 1]; panel1.Controls.Add(btn); }
You can adapt this to your requirements. Load the currently active clients into an array/collection then use the length of the collection to determine the number iterations in the loop
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
![]() |
Similar Threads
- Handler attached to Button.Click inside Gridview not firing (ASP.NET)
- Handing OnChange and OnEnter events for forms with many controls (Pascal and Delphi)
- Button Click for dynamic controls (ASP)
- Handling Browser Click Events (VB.NET)
- hangman revised (VB.NET)
- Controls on form disappears when auto-generated controls code modified (C++)
- Big Problem, Generic Error (VB.NET)
Other Threads in the C# Forum
- Previous Thread: SMS Sender in C#.NET
- Next Thread: using INI(setting) file in c#
| Thread Tools | Search this Thread |
.net 2007 access activedirectory algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion cryptographyc#winformsencryption csharp custom database datagrid datagridview dataset date datetime degrees development disabled displayingopenforms draganddrop drawing encryption enum eventcloseformc# excel file foreach form format forms ftp function gdi+ image index index-error input install java label list listbox listener listview load mandelbrot math mathematics mouseclick mysql operator path photoshop picturebox pixelinversion post prime programming radians regex remoting richtextbox security server setup sleep socket sql statistics stream string table text textbox thread time timer totaldays update user usercontrol validation visual visualstudio webbrowser windows winforms wpf xml






