3

I create Form2 from Form1. I want that Form2 opens on the second monitor. How I can do this? I use this code:

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

How to change this code for this? Thanks for all.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
annayak
  • 103
  • 2
  • 4
  • 10
  • 4
    possible duplicate: http://stackoverflow.com/questions/1363374/showing-a-windows-form-on-a-secondary-monitor – chaliasos May 24 '12 at 07:41

3 Answers3

8

Use this code

Form2 dlg = new Form2();
Screen[] screens = Screen.AllScreens;
Rectangle bounds = screen[1].Bounds;
dlg.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);
dlg.StartPosition = FormStartPosition.Manual;
dlg.Show();
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
3

Try something like this

Screen[] sc; 
sc = Screen.AllScreens; 
//get all the screen width and heights 
Form2 f = new Form2(); 
f.FormBorderStyle = FormBorderStyle.None; 
f.Left = sc[neededmonitor].Bounds.Width; 
f.Top = sc[neededmonitor].Bounds.Height; 
f.StartPosition = FormStartPosition.Manual; 
f.Show(); 
Likurg
  • 2,742
  • 17
  • 22
  • But can I check this if (exist.sc[1]) { show form on the second monitor} else { on 1 monitor }. sc[1] = what's type of data? – annayak May 24 '12 at 08:10
  • Data type is Screen. You can check length of sc. Length = number of allowed screens. if(sc.Length>1) { show form on the second monitor} – Likurg May 24 '12 at 10:51
0

And you can detect secondary monitor (using System.Linq) like this:

var screen = Screen.AllScreens.FirstOrDefault(s => !s.Primary && s.DeviceName.Contains("DISPLAY2"));