Move form to specified screen

I am trying to figure out how to transfer the specified System.Windows.Forms.Form to a different screen than the main one. I have a ComboBox with a list of available screens, where the user selects any screen that he likes, and my application must move one of his windows to this screen.

I have only one screen on my laptop and an external monitor, so ComboBox on my computer offers only one option. I think it minimizes the desired window by moving it to the left in the center of the selected screen. Borders and maximization will do the job, right? I just can't check it. Is this a good way to go?

Thanks in advance!

+5
source share
1 answer

Here is what I did as a simple test ...

I added a simple wrapper class to change what happens when ToString is called (I just wanted to see the name specified 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 the following:

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

And for the selected index change event in 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;
}

Moved the shape to the upper left corner of each of my screens.

+5
source

All Articles