Home > Back-end >  C# Form Button added via code doesn't work on Click
C# Form Button added via code doesn't work on Click

Time:04-10

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?

CodePudding user response:

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";

CodePudding user response:

If you are doing an android app or something like that, you have to add this line in ur oncreate : $btnVar$.Click = function;

  •  Tags:  
  • c#
  • Related