0

I have an application that when it loads a certain window it need to automatically move to the screen that has 1920x515 resolution. I found a way to make it work but I'd be looking for a more efficient way of doing this as clearly this is the worst approach. How can I improve this?

private void ComboBox2_Initialized(object sender, EventArgs e)
        {
            foreach (var screen in Screen.AllScreens)
            {
                // For each screen, add the screen properties to a list box.
                //ComboBox2.Items.Add("Location: "  + screen.Bounds.Location .ToString());
                ComboBox2.Items.Add(screen.Bounds.Left.ToString());
                ComboBox2.Items.Add(screen.Bounds.Width.ToString());
                ComboBox2.Items.Add(screen.Bounds.Top.ToString());
                ComboBox2.Items.Add(screen.Bounds.Height.ToString());

                //double top = 0;
                //double left = 0; 


                if (ComboBox2.Items.Contains("1920"))
                {


                    if (ComboBox2.Items.Contains("515"))
                    {

                        Properties.Settings.Default.Top = screen.Bounds.Top;
                        Properties.Settings.Default.Left = screen.Bounds.Left;


                        Properties.Settings.Default.Save();
                        //System.Windows.MessageBox.Show(Properties.Settings.Default.Left.ToString());

                    }
                }


            }
        }```
nabpp
  • 9
  • 4

2 Answers2

0

This code doesn't move the screen at all. It only fills a combo with the dimensions of the screens, then saves some of them in the application's settings.

To move the form you need to find the top left coordinates of the target screen and move the form to them:

var desired=new Size(1920,515);
var targetScreen= Screen.AllScreens.FirstOrDefault(
                          screen=> screen.Bounds.Size==desired);
if (targetScreen != null)
{
    this.Location=targetScreen.WorkingArea.Location;
}
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • Thanks. Sorry I failed to mention I was using WPF but using the System.Windows.Forms reference. When I paste this into the Window_Initialized event I get the following errors. "The name screen does not exist in the current context" and Operator '==' cannot be applied to operands of type 'Size' and 'Size' – nabpp Sep 23 '22 at 17:10
  • [It can](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.size.op_equality?view=net-6.0). `System.Drawing.Size` always had an equality operator. Are you using a different class perhaps? – Panagiotis Kanavos Sep 23 '22 at 17:13
  • I had to define it with `var desired = new System.Drawing.Size(1920, 515);` . Because this is WPF I am also using `this.WindowStartupLocation = screen.WorkingArea.Location;` The last error I need to clear is "The name screen does not exist in the current context" – nabpp Sep 23 '22 at 17:24
0

Thank you @Panagiotis Kanavos This fixed my issue

var desired = new System.Drawing.Size(1920, 515);


            var targetScreen = Screen.AllScreens.FirstOrDefault(
                                      screen => screen.Bounds.Size == desired);

            
            if (targetScreen != null)
            {
                this.Left = targetScreen.WorkingArea.Location.X;
                this.Top = targetScreen.WorkingArea.Location.Y;
            }
nabpp
  • 9
  • 4