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)
:(

Edited 7 Years Ago by peter_budo: Removing quote, not necessary

Try it,

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
        }

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

:-/

Edited 7 Years Ago by peter_budo: Removing quote

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:

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

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

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
        }

im building windows application
it generate buttons during run time

code:

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???


:-O

Edited 7 Years Ago by peter_budo: Removing quote tags

these no of buttons depends on d value of 'i'....
i cnt define a particular no of bttns....
:confused:

Edited 7 Years Ago by peter_budo: Keep It Clean - We strongly encourage all posts to be in full-sentence English.

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?

Comments
Correct.

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....
:(

Edited 7 Years Ago by peter_budo: Removing quote tag

I posted this in a similar thread:

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

hi,

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

:(

Edited 7 Years Ago by peter_budo: Removing quote tag

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:

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

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

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
        }

Does this look familiar to anyone else?...sure i've seen that code somewhere before ;)

bk_bhupendra,

Please stop using [quote][/quote] quotes for all of your posts. That is intended for quoting other people when you want to reply to their post. Just post your responses in the normal whitespace of the reply box. Use [code]

[/code] when posting code.

This article has been dead for over six months. Start a new discussion instead.