Allen Jed Borromeo
Allen Jed Borromeo

Reputation: 23

Button creates a button then that button show/hide form

I already know how to create a button when a button is clicked. What code should I write next to this lines for me to be able to show/hide forms?


                    Button b1 = new Button();
                    b1.Location = new Point (21, 0);
                    b1.Name = "";
                    b1.Size = new Size(120, 100);
                    b1.FlatStyle = FlatStyle.Flat;
                    b1.Image = TITOMS_LOGIN.Properties.Resources.icon1_1_;
                    b1.BackColor = Color.Transparent;

                    Button b2 = new Button();
                    b2.Location = new Point(21, 99);
                    b2.Name = "";
                    b2.Size = new Size(120, 100);
                    b2.FlatStyle = FlatStyle.Flat;
                    b2.Image = TITOMS_LOGIN.Properties.Resources.icon2_1_;
                    b2.BackColor = Color.Transparent;

                    Button b3 = new Button();
                    b3.Location = new Point(21, 198);
                    b3.Name = "";
                    b3.Size = new Size(120, 100);
                    b3.FlatStyle = FlatStyle.Flat;
                    b3.Image = TITOMS_LOGIN.Properties.Resources.icon3_1_;
                    b3.BackColor = Color.Transparent;

                    Button b4 = new Button();
                    b4.Location = new Point(21, 297);
                    b4.Name = "";
                    b4.Size = new Size(120, 100);
                    b4.FlatStyle = FlatStyle.Flat;
                    b4.Image = TITOMS_LOGIN.Properties.Resources.icon2_1_;
                    b4.BackColor = Color.Transparent;

for each button they show different form

For example: Button 1 shows Form 1 and Hide others

Button 2 shows Form 2 and hide others

Upvotes: 0

Views: 45

Answers (2)

PandaPlayer
PandaPlayer

Reputation: 171

You have the buttons.
You need Events for the Buttons to perform actions.

b1.Click += new System.EventHandler(button1_Click);
b2.Click += new System.EventHandler(button2_Click);
b3.Click += new System.EventHandler(button3_Click);
b4.Click += new System.EventHandler(button4_Click);

button1_Click is a method which is called, whenever the button is clicked.

If you want do show an new Form:

 private void button1_Click(object sender, EventArgs e)
 {
     Form form = new Form();
     form.Show();
 }

If you want to close other Forms, the forms must be global.

    Form2 form1;
    Form2 form2;

    private void button1_Click(object sender, EventArgs e)
    {
        //Create new Form
        form2 = new Form2();
        form2.Show();

        //Check if other Form1 is not null -> it was initialized
        if (form1 != null )
        {
            form1.Close();
            form1.Dispose();
            form1 = null;
        }
    }

Upvotes: 0

CyberZeus
CyberZeus

Reputation: 311

You should handle button click events and inside each of them you have to instantiate the desired form and show it with Show() method.

Upvotes: 1

Related Questions