Results 1 to 7 of 7

Thread: Form Not Centering

  1. #1

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    Unhappy Form Not Centering

    I was writing an application that draws a bunch of random lines on a form with the mouse move event. I set the windows start position property of the form to center screen, but no matter what I did the form would appear in the windows default position. After around 30 minutes of cutting code and trying to paste it into new projects, it still wouldn't center the form. I finally gave up trying to center the form by setting the form property, so I changed it to manual and wrote code that centered the form horizontally, and around 50 pixels from the top edge and the code worked. Has any one else had this experience with VS 2005 proffesional? It was really bugging me for some reason.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Form Not Centering

    Show your form's load code?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Form Not Centering

    Is it just the main form or a child form?
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  4. #4

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    Re: Form Not Centering

    Sorry about the long response time, but I have no interent right now.

    Well, there was no form load code when the form wouldn't center, I had to add form load event code to get it to center. I was trying to set the form's position property in the properties tab, and it wouldn't center it. After I set the form's position property to manual and added this code

    Visual Basic Code:
    1. Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.  
    3.         Dim startX As Integer = (Screen.PrimaryScreen.WorkingArea.Width \ 2) - (Me.Width \ 2)
    4.         Dim startY As Integer = 50
    5.         Dim startPoint As New Point(startX, startY)
    6.  
    7.         Me.Location = startPoint
    8.  
    9.     End Sub
    , then the form started to center correctly. It is the parent form.

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Form Not Centering

    I was trying to set the form's position property in the properties tab, and it wouldn't center it.
    That's kinda strange. I was thinking that something in the form load event might be changing the form's position but apparently not...

    Can I test your project?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Form Not Centering

    Quote Originally Posted by koolsid View Post
    That's kinda strange. I was thinking that something in the form load event might be changing the form's position but apparently not...

    Can I test your project?
    After you had the Form's Loading event, that seemed plausible. This is definitely weird. Testing his project would be ideal.

    Also, is there anything in the Application Events?
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  7. #7

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    Re: Form Not Centering

    This code requires a form named MainForm. Try to comment out the forms load event code and set the properties of the form start position to centerscreen. Thanks for taking a look.

    VB 2005 Code:
    1. Public Class MainForm
    2.  
    3.     Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.  
    5.         Dim startX As Integer = (Screen.PrimaryScreen.WorkingArea.Width \ 2) - (Me.Width \ 2)
    6.         Dim starty As Integer = 50
    7.         Dim startPoint As New Point(startX, starty)
    8.  
    9.         Me.Location = startPoint
    10.  
    11.     End Sub
    12.  
    13.     Private Sub MainForm_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    14.  
    15.         DrawLines()
    16.  
    17.     End Sub
    18.  
    19.     Private Sub MainForm_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    20.  
    21.         DrawLines()
    22.  
    23.     End Sub
    24.  
    25.     Private Sub DrawLines()
    26.  
    27.         For i As Integer = 1 To 100
    28.             Randomize()
    29.  
    30.             'create a random object and generate random line points
    31.             Dim randomGen As New Random
    32.             Dim intRandX As Integer = randomGen.Next(10, Me.ClientSize.Width - 10)
    33.             Dim intRandY As Integer = randomGen.Next(10, Me.ClientSize.Height - 10)
    34.             Dim intRandXX As Integer = randomGen.Next(10, Me.ClientSize.Width - 10)
    35.             Dim intRandYY As Integer = randomGen.Next(10, Me.ClientSize.Height - 10)
    36.  
    37.             'create a graphics object to draw on the form
    38.             Dim formGraphics As Graphics
    39.             formGraphics = Me.CreateGraphics
    40.  
    41.             'make a rgba(alpha,red,green,blue) pen with random colors, size, and dash style
    42.             Dim randAlpha As Integer = randomGen.Next(5, 255)
    43.             Dim randRed As Integer = randomGen.Next(0, 255)
    44.             Dim randBlue As Integer = randomGen.Next(0, 255)
    45.             Dim randGreen As Integer = randomGen.Next(0, 255)
    46.             Dim randSize As Integer = randomGen.Next(1, 10)
    47.             Dim randPen As New Pen(Color.FromArgb(randAlpha, randRed, randGreen, randBlue), randSize)
    48.             randPen.StartCap = Drawing2D.LineCap.Round
    49.             randPen.EndCap = Drawing2D.LineCap.Round
    50.  
    51.             'pick dash style
    52.             Dim dashStyle As Integer = randomGen.Next(1, 5)
    53.             Select Case dashStyle
    54.                 Case 1
    55.                     randPen.DashStyle = Drawing2D.DashStyle.Solid
    56.                 Case 2
    57.                     randPen.DashStyle = Drawing2D.DashStyle.Dash
    58.                 Case 3
    59.                     randPen.DashStyle = Drawing2D.DashStyle.DashDot
    60.                 Case 4
    61.                     randPen.DashStyle = Drawing2D.DashStyle.DashDotDot
    62.                 Case 5
    63.                     randPen.DashStyle = Drawing2D.DashStyle.Dot
    64.             End Select
    65.  
    66.             'draw line
    67.             formGraphics.DrawLine(randPen, intRandX, intRandY, intRandXX, intRandYY)
    68.  
    69.             'dispose of graphics object
    70.             formGraphics.Dispose()
    71.         Next i
    72.  
    73.         'clear the form
    74.         Me.Invalidate()
    75.  
    76.     End Sub
    77.  
    78. End Class

Posting Permissions

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



Click Here to Expand Forum to Full Width