Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I added a button with code at my form and handled its click event but when I execute it, it doesn't work and there are no run time errors !!
does it because I added my button in handling function of another object and with code.
C#
MySchema.OnSelectedSchemaChanged += new SelectedSchemaChangedHandler(MySchema_OnSelectedSchemaChanged);

      }

      void MySchema_OnSelectedSchemaChanged(string schema)
      {
          Button btnCreate = new Button();
          btnCreate.Left = 270;
          btnCreate.Top = 520;
          btnCreate.Width = 130;
          btnCreate.Height = 40;
          btnCreate.Text = "Create Form";
          btnCreate.Name = "btnCreateForm";
          groupBox1.Controls.Add(btnCreate);
          btnCreate.Click += new EventHandler(btnCreate_Click_1);
      }

    void btnCreate_Click_1(object sender, EventArgs e)
      {

          MessageBox.Show("Ok");   // this message doesn't appear

      }
Posted
Updated 7-Apr-12 8:38am
v2
Comments
André Kraak 7-Apr-12 14:38pm    
Edited question:
Added pre tags

Hello

The code seems Ok and can runs and shows the MessageBox

If you see btnCreate on gropBox1 and click on it, you will find out that the Message will be shown.

But maybe you can not see btnCreate because of btnCreate.Left and btnCreate.Top, I also had this problem. The Button was out of my screen. so change the Left and Top
 
Share this answer
 
v4
Comments
VJ Reddy 8-Apr-12 20:56pm    
+5
Shahin Khorshidnia 9-Apr-12 6:33am    
Thank you VJ
Try using this code:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace button
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Button btn = new Button();

        private void Form1_Load(object sender, EventArgs e)
        {
            btn.Name = "0";
            btn.Text = "btn";
            btn.Location = new Point(10, 10);
            btn.Width = 200;
            btn.Height = 100;
            btn.Text = "Button";
            btn.MouseClick += mouseclick;
            Controls.Add(btn);
            btn.BringtoFront(); //BringtoFront code
        }

        private void mouseclick(object sender, MouseEventArgs e)
        {
            MessageBox.Show("Button Clicked");
        }
    }
}


if you don't see your button,add BringtoFront() code and check location.
 
Share this answer
 
v2
delete that button and take new button...
It may be because of renaming button...
 
Share this answer
 

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