0

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

  • Form1 must have focus
  • Form2 is opened under Form1 (background Form1)

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;
        }
    }
Community
  • 1
  • 1
Musketyr
  • 745
  • 1
  • 16
  • 37
  • Do you mean showing the forms side by side then you would have to set the size and position of the forms. – V4Vendetta Oct 03 '12 at 05:32
  • No, I mean setting Z-order of Form2. I want to still Form1 on the top, but I cannot set Form1 as TOP MOST (because Form1 can be another Form from application). The point is: every automaticaly (programmatically) opened Form must be open under active Form (Form1 in this case) not TOP. – Musketyr Oct 03 '12 at 05:40

3 Answers3

2

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();
    }

}
mihirj
  • 1,199
  • 9
  • 15
  • But I don't want to create MDI form! Forms which will be opened are more than one and from different locations code. This is short example what I want to do. – Musketyr Oct 03 '12 at 05:11
  • I think in order to view multiple forms under a single parent form, you have to use Multiple Document Interface. you can set your current parent form to have MdiChilds as follows: this.IsMdiContainer = true; and then set form2.MdiParent to this; Else, other way to do this is by using ShowDialog(). However, ShowDialog would restrict the number of forms to 2 only. – mihirj Oct 03 '12 at 05:46
  • Problem is I cannot use MDI, because the application is modular application from another company and I make one module to this application. So I application is not MDI and I cannot use this. – Musketyr Oct 03 '12 at 06:06
  • Please try this. var form2 = new Form2(); this.AddOwnedForm(form2); form2.Show(); Let me know if this works for you. You can remove the form by using this.RemoveOwnedForm(form2); once form2 is closed. – mihirj Oct 03 '12 at 06:25
  • No, still not working. Here is simple test code: http://www.ulozto.cz/xUjSyrb/topmostwindow-zip – Musketyr Oct 03 '12 at 06:34
  • I am unable to access this site. Could you please edit that code in your question? – mihirj Oct 03 '12 at 06:38
  • I saw your code. If you want to show Form2 on button click then, please transfer the code from timer1_Tick to button1_Click. You can change your timer1_Tick code by adding additional check as follows:if (!form2.Visible){ //code }. This would prevent the form2 opening multiple times. – mihirj Oct 03 '12 at 07:05
  • The timer only simulating opening Form2 from code from another part of application. I tried to transfer code to event of button1, but still Form2 is opened above Form1. – Musketyr Oct 03 '12 at 07:07
  • try to add line this.WindowState = FormWindowState.Maximized above this.AddOwnedForm(form2); line. Remember, this would only give appearance that form2 is under form1 but I am afraid it is not possible to create form under parent form without MDI. – mihirj Oct 03 '12 at 07:13
  • This only maximized Form1. No changes. – Musketyr Oct 03 '12 at 07:17
  • The code runs ok at my end. It shows form1 maximized with form2 on top of that. – mihirj Oct 03 '12 at 07:26
  • You open Form2 under Form1 - Form1 is on TOP MOST? – Musketyr Oct 03 '12 at 07:28
  • Yes. Form2 is displayed as small rectangle along with Form1 which is maximised with focus set on Form2. – mihirj Oct 03 '12 at 07:32
  • After tests it si working but I have to change: `this.AddOwnedForm(form2);` to `form2.AddOwnedForm(this);` – Musketyr Oct 03 '12 at 13:58
0

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();
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
0

I conjecture that you want a dialog

var form2 = new Form2(); 
form2.Visible = true; 
this.ShowDialog();
Amir
  • 770
  • 8
  • 21