Bring Form to the front.

G

Guest

hi, I have two applications, App1 and App2. When I click "switch to App2"
button, I will like to bring form of App2 to the front. Let's say Form of
App2 name Form2.

I calling Form2.Focus(), Form2.Show(), Form2.Refresh(), Form2.BringToFront()
and Form2.Visible=true. However none of them work except Form2.ShowDialog()

But ShowDialog basicly prevent user to go back to App1 until user enter
something in Form2 textbox.

Can anyone help? Thanks.
 
L

Lloyd Dupont

Poor you.
I've given up on that and basically avoid such behavior by avoiding anything
which could lead to such behavior.

However this is a very common, generally non-deterministic bug, so if you
have a clear deterministic sample where such bug arise, I do advice you to
file a bug report here:
http://lab.msdn.microsoft.com/productfeedback/
(MS is very responsive, you'll see)
Choose VisualC#, type your bug description and follows the wizard. In your
bug description give an hyperlink to where download your sample.


and let us all knows!
 
L

Lloyd Dupont

????
I have to test too.
I try many WinAPI call unsuccessfully (many variation on ShowWindow, I do
remember).
I don't remember if I test this one.
that would be great if it does work!

liu_wayne10 said:
hi: I find out a way to resolve that.
We need to use P/Invoke to invoke WinApi functions, the function is in
coredll.dll files. The function name is SetForegroundWindow(IntPtr Hwnd);
Where Hwnd is the window handle of the form. I catch that HWnd at the
form's load method by calling GetForegroundWindow().

The code looks like this, I simplify the code, but should work. Try it.

Wayne

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Itron.MRFSFD.Gui
{
#region SummaryFormHeader
//************************************************************
// Type Name: SummaryForm
/// <summary>
/// Summary description for SummaryForm.
/// </summary>
///
/// <remarks></remarks>
///
/// <authorName>Peter Dunn</authorName>
/// <creationDate>May 4, 2004</creationDate>
//************************************************************
#endregion SummaryFormHeader
public class SummaryForm : Form
{
#region Variables
private const int SW_SHOW = 5;
private IntPtr m_Hwnd;

public IntPtr Hwnd
{
get
{
return m_Hwnd;
}
}
#endregion Variables

#region CONSTRUCTORS
#region SummaryForm Method

public SummaryForm()
{
InitComponent();
}
#endregion SummaryForm Method
#endregion CONSTRUCTORS

#region Dispose Method
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#endregion Dispose Method

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitComponent()
{
this.Load += new System.EventHandler(this.SummaryForm_Load);
}
private void SummaryForm_Load(object sender, System.EventArgs e)
{
m_Hwnd = GetForegroundWindow();
}

#endregion Windows Form Designer generated code
#region Public Methods

public void ShowAsTopWindow()
{
Show();
SetForegroundWindow(Hwnd);
Refresh();
}

#endregion Public Methods

#region DllImport
[DllImport("coredll.dll")]
private static extern
IntPtr GetForegroundWindow();
static extern bool SetForegroundWindow(IntPtr hWnd);
#endregion DllImport
}
}


Lloyd Dupont said:
Poor you.
I've given up on that and basically avoid such behavior by avoiding anything
which could lead to such behavior.

However this is a very common, generally non-deterministic bug, so if you
have a clear deterministic sample where such bug arise, I do advice you to
file a bug report here:
http://lab.msdn.microsoft.com/productfeedback/
(MS is very responsive, you'll see)
Choose VisualC#, type your bug description and follows the wizard. In your
bug description give an hyperlink to where download your sample.


and let us all knows!
 

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