dcsimg
Results 1 to 11 of 11

Thread: Center a form onscreen in VB 2015

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    13

    Center a form onscreen in VB 2015

    Having trouble getting my startup form to appear directly in the center of the screen. First I tried the obvious, setting the StartPosition property to CenterScreen in the properties pane, this works for all subsequent forms, but not the initial form. A little reading informed me that I was supposed to do this in code, so I tried setting the property directly in the Load event, after setting the StartPosition to manual in the properties pane. This didn't work either.

    Next I looked for some snippets that set the position by other means, nothing has worked. The latest one I tried was from these forums, on this page:

    http://www.vbforums.com/showthread.p...reen-in-VB-NET

    Me.Top = (My.Computer.Screen.WorkingArea.Height \ 2) - (Me.Height \ 2)
    Me.Left = (My.Computer.Screen.WorkingArea.Width \ 2) - (Me.Width \ 2)
    Not working either.

    Is there some special thing I have to do in VB 2015 to make this work? This should be a simple matter of setting the property, why isn't it?
    Last edited by jfs2845; Jan 24th, 2018 at 11:57 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,864

    Re: Center a form onscreen in VB 2015

    The StartPosition property is the correct property. If in Form_load, you modify the Form size, it would make the Form appear off centre...

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    13

    Re: Center a form onscreen in VB 2015

    Nope. Size is not manipulated in Load(). Just tried this as well:

    Code:
     Me.StartPosition = FormStartPosition.CenterScreen
    Attempts to directly position with Me.left and Me.Top do not work either.

    Form properties otherwise are fairly normal, size is preset in the properties pane to about 1000x1300, I do have the maximize and minimize buttons disabled, would that affect things?

    Window state is maximized, changing this has no effect. FormBorderStyle is FixedSingle. ControlBox is True. Autosize is False. AutosizeMode is GrowOnly. I have a large jpeg as a background image, tiled.

    So what other subtle setting elsewhere could be prohibiting this from working correctly?

    EDIT:

    I should also mention that I invoke the form by using:

    Code:
    Sub main()
            Application.Run(New Form1)
        End Sub
    from module1, as the startup object, should this be initialized in a different fashion?
    Last edited by jfs2845; Jan 25th, 2018 at 03:50 PM.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,505

    Re: Center a form onscreen in VB 2015

    I don't know when form sizing is done. It may be after load or before it. If you move your code to Shown, and it still doesn't work, then something else is going on, because that's as late in the process as you can move it. There's a Resize event, I just don't know when it gets raised relative to Load. You likely need to be after that.
    My usual boring signature: Nothing

  5. #5
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,021

    Re: Center a form onscreen in VB 2015

    Set the StartPosition to CenterScreen in the Form's HandleCreated event handler. It fires before Load.

    EDIT: Shaggy's right, the Resize event works too. It fires twice before Load (even for a form of default size).

    By the way, it's easy to check the order in which events fire by putting Console.WriteLine with the event name in the event handler. If you open the form's Designer.vb file, you can do the same trick in the New or InitializeComponent methods.

    BB

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    13

    Re: Center a form onscreen in VB 2015

    None of your suggestions work. Whether you stick it in HandleCreated, or Resize, or Shown, it just gleefully ignores the property and starts it over on the left hand side of the screen. Whether it's run as a debug-enabled exe from the IDE or a stand-alone release exe placed elsewhere, doesn't matter, it's always the same. Browsing the project files by hand, nothing seems to out of order, so ***? What would cause this?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,864

    Re: Center a form onscreen in VB 2015

    Could you upload the project somewhere and post a link? You can send the link by PM if you prefer

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,053

    Re: Center a form onscreen in VB 2015

    Quote Originally Posted by .paul. View Post
    Could you upload the project somewhere and post a link?
    Or just zip the solution folder and attach it to a post. Easiest for everyone. Just be sure to delete the 'bin' and 'obj' folders first, as you're not allowed to post binaries.

    By the way, what happens if you just create a new project and set the StartPosition of the form to CentreScreen? Does that behave as expected?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,864

    Re: Center a form onscreen in VB 2015

    Quote Originally Posted by jmcilhinney View Post
    Or just zip the solution folder and attach it to a post. Easiest for everyone.
    Depends if you don't mind anyone seeing your project, or just those you post the link to...

  10. #10
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,021

    Re: Center a form onscreen in VB 2015

    Quote Originally Posted by jfs2845 View Post
    None of your suggestions work. Whether you stick it in HandleCreated, or Resize, or Shown, it just gleefully ignores the property and starts it over on the left hand side of the screen. Whether it's run as a debug-enabled exe from the IDE or a stand-alone release exe placed elsewhere, doesn't matter, it's always the same. Browsing the project files by hand, nothing seems to out of order, so ***? What would cause this?
    Just a thought. I see from post #1 that you are starting the program as a console project. Possibly those events don't fire in the same way as a normal Forms project. Seeing that the StartPosition needs to be set early (the Forms designer puts it in the InitializeComponent sub), maybe you could try this:
    Code:
     Sub Main()
          Application.Run(New Form1 With {.StartPosition = FormStartPosition.CenterScreen})
       End Sub
    It gave me a nicely centred Form1, but I have little experience with Console projects so I could be mistaken.

    BB

  11. #11

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    13

    Re: Center a form onscreen in VB 2015

    BB, I tried your above, and it does not work. THEN I deleted Sub Main() entirely, and set Form1 as the startup object, and neither setting StartPosition in code or in the properties pane worked.

    Soooo, then I tried another test project slapped together with the same setup and form loading method and sequence. It worked fine. Hmmmmm? What.... the ..... *********?

    So what I have done, is delete that nasty Form1 in its entirety and just built a whole new one completely from scratch, with a different name. Run it as the startup object with its start position set to CenterScreen in the properties pane and... it works perfectly!

    Something must have been corrupted with regards to the original Form1, but I'll be danged if I can figure out what it was.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width