User6667769
User6667769

Reputation: 805

Button click - redirect to UserControl.cs page in Windows Form

In my project, It consist two page. Those are Form1.cs,myusercontrol1.cs.

Form1 having Button. In Button_click event need to redirect Form1 to myuserontrol1.cs page.

This is my code:

private void button1_Click(object sender, EventArgs e)
        {
            // Code1
            var destinationform = new myusercontrol1();
            destinationform.Show();


            // Code2
            myusercontrol1 destinationformobj = new myusercontrol1();
            destinationformobj.Show();
        }

But it never redirect to myusercontrol1.cs page

Is this possible to redirect from Form to UserControl page?

Thanks in advance

Upvotes: 0

Views: 551

Answers (1)

Emad
Emad

Reputation: 3939

Ok according to the comments you can do this to solve your problem with a form:

public frmUserControl : Form
{
    private UserControl control;

    public frmUserControl(UserControl control)
    {
        this.control = control;
        this.Load += frmUserControl_Load;
    }

    public frmUserControl_Load(object sender, EventArgs e)
    {
        this.Controls.Add(control);
    }
}

You also have to take care of a few things like the size of the new form and the position of your user control inside the form. I tried to find my old code for that but it was too old and I don't have it right now. So I hope it helps :)

Upvotes: 1

Related Questions