manually set form position to center on another form

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 43937 Views - Last Post: 07 March 2012 - 05:12 AM Rate Topic: -----

#1 timosilver   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 20-March 11

manually set form position to center on another form

Posted 04 March 2012 - 09:22 AM

Hello

i have my main form and other forms. the other forms startposition is set to manual.
is there a way i can set the start position of the other forms to center at the main form
Me.Location = New Point(150, 248)

the above code will position according to the monitor not the mainform.

Thanks for your help
Is This A Good Question/Topic? 0
  • +

Replies To: manually set form position to center on another form

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15331
  • View blog
  • Posts: 61,451
  • Joined: 12-June 08

Re: manually set form position to center on another form

Posted 04 March 2012 - 09:51 AM

In the designer the forms have a 'start position' and an option for setting to parent should be in there..
Was This Post Helpful? 0
  • +
  • -

#3 timosilver   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 20-March 11

Re: manually set form position to center on another form

Posted 04 March 2012 - 10:07 AM

tried setting the startpostion to centerparent but it didnt work as the form starts at different location. i also set the mainform to isMDIcontainer.
Was This Post Helpful? 0
  • +
  • -

#4 nK0de   User is offline

  • Catch me As Exception
  • member icon

Reputation: 206
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: manually set form position to center on another form

Posted 04 March 2012 - 11:32 AM

Here's a solution I came across. Tried it and works fine.

This post has been edited by nK0de: 04 March 2012 - 11:34 AM

Was This Post Helpful? 0
  • +
  • -

#5 timosilver   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 20-March 11

Re: manually set form position to center on another form

Posted 04 March 2012 - 11:57 AM

thanks but this is C# or VB.net because am having some errors at SmallForm() 'type SmallForm is not defined'

Dim MyForm As New SmallForm()
        MyForm.MdiParent = DirectCast(Me, Form1) '/// set MyForm's parent here.
        MyForm.Show()
        Dim x As Integer = (Me.Width / 2) - (MyForm.Width / 2)
        Dim y As Integer = (Me.Height / 2.2) - (MyForm.Height / 2) '/// allow extra for the toolbox ( hence 2.2 )
        MyForm.Location = New Point(x, y)'///Center the form in it's parent. 

This post has been edited by timosilver: 04 March 2012 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: manually set form position to center on another form

Posted 04 March 2012 - 12:04 PM

OK, so reading through the code... What can you see this code doing? How many different forms are involved in this code sample?

Once you understand that code, it should be easy for you to see what you need to do.

Right now, it appears you're just copy & pasting solutions into your application with no understanding of what they are doing.

This post has been edited by CharlieMay: 04 March 2012 - 12:05 PM

Was This Post Helpful? 0
  • +
  • -

#7 timosilver   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 20-March 11

Re: manually set form position to center on another form

Posted 04 March 2012 - 12:09 PM

there are two forms here myform and form1. form1 is the parent form
while myform is the small form or child form.
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: manually set form position to center on another form

Posted 04 March 2012 - 12:12 PM

OK, so I assume you have 2 forms, one of those is the parent. You are probably using the default instance so no need to instantiate a new form as this code is doing. Try it with your form names and removing line 1.

This post has been edited by CharlieMay: 04 March 2012 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#9 nK0de   User is offline

  • Catch me As Exception
  • member icon

Reputation: 206
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: manually set form position to center on another form

Posted 04 March 2012 - 12:12 PM

actually SmallForm is the child form. myform is an instance of the SmallForm.
Was This Post Helpful? 0
  • +
  • -

#10 timosilver   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 20-March 11

Re: manually set form position to center on another form

Posted 04 March 2012 - 12:18 PM

yes.. i have this
(Me, Main1) Main1 is the parent form
error on line one
"form1 cannot be converted to Main1"
 MdiParent = DirectCast(Me, Main1) '/// set MyForm's parent here.
        Dim x As Integer = (Me.Width / 2) - (Width / 2)
        Dim y As Integer = (Me.Height / 2.2) - (Height / 2) '/// allow extra for the toolbox ( hence 2.2 )
        Location = New Point(x, y) '///Center the form in it's parent. 

Was This Post Helpful? 0
  • +
  • -

#11 nK0de   User is offline

  • Catch me As Exception
  • member icon

Reputation: 206
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: manually set form position to center on another form

Posted 04 March 2012 - 12:24 PM

you're missing something in line 2 and 3. second part of your calculation. check again with the code sample.
Was This Post Helpful? 0
  • +
  • -

#12 timosilver   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 20-March 11

Re: manually set form position to center on another form

Posted 04 March 2012 - 12:28 PM

yes i omitted the child form name from the width and height because it gave me an error 'form cannot refer to it self' hold. where am i supposed to have this code? the parent form of the child form?
Was This Post Helpful? 0
  • +
  • -

#13 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: manually set form position to center on another form

Posted 04 March 2012 - 12:45 PM

are you sure it didn't say form cannot refer to itself, use Me instead?
Was This Post Helpful? 0
  • +
  • -

#14 timosilver   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 20-March 11

Re: manually set form position to center on another form

Posted 04 March 2012 - 12:51 PM

thats true
but still having an error on line 1 the me is under lined
error: ' value of type form1 cannot be converted to Main1'

        Me.MdiParent = DirectCast(Me, Main1) '/// set MyForm's parent here.
        Me.Show()
        Dim x As Integer = (Me.Width / 2) - (Me.Width / 2)
        Dim y As Integer = (Me.Height / 2.2) - (Me.Height / 2) '/// allow extra for the toolbox ( hence 2.2 )
        Me.Location = New Point(x, y) '///Center the form in it's parent. 


Was This Post Helpful? 0
  • +
  • -

#15 nK0de   User is offline

  • Catch me As Exception
  • member icon

Reputation: 206
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: manually set form position to center on another form

Posted 04 March 2012 - 01:07 PM

where are you putting this code? In which form?
and still you're wrong at line number 3 and 4. (Me.Width / 2) - (Me.Width / 2), that doesn't make sense. It should be (<main form>.Width / 2) - (<child form>.Width / 2) Same with the height.

This post has been edited by nK0de: 04 March 2012 - 01:10 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3