|
-
Nov 20th, 2009, 11:48 AM
#1
Thread Starter
Lively Member
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.
-
Nov 20th, 2009, 11:59 AM
#2
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
-
Nov 20th, 2009, 12:08 PM
#3
Re: Form Not Centering
Is it just the main form or a child form?
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Nov 22nd, 2009, 11:46 AM
#4
Thread Starter
Lively Member
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:
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim startX As Integer = (Screen.PrimaryScreen.WorkingArea.Width \ 2) - (Me.Width \ 2)
Dim startY As Integer = 50
Dim startPoint As New Point(startX, startY)
Me.Location = startPoint
End Sub
, then the form started to center correctly. It is the parent form.
-
Nov 22nd, 2009, 02:58 PM
#5
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
-
Nov 22nd, 2009, 03:13 PM
#6
Re: Form Not Centering
 Originally Posted by koolsid
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
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Nov 23rd, 2009, 12:13 PM
#7
Thread Starter
Lively Member
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:
Public Class MainForm
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim startX As Integer = (Screen.PrimaryScreen.WorkingArea.Width \ 2) - (Me.Width \ 2)
Dim starty As Integer = 50
Dim startPoint As New Point(startX, starty)
Me.Location = startPoint
End Sub
Private Sub MainForm_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
DrawLines()
End Sub
Private Sub MainForm_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
DrawLines()
End Sub
Private Sub DrawLines()
For i As Integer = 1 To 100
Randomize()
'create a random object and generate random line points
Dim randomGen As New Random
Dim intRandX As Integer = randomGen.Next(10, Me.ClientSize.Width - 10)
Dim intRandY As Integer = randomGen.Next(10, Me.ClientSize.Height - 10)
Dim intRandXX As Integer = randomGen.Next(10, Me.ClientSize.Width - 10)
Dim intRandYY As Integer = randomGen.Next(10, Me.ClientSize.Height - 10)
'create a graphics object to draw on the form
Dim formGraphics As Graphics
formGraphics = Me.CreateGraphics
'make a rgba(alpha,red,green,blue) pen with random colors, size, and dash style
Dim randAlpha As Integer = randomGen.Next(5, 255)
Dim randRed As Integer = randomGen.Next(0, 255)
Dim randBlue As Integer = randomGen.Next(0, 255)
Dim randGreen As Integer = randomGen.Next(0, 255)
Dim randSize As Integer = randomGen.Next(1, 10)
Dim randPen As New Pen(Color.FromArgb(randAlpha, randRed, randGreen, randBlue), randSize)
randPen.StartCap = Drawing2D.LineCap.Round
randPen.EndCap = Drawing2D.LineCap.Round
'pick dash style
Dim dashStyle As Integer = randomGen.Next(1, 5)
Select Case dashStyle
Case 1
randPen.DashStyle = Drawing2D.DashStyle.Solid
Case 2
randPen.DashStyle = Drawing2D.DashStyle.Dash
Case 3
randPen.DashStyle = Drawing2D.DashStyle.DashDot
Case 4
randPen.DashStyle = Drawing2D.DashStyle.DashDotDot
Case 5
randPen.DashStyle = Drawing2D.DashStyle.Dot
End Select
'draw line
formGraphics.DrawLine(randPen, intRandX, intRandY, intRandXX, intRandYY)
'dispose of graphics object
formGraphics.Dispose()
Next i
'clear the form
Me.Invalidate()
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|