'C# Form Button added via code doesn't work on Click
I got a form. At Designer I wrote
public Button dugme = new Button();
and
this.Controls.Add(dugme);
dugme.Location = new Point(100, 300);
dugme.Size = new Size(400, 50);
dugme.Text = "Hesapla";
after that I went to Form1 and wrote
private void dugme_Click(object sender, EventArgs e)
{
MessageBox.Show("String");
}
But when I press the button it doesn't work. And not just in these function, I wrote different things too. How can I fix that?
Solution 1:[1]
I think you miss that binding Click event for your dugme Button.
Which might add to InitializeComponent method.
dugme.Click += new System.EventHandler(this.dugme_Click);
so the part of the code might look as below.
this.Controls.Add(dugme);
dugme.Location = new Point(100, 300);
dugme.Size = new Size(400, 50);
dugme.Click += new System.EventHandler(this.dugme_Click);
dugme.Text = "Hesapla";
Solution 2:[2]
If you are doing an android app or something like that, you have to add this line in your oncreate :
$btnVar$.Click += function;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | D-Shih |
| Solution 2 | Jean-François Fabre |
