Forms and focus

P

Per Rollvang

Hi all!

I have different forms displayed at the same time:

pseudocode:

formA displays formB using B.Show()

How do I assure that formA get focus when I close formB?



TIA
Per Rollvang
 
M

Mohamoss

Hi
if form A is the parent of form b , then it will get focus by default when
form b is closed .
you can make from A parent of form b " if this not the case " by using the
setParent() API function ....
you can use this function this way

using System.Runtime.InteropServices;

[DllImport("user32.dll",
EntryPoint="SetParent")]
public static extern bool
SetParent_DllImport(IntPtr child ,IntPtr parent );
private void Form1_Load(object sender, System.EventArgs e)
{
Form2 b = new Form2();
b.Show();
SetParent_DllImport(b.Handle ,this.Handle);
}
hope that would help.....

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
P

Per Rollvang

Hi there Mohamed,

I have a feeling SetParent will only display Form B "inside" Form A. Not
quite what I'm after... ;)

As long as I know the text on Form A, i guess I can cycle through all
parent-windows and search with 'GetText', but all those API's involved must
be overkill just to do this, am I right?

-Per

Mohamoss said:
Hi
if form A is the parent of form b , then it will get focus by default when
form b is closed .

Well, this is all fine as long there is no 'fiddling around' in the mean
time, and Form A is next to Form B in the z-order-queue
you can make from A parent of form b " if this not the case " by using the
setParent() API function ....
you can use this function this way

using System.Runtime.InteropServices;

[DllImport("user32.dll",
EntryPoint="SetParent")]
public static extern bool
SetParent_DllImport(IntPtr child ,IntPtr parent );
private void Form1_Load(object sender, System.EventArgs e)
{
Form2 b = new Form2();
b.Show();
SetParent_DllImport(b.Handle ,this.Handle);
}
hope that would help.....

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
M

Morten Wennevik

What about passing FormA as a reference when creating FormB, then have it
call FormA.Activate() when it is closing?
 
M

Mohamoss

Hi all..
what Morten is Suggesting seems a good solution ...................
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
P

Per Rollvang

This was such a trivial task in VS 6.0...

I'll give it a go...

Thanks a lot, guys!

Regards,
-Per
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi,

In Windows overlapped (forms are such a windows when TopLevel property is
set to true) and popup windows doesn't have parent. They have owner. Only
child windows have parent.
In Win32 API the same function si used to set the owner and the parent -
SetParent. In WndowsForms however we have to separate ways to set the parent
and the owner. For forms setting Parent doesn't make sense unless TopLevel
is set to *false*. Setting TopLevel to *false* chanages the form windows
styles and makes it a child window so it behaves as the other controls. If
you want to set owner/owned relation between forms set Form.Owner property.
 
R

Ravichandran J.V.

You can create a module level object
Public frmObj as Object

and reference the different forms with this object as you would while
implementing polymorphism. In the constructor of the forms set the
object to

frmObj=Me

'frmObj' will contain the reference of the form and its objects till you
change the reference to another form and will be available in all forms
of the project. So, even if you do hide a form the Form's Textbox value,
for example, will be persisted in the 'frmObj' object.

I hope this is what you are looking for.


with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
R

Ravichandran J.V.

Unless you are hiding the base form there should not be any problem. And
even if you are, if you set the FormA's IsMdiContainer to True then upon
closing the FormB your base form should get focus. So, what is the
problem ? Are you hiding the base form and then invoking the FormB?

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 

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