Hi

I would like to ask if it is possible for me to create buttons inside a button using C#, such that when i click a button few more other buttons is available for clicking. How can i do that??

Thanks

Sure, you can do this.

private void Form1_Load(object sender, EventArgs e)
        {
            Button b1 = new Button();
            b1.Size = new Size(200, 200);
            Button b2 = new Button();
            // Adding event
            b2.Click += new EventHandler(test);
            b1.Controls.Add(b2);
            Controls.Add(b1);
        }

        void test(object sender, EventArgs e)
        {
            MessageBox.Show("hi");
        }

Thanks for your help. i am new to c#. Can i ask how to add the code in? just copy and paste? i did this but it cant seem to work.

Sure, you can do this.

private void Form1_Load(object sender, EventArgs e)
        {
            Button b1 = new Button();
            b1.Size = new Size(200, 200);
            Button b2 = new Button();
            // Adding event
            b2.Click += new EventHandler(test);
            b1.Controls.Add(b2);
            Controls.Add(b1);
        }

        void test(object sender, EventArgs e)
        {
            MessageBox.Show("hi");
        }

Thanks for your help. i am new to c#. Can i ask how to add the code in? just copy and paste? i did this but it cant seem to work.

Double click at Form GUI and paste the code following code in Form1_Load event.

Button b1 = new Button();
            b1.Size = new Size(200, 200);
            Button b2 = new Button();
            // Adding event
            b2.Click += new EventHandler(test);
            b1.Controls.Add(b2);
            Controls.Add(b1);

And also Paste the definition of test() Method outside the Form1_Load event.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.