Musketyr
Musketyr

Reputation: 773

How to open form under the active form

In .Net WinForms I have two forms. Form1 is open and active. How can I open new form (Form2) that:

First point is simple see stackoverflow... But I don't know how to show Form2 under Form1. Thanks.

EDIT

 public partial class Form1 : Form
{
    Form2 frm;

    public Form1()
    {
        InitializeComponent();
        frm = new Form2();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        textBox1.Focus();
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        this.AddOwnedForm(frm);
        frm.Show();
        frm.SendToBack();
        this.BringToFront();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frm.Show();
        frm.SendToBack();
        this.BringToFront();
    }

}

Form2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();            
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        textBox1.Focus();
    }

    protected override bool ShowWithoutActivation
    {
        get
        {
            return true;
        }
    }

    protected override CreateParams CreateParams
    {
        get
        {
            //make sure Top Most property on form is set to false
            //otherwise this doesn't work
            int WS_EX_TOPMOST = 0x00000008;
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= WS_EX_TOPMOST;
            return cp;
        }
    }

Upvotes: 0

Views: 9747

Answers (3)

mihirj
mihirj

Reputation: 1219

I think for second point what you need to do is this:

var form2 = new Form2();
    form2.MdiParent = form1; //form1 is your parent form; use this operator in case you are creating this form from out base form

    form2.WindowState = FormWindowState.Normal;
    form2.Show();

Please have a look at this for further details:

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.mdiparent.aspx

EDIT

Please see below code which gives an appearance that frm is open on top of Form1.

public partial class Form1 : Form
{
    Form2 frm;

    public Form1()
    {
        InitializeComponent();
        frm = new Form2();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.WindowState = FormWindowState.Maximized;
        textBox1.Focus();
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (!frm.Visible)
        {
          this.AddOwnedForm(frm);
          frm.Show();
          frm.SendToBack();
          this.BringToFront();
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.AddOwnedForm(frm);
        frm.Show();
        frm.SendToBack();
        this.BringToFront();
    }

}

Upvotes: 2

Amir
Amir

Reputation: 788

I conjecture that you want a dialog

var form2 = new Form2(); 
form2.Visible = true; 
this.ShowDialog();

Upvotes: 0

Jeremy Thompson
Jeremy Thompson

Reputation: 65554

You simply instantiate the Form2 and show it, eg:

var form2 = new Form2();
form2.Visible = true;

Then you set Form1 to be the active form (assuming this is done in Form1's code - hence the this):

this.BringToFront();

Upvotes: 0

Related Questions