click events for rutime generated controls

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 22
Reputation: bk_bhupendra is an unknown quantity at this point 
Solved Threads: 1
bk_bhupendra bk_bhupendra is offline Offline
Newbie Poster

click events for rutime generated controls

 
-2
  #1
19 Days Ago
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)
Last edited by peter_budo; 17 Days Ago at 4:56 am. Reason: Removing quote, not necessary
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,560
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 454
Moderator
adatapost's Avatar
adatapost adatapost is online now Online
Posting Maven
 
0
  #2
19 Days Ago
Try it,
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. Button b = new Button();
  4. b.Click += new EventHandler(b_Click);
  5. }
  6.  
  7. void b_Click(object sender, EventArgs e)
  8. {
  9. // put your code
  10. }
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 22
Reputation: bk_bhupendra is an unknown quantity at this point 
Solved Threads: 1
bk_bhupendra bk_bhupendra is offline Offline
Newbie Poster

click events for rutime generated controls

 
-1
  #3
19 Days Ago
sry sir, bt i hav generated 10 button and for each one i hav to fire event..
i need to pass some values too in case of each button...
hew is it possible wit ur reply

Last edited by peter_budo; 17 Days Ago at 4:57 am. Reason: Removing quote
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 306
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 55
Ryshad's Avatar
Ryshad Ryshad is online now Online
Posting Whiz
 
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:

  1. Button btnClicked = (Button)sender;
  2. if(btnClicked.Name=="Button1")
  3. //do something

Alternaively, if each button has different behaviour then give each one a different handler:

  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. Button b1 = new Button();
  4. b1.Click += new EventHandler(b1_Click);
  5. Button b2 = new Button();
  6. b2.Click += new EventHandler(b2_Click);
  7. }
  8. void b1_Click(object sender, EventArgs e)
  9. {
  10. // put your code
  11. }
  12. void b2_Click(object sender, EventArgs e)
  13. {
  14. // put your code
  15. }
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 22
Reputation: bk_bhupendra is an unknown quantity at this point 
Solved Threads: 1
bk_bhupendra bk_bhupendra is offline Offline
Newbie Poster

click events for rutime generated controls

 
-2
  #5
19 Days Ago
im building windows application
it generate buttons during run time

code:

  1. int x=50,y=50;
  2. for(int i=0;i<10;i++)
  3. {
  4.  
  5. Button field = new Button();
  6. ActiveForm.Controls.Add(field);
  7.  
  8. field.Name = "button"+i.ToString();
  9. field.Text = i.ToString();
  10. field.Size = new Size(100,23);
  11. field.Location = new Point(x, y);
  12.  
  13.  
  14. y = y +30;
  15.  
  16.  
  17. }


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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 22
Reputation: bk_bhupendra is an unknown quantity at this point 
Solved Threads: 1
bk_bhupendra bk_bhupendra is offline Offline
Newbie Poster

click events for rutime generated controls

 
-2
  #6
19 Days Ago
these no of buttons depends on d value of 'i'....
i cnt define a particular no of bttns....
Last edited by peter_budo; 17 Days Ago at 4:59 am. Reason: Keep It Clean - We strongly encourage all posts to be in full-sentence English.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 306
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 55
Ryshad's Avatar
Ryshad Ryshad is online now Online
Posting Whiz
 
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?
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 22
Reputation: bk_bhupendra is an unknown quantity at this point 
Solved Threads: 1
bk_bhupendra bk_bhupendra is offline Offline
Newbie Poster

click events for rutime generated controls

 
-2
  #8
18 Days Ago
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....
Last edited by peter_budo; 17 Days Ago at 5:02 am. Reason: Removing quote tag
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 306
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 55
Ryshad's Avatar
Ryshad Ryshad is online now Online
Posting Whiz
 
0
  #9
18 Days Ago
I posted this in a similar thread:

  1. string[] Descriptions = { "One", "Two", "Another Description", "Desc." };
  2.  
  3. for( int i = 1; i<=Descriptions.Length;i++)
  4. {
  5. Button btn = new Button();
  6. btn.Name = "btn" + i.ToString(); //btn1, btn2, etc
  7. btn.Text = Descriptions[i - 1];
  8. panel1.Controls.Add(btn);
  9. }

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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 22
Reputation: bk_bhupendra is an unknown quantity at this point 
Solved Threads: 1
bk_bhupendra bk_bhupendra is offline Offline
Newbie Poster

events for generated buttons

 
0
  #10
18 Days Ago
hi,

i hav generated the buttons ...
i need the events for each one....
hw can i do that???

Last edited by peter_budo; 17 Days Ago at 5:03 am. Reason: Removing quote tag
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC