2

I'm trying to position a form in a specific spot on the screen when opening. I used Form1.Location = New Point(200, 1200) which worked on my screen but on another screen it placed the form outside the viewing area. Is there a way to check the screen size first and position the form with a percent of screen position? Something like this Me.Location = New Point(Height / 2, Width / 2) but not in the center?

djv
  • 15,168
  • 7
  • 48
  • 72
Malachilee
  • 99
  • 1
  • 11
  • 3
    Make your app DpiAware: [How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?](https://stackoverflow.com/a/13228495/7444103) and read the notes [here](https://stackoverflow.com/a/53026765/7444103) about the Displays. – Jimi Dec 03 '19 at 14:44

2 Answers2

3

Do you mean something like that?

Me.Location = New Point(CInt((My.Computer.Screen.WorkingArea.Width - Me.Width) * 0.5),
                        CInt((My.Computer.Screen.WorkingArea.Height - Me.Height) * 0.5))
G3nt_M3caj
  • 2,497
  • 1
  • 14
  • 16
  • 1
    However take care that @Jimi said above about the percentage (e.g.: 150%). Here i had posted another answer about that https://stackoverflow.com/questions/58971670/how-can-i-get-the-scale-factor-of-all-my-monitors/58980909#58980909 – G3nt_M3caj Dec 03 '19 at 15:14
  • I am looking over those links thank you. The code you posted didn't do anything for me , I'm just learning. do I need to define My.Computer.Screen.WorkingArea somewhere else in my code? – Malachilee Dec 03 '19 at 15:33
  • 1
    If you are working in a windows forms project 'My' is a useful NameSpace that permit you to interact with PC, resources etc. You can put the code above in the Sub Load of your form. It’s presumed is an WinForm application if you want to place a “Form” in center of your screen. :) – G3nt_M3caj Dec 03 '19 at 15:45
  • 1
    Okay, I got it now! That does exactly what I needed it to do. Thank you for explaining it. My 8 year old son wants to be a coder so we are learning together. – Malachilee Dec 03 '19 at 15:56
  • 1
    I’m a coder and I’want to become a 8y old child :P….. As advice for your son. ;). Have fun my friend – G3nt_M3caj Dec 03 '19 at 16:01
1

Well it's actually quite simple. There is something built into the Form called System.Windows.Forms.StartPosition. This one line Me.StartPosition = FormStartPosition.CenterScreen must be put in the constructor.

Sub New()
    InitializeComponent()
    Me.StartPosition = FormStartPosition.CenterScreen
End Sub

If you are doing it manually, perhaps because you don't want to modify the constructor for any reason, here is code to use. I know you have accepted an answer but there is no My namespace in c# so I usually try to avoid it because I frequently switch between the languages. I know it's not a requirement but it's just a recommendation. Using System.Windows.Forms.Screen,

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim x = CInt((Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2)
    Dim y = CInt((Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2)
    Me.Location = New System.Drawing.Point(x, y)
End Sub
djv
  • 15,168
  • 7
  • 48
  • 72
  • 1
    Thank you for posting your answer as well, I tried it and it also did what I needed. I will gladly learn multiple ways of doing something. :) – Malachilee Dec 03 '19 at 16:16
  • 1
    StartPosition is available in the Properties window and can be set at Design time. – Mary Dec 03 '19 at 16:22
  • 1
    My answer assume a concept around a specific point in the screen (in your case the center) but if the target of your form is always the center let me to suggest as advice you need to use the method @djv answered as is more simple and fluent. – G3nt_M3caj Dec 03 '19 at 16:25