hey i just created a button via code and how do i set it up to respond when clicked?
Printable View
hey i just created a button via code and how do i set it up to respond when clicked?
later...Code:Button button1 = new Button();
button1.Click += new System.Windows.Forms.Button();
this.Controls.Add(button1);
Code:private void button1_Click(object sender, System.EventArgs e)
{
}
PT,
Check your event handler hookup...You need to wire it up via a delegate. ;)
Should be
Code:Button button1 = new Button();
button1.Click += new EventHandler(this, button1_Click);
this.Controls.Add(button1);
Actually its this :D
Code:Button button1 = new Button();
button1.Click += new EventHandler(this.button1_Click);
this.Controls.Add(button1);
Isn't the this added automatically?
Yeah, you made a mistake with the comma afte this. It should be this.button1_click
ops forgot that when i made copy past of some parts of the code...when putting all toguether i deleted the delegate i think..lol
Indeed. I thought you must pass the owner object seperatly.
Would this work?
Button button1 = new Button();
button1.Click += new EventHandler(button1_Click);
Controls.Add(button1);
whats the difference?
Not explicitly referencing this. I wonder why people always write
this.Controls.Add...
etc.
hehe because in the IDE a combobox appears and u only have to write the first letters then :D
and sometimes u dont remember well the names of the vars/methods
In this case instead of writing 'this.' simply hit Ctrl+Space. Brings up the same box.