Hi all,

I want to show Form2 when button1 of Form1 is clicked.
how would i do it.

this is what i was trying to do.But it is not working
But it gives me an error ;
The event 'System.Windows.Forms.Control.Click' can only appear on the left hand side of += or -=

Form1 fm1=new Form1()
fm1.show();

EventHandler ev=fm1.button1.Click();

if (ev!=null)
{
Form2 fm2=new Form2()
fm2.show();
}

PLZ... help me

Recommended Answers

All 3 Replies

Hi all,

I want to show Form2 when button1 of Form1 is clicked.
how would i do it.

this is what i was trying to do.But it is not working
But it gives me an error ;
The event 'System.Windows.Forms.Control.Click' can only appear on the left hand side of += or -=

Form1 fm1=new Form1()
fm1.show();

EventHandler ev=fm1.button1.Click();

if (ev!=null)
{
Form2 fm2=new Form2()
fm2.show();
}

PLZ... help me

This is off the top of my head, don't have .NET near me...

in your initialisation....

IForm2 fm2 = null;
frm1.button1.Click += new EventHandler(my_button_click());

private void my_button_click(object sender, EventArgs e)
{

}

Hi all,

I want to show Form2 when button1 of Form1 is clicked.
how would i do it.

this is what i was trying to do.But it is not working
But it gives me an error ;
The event 'System.Windows.Forms.Control.Click' can only appear on the left hand side of += or -=

Form1 fm1=new Form1()
fm1.show();

EventHandler ev=fm1.button1.Click();

if (ev!=null)
{
Form2 fm2=new Form2()
fm2.show();
}

PLZ... help me

This is off the top of my head, don't have .NET near me...
And doesn't take into account if the form is already showing.

This isn't the cleanest way to do navigation, if it's just a small project it is okay, but larger ones you need to look into Routing and commands.

in your initialisation....

IForm2 fm2 = ;
frm1.button1.Click += new EventHandler(my_button_click());

private void my_button_click(object sender, EventArgs e)
{
fm2.Show();
}

TRy this:
Open a new forms application and fill in some code from what you see here:

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Button ShowBtn; // declare a button
        Form MyForm2;  // declare a Form

        public Form1()
        {
            InitializeComponent();

            // initialize a button
            ShowBtn = new Button();
            ShowBtn.Location = new System.Drawing.Point(100, 100);
            ShowBtn.Name = "Showit";
            ShowBtn.Size = new System.Drawing.Size(75, 23);
            ShowBtn.Text = "Show form";
            ShowBtn.Click += new System.EventHandler(MyShowFormButton_Click);

            this.Controls.Add(ShowBtn); //add button to controlscollection of the form
        }

        private void MyShowFormButton_Click(object sender, EventArgs e)
        {
            // initialize a second form
            MyForm2 = new Form();
            MyForm2.Show();
        }
    }
}
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.