Q: Parent of a form

G

Geoff Jones

Hi

Suppose I have two forms: Form1 and Form2. I create and show an instance of
Form2 in the code of Form1 e.g.

Dim myForm2 = New Form2
myForm2.Show()

How do I tell myForm2 that Form1 is its parent? That is, when I look at
either "Parent" or "ParentForm" in Form2, they both show "Nothing".

Can anybody help???

Thanks in advance

Geoff
 
H

Herfried K. Wagner [MVP]

Geoff Jones said:
Suppose I have two forms: Form1 and Form2. I create and show an instance
of
Form2 in the code of Form1 e.g.

Dim myForm2 = New Form2
myForm2.Show()

How do I tell myForm2 that Form1 is its parent? That is, when I look at
either "Parent" or "ParentForm" in Form2, they both show "Nothing".

Do you want to show your form modally?

\\\
Dim MyForm2 As New Form2()
MyForm2.ShowDialog()
///
 
G

Geoff Jones

Hi

Thanks for your advice; although I don't think what you are suggesting is
exactly what I require.

In Form2, I have a progress control, which I am using to display the
progress of calculations in Form1. If I understand ShowDialog correctly,
using this, the program will stop within Form1. I suppose I could use
Threads, as others in the newsgroup have kindly suggested, however, I still
think there must be a way to do it using the technique I have described i.e.
show the form with the progress control in the first form, and increment the
control from within Form1 e.g.

theProgressControl.Increment(1) ' This is called within Form1

This does indeed work. However, I want the second form i.e. the one with the
progress control, to be centered in the first form. I've tried using:

myForm2.StartPosition = FormStartPosition.CenterParent

but this does not do what I expect i.e. it isn't centered on the first form.

Can anybody help?

Geoff
 
C

Cor Ligthert

Geoff,

I find this question (I have seen it more) forever weird, therefore I am
curious for the reason you do it this way and not just use an almost
foerever hidded panel that you hide and make visible.

The only thing you have to think than about is that you set the Zorder of
the panel right.

However maybe you have a special reason for the form and I am curious for
that. Will you tell why?

Cor
 
D

David Gacek

myForm2.StartPosition = FormStartPosition.CenterParent

works for me
maybe try to do it manually
 
G

Geoff Jones

Hi David

In which member do you write this line of code? Maybe I'm putting it in the
wrong place?

Geoff
 
G

Geoff Jones

Hi Cor

You're idea of using an forever hidden panel is interesting. I may give it a
go.

However, I am still puzzled why my code is not working. Being from a C++
background, I'm trying to understand VB.net and I can't understand why
CenterParent does not work. This, as with most things, has made me ask some
other questions as you have seen. For example, how do I tell Form2 that
Form1 is the calling routine i.e. it is the parent. I would have thought it
would have implicitly passed a pointer to Form2 to say that Form1 was the
parent. If it doesn't, how do I pass a pointer to Form2 to tell it that Form
1 is a pointer i.e. in C++ I would have passed a "this" pointer explicitly.

Geoff
 
G

Geoff Jones

Hi

I'm putting the line:

Me.StartPosition = FormStartPosition.CenterScreen

in Form1_Load to see if I can center Form1 as an experiment. It does not
work! So I must be placing it in the wrong place.

Can anybody enlighten me?

Geoff
 
G

Geoff Jones

I've just tried something else. If I write:

Me.WindowState = FormWindowState.Maximised

in Form1_Load, it does indeed maximise the window

BUT!!!!!

If I write

Me.StartPosition = FormStartPosition.CenterScreen

it doesn't work! i.e. the form is displayed not in the center!!!!

Is this a bug or am I going mad???????? LOL

Geoff
 
G

Geoff Jones

Hi David

Just to make it clear, I am doing all this manually i.e. I'm not setting the
Form location in the properties window of the IDE.

Geoff
 
L

Larry Serflaten

Geoff Jones said:
Is this a bug or am I going mad???????? LOL

There is always the concern that you do things in their proper order.

You have to have gas in the tank before turning the ignition key will
work to start the car, right?

It may be, the StartPosition property has already been used to position
the form, before you get to the Form_Load event. No matter what you
set it to in Form_Load, it won't effect the form's position because that
property may have already been used to position the form.

Try adding it to the form's New sub (In the Designer generated section)
or setting it via the IDE....

<g>
LFS
 
G

Geoff Jones

Hi Larry

I did a text search for StartPosition and it only occurs if I've specified
"CenterScreen" in the properties window i.e. if WindowsDefaultLocation is
selected by default then StartPosition does not appear in the code. So, as
you say, if I select CenterParent in the properties window, the startup form
is displayed in the center of the screen. If I do it manually in the code,
when the properties window shows WindowsDefaultLocation, and no StartPostion
(apart from the one I've added) is in the "Windows Forms Designer Generated
Code", then it doesn't work.

I'm going to list the steps that I've done again to see if I've done
something very silly (which is incredibly likely!!!):

(1) Start a new Windows Application
(2) In the Form1_Load member write:

Me.StartPosition = FormStartPosition.CenterScreen

(3) Run the application.

My understanding is that this should center the form in the screen. It
doesn't! However, if you set the FormStartPosition in the properties window
to CenterScreen, it does work!!!

Can anybody explain????

Geoff
 
G

Guest

Oh dear.

Much head scratching later; it seems that some bright spark (who's since
left) thought it'd be fun to delete some rows from the dataset at runtime.
Without documenting this behaviour.

Quite how that got past our entire department pointing at it and going
"eh?", I'm not sure.

Anyway, this is now resolved for me :)
 
L

Larry Serflaten

Geoff Jones said:
I'm going to list the steps that I've done again to see if I've done
something very silly (which is incredibly likely!!!):

(1) Start a new Windows Application
(2) In the Form1_Load member write:

Me.StartPosition = FormStartPosition.CenterScreen

(3) Run the application.

My understanding is that this should center the form in the screen. It
doesn't!

Did you miss this:

I then said:

So, open up the designer generated section (at the top of the code) and
make it look like this:

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
Me.StartPosition = FormStartPosition.CenterScreen
End Sub


LFS
 
M

Marco De Sanctis

You can add your form2 to form1 owned forms collection using the
form1.AddOwnedForm method and then you will be able to reference form1 using
form2.Owner property.

As a result, if you minimize or close form1, form2 too will be minimized or
closed. Moreover, Form1 will always be behind Form2.

Bye!
 
C

Cor Ligthert

Geoff,

Is it not that your idea of a parent is not what a parent is.
http://msdn.microsoft.com/library/d...systemwindowsformscontrolclassparenttopic.asp

When you want to center it to the screen, than it is in my opinion
calculating the point to screen from what you call your parent form and set
that in your child form.

When you want to set a reference do it in the same way as you said you do it
in C++ only here it is named. Me.

\\\
Private Sub Button1_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim frm As New Form2
frm.Text = "geoff"
frm.Height = Me.Height - 100
frm.Width = Me.Width - 100
Me.Location = New Drawing.Point _
(Me.Location.X + 50, Me.Location.Y + 50)
frm.parentform = Me 'the C this pointer and created as property on
Form2
' with this a reference is created to the mainform on the subform
frm.Show()
End Sub.
///
In my idea do you focus to much on the existing parent property, while it is
in my idea possible as well on the "child", just referencing the "me" from
the by you created"parentform" on your child form.

Cor
 
C

Cor Ligthert

Rowland,

I am glad this helps, however that could not be the error when you did all
those test on the point I asked you. Directly after the setting to of the
datasource.

And please add this message to the original thread, I saw it because I was
busy with a answer for Geof.

Cor
 
G

Geoff Jones

Hi Larry,

Ah, ha!!! Yes, that worked. This seems to make sense now but I'm surprised
that the documentation and various searches on this topic have not
highlighted that you have to add the code here for it to work. All the
examples I have seen just say use: StartPosition and CenterScreen.

Now, as usual, I'm awkward and going to ask a follow up question:

How do I make a form which is created in the first form, open up so that it
is centered on the first form? Okay, I know I could probably do the same
thing i.e. center it on the screen, but what if the first form is not
centered on the screen? I've tried setting the properties value of the
second form to:

FormStartPosition to CenterParent

but it doesn't work.

Any ideas?

Geoff
 
L

Larry Serflaten

Geoff Jones said:
How do I make a form which is created in the first form, open up so that it
is centered on the first form? Okay, I know I could probably do the same
thing i.e. center it on the screen, but what if the first form is not
centered on the screen? I've tried setting the properties value of the
second form to:

FormStartPosition to CenterParent

but it doesn't work.

Any ideas?


Sure, how about you tell it to center itself on the parent form?

Public Overloads Sub Show(ByRef Parent As Form)
Dim center As Size = New Size((Parent.Width - Me.Width) \ 2, _
(Parent.Height - Me.Height) \ 2)

Me.StartPosition = FormStartPosition.Manual
Me.Location = Point.op_Addition(Parent.Location, center)
Me.TopMost = True
Me.Show()
End Sub


Add that to your second form, and call it from your first form like:

Dim f2 As New Form2
f2.Show(Me)

Easy enough? <g>
LFS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top