Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move Form onto specified Screen

I am trying to figure out how to move specified System.Windows.Forms.Form onto another than primary screen. I have ComboBox with list of available screens where user selects whichever screen he likes and my application is supposed to move one of its windows onto that screen.

I have only one screen on my laptop and no external monitor, so ComboBox on my computer offers only one option. I think minimalising desired window, moving it's left corner in the center of selected screen's Bounds and maximilising would do the job, right? I just can't test it. Is this a good way to go?

Thanks in advance!

like image 915
Aaron Summernite Avatar asked Dec 07 '11 18:12

Aaron Summernite


1 Answers

Here's what I did, as a simple test...

I added a simple wrapper class so that I could change what happens on the ToString call (I only wanted to see the name listed in the combo box)

private class ScreenObj
{
    public Screen screen = null;

    public ScreenObj(Screen scr)
    {
        screen = scr;
    }

    public override string ToString()
    {
        return screen.DeviceName;
    }
}

In the form load event I added this:

foreach(Screen screen in Screen.AllScreens)
{
     cboScreens.Items.Add(new ScreenObj(screen));
}

And for the selected index change event of the combo box I had this:

private void cboScreens_SelectedIndexChanged(object sender, EventArgs e)
{
    object o = cboScreens.SelectedItem;
    if(null == o)
        return;

    ScreenObj scrObj = o as ScreenObj;
    if(null == scrObj)
        return;

    Point p = new Point();

    p.X = scrObj.screen.WorkingArea.Left;
    p.Y = scrObj.screen.WorkingArea.Top;

    this.Location = p;
}

It moved the form to the upper left hand corner of each of my screens.

like image 78
pennyrave Avatar answered Sep 18 '22 02:09

pennyrave