Can't bring form to front

A

Anders Thomsen

Hi ng,

I have a problem when trying to focus certain forms.

My application consists of several forms, at first form1 is shown. Clicking
a button here will close form1 and show form2. When form2 i showing, I try
to open the File manager in PPC. From here, I can navigate to my project
folder and open my application by clicking on the .exe. This opens up the
app where I left it, showing form2 - as suspected.

Then I click a button on form2 which closes form2 and create a new instance
of Form1 (the first instance was disposed and it's reference set to null).
On this new instance i've tried to call BringtoFront(), Focus(), Show and
Visible = true, but I simply can't get the form to show. Instead, the file
manager is shown - which is okay, since it's the next form in the Z-order
after form2. But after creating Form1 and calling BringToFront etc. I
believe that form1 should be put on top.

How can I activate form1?

Thanks in advantage,
 
J

Jan Yeh

Hi, there

I am wondering when you click the button on form2, it closes form2 itself
AND
create a new instance of form1, is that right?
If correct, the the sequence of statement is important, for example:
<in form2 instance>
this.Close(); // When it close, who will keep the reference of frm?
form1 frm=new form1();
frm.Show();

Another exampler:
<in form2 instance>
form1 frm=new form1();
frm.Show();
this.Close(); // When it close, it dispose everything in form2, including
frm.

The result of above will cause frm instance disappear, as you've seen.

----------------------------------------------------------------------------
--
You may try the following way.
<in form1 instance>
private void Button1_Click(
form2 frm2=new form2(this); // pass the handle of form1 to form2
frm2.Show();
this.Hide();
<in form2 instance>
private form1 f=null;
public form2(form1 frm)
{
f=frm; // form2 will take the handle of form1 instance
}

private void Button1_Click(object sender, EventArgs e)
{
f.Show();
this.Hide(); // or this.Close();
// then it will back to the original form1, instead of a new instance.
}
 
A

Anders Thomsen

Actually, it's not form2 who creates form1, but some underlying handlers.
The form1 IS there, as it is focused when I minimize the filemanager.
 
M

Mark Arteaga

To set focus on a form I had to P/Invoke to get it to work properly. Try
this code:

[DllImport("coredll",EntryPoint="FindWindow")]
public static extern IntPtr FindWindow(string lpClassName,string
lpWindowName);
[DllImport("coredll",EntryPoint="SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

IntPtr hWnd = FindWindow(null,form1.Text);
if(!hWnd.Equals(IntPtr.Zero))
SetForegroundWindow(hWnd);

HTH
Mark Arteaga
www.clearknowledge.com
 
A

Anders Thomsen

Okay, thanks.

Do you know if there's another way to get the handle of the form - many of
my forms has
the same title.

-and thanks again : )

Mark Arteaga said:
To set focus on a form I had to P/Invoke to get it to work properly. Try
this code:

[DllImport("coredll",EntryPoint="FindWindow")]
public static extern IntPtr FindWindow(string lpClassName,string
lpWindowName);
[DllImport("coredll",EntryPoint="SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

IntPtr hWnd = FindWindow(null,form1.Text);
if(!hWnd.Equals(IntPtr.Zero))
SetForegroundWindow(hWnd);

HTH
Mark Arteaga
www.clearknowledge.com

Anders Thomsen said:
Actually, it's not form2 who creates form1, but some underlying handlers.
The form1 IS there, as it is focused when I minimize the filemanager.
 
M

Mark Arteaga

yup....on the form set Capture=ture then p/invoke get capture. this will
return the handle.

[DllImport("coredll.dll", EntryPoint="GetCapture", SetLastError=true)]
private static extern IntPtr GetCapture();

HTH
Mark.
Anders Thomsen said:
Okay, thanks.

Do you know if there's another way to get the handle of the form - many of
my forms has
the same title.

-and thanks again : )

Mark Arteaga said:
To set focus on a form I had to P/Invoke to get it to work properly. Try
this code:

[DllImport("coredll",EntryPoint="FindWindow")]
public static extern IntPtr FindWindow(string lpClassName,string
lpWindowName);
[DllImport("coredll",EntryPoint="SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

IntPtr hWnd = FindWindow(null,form1.Text);
if(!hWnd.Equals(IntPtr.Zero))
SetForegroundWindow(hWnd);

HTH
Mark Arteaga
www.clearknowledge.com

Anders Thomsen said:
Actually, it's not form2 who creates form1, but some underlying handlers.
The form1 IS there, as it is focused when I minimize the filemanager.



Hi, there

I am wondering when you click the button on form2, it closes form2 itself
AND
create a new instance of form1, is that right?
If correct, the the sequence of statement is important, for example:
<in form2 instance>
this.Close(); // When it close, who will keep the reference of frm?
form1 frm=new form1();
frm.Show();

Another exampler:
<in form2 instance>
form1 frm=new form1();
frm.Show();
this.Close(); // When it close, it dispose everything in form2,
including
frm.

The result of above will cause frm instance disappear, as you've seen.
 
L

Lloyd Dupont

When I try to deploy my project with VS.NET on the device (PocketPC 2002) OR
on the emulator TOO !,
VS.NET gives me this error while copying the files:
"Error: Cannot establish a connection. Be sure the device is physically
connected to the development computer."

therefore I can't debug, well I could copy the application and see what
happen....
but it would be better if I could use the debugger !
 

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