Click here to Skip to main content
14,828,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am creating a win form application in which i need to make button at run-time
this is code how i am doing

C#
Button button1= new Button();  
panel1.Controls.Add(button1);              
               
button1.Location=new Point(220,457);
button1.Name="btnnextimage11";
button1.Size=new Size(118, 39);
button1.Text = "Next Image";
button1.UseVisualStyleBackColor = true;

button1.Click += new System.EventHandler(button1_Click);

private void button1_click(object sender, EventArgs e)            
{
}
Posted
Updated 13-Sep-13 3:26am
v2
Comments
Code Mirror 13-Sep-13 9:17am
   
i get this error
The name 'button1_Click' does not exist in the current context

C# is case sensitive - rename your function from button1_click to button1_Click
   
Thanks a lot to helping hands :)
I am posting my final code to create a Button dynamically and related stuff too
C#
{
Button button1= new Button();
button1.Location=new Point(220,457);
button1.Name="button1";
button1.Size=new Size(118, 39);
button1.Text = "Click This button ";
button1.UseVisualStyleBackColor = true;
button1.Click += new EventHandler(button1_Click);
}
private void button1_Click(object sender, EventArgs e)
{
 MessageBox.Show(" I am button1 , you just clicked me ");
}

you have created a button,
now below code will add the button1 to the form
C#
Controls.Add(button1);


or if you want you can add the button1 to the panel1
C#
panel1.Controls.Add(button9);
   
This Will help you....C# is case sensitive, language check your event



C#
 Button btn = new Button();
btn.Click += new EventHandler(btn_Click);


 private void btn_Click(object sender, EventArgs e)
    {

    }
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900