Bringing child window on top of parent window

D

DBC User

Hi all,

I have a parent window and it has a button. When I press the button, it
starts a long process in the back ground and I show a WinForm (Dialog
box) on top of the parent windowto the user to let them know the back
ground processing is running.

I have 2 questions
1. How can I keep the dialog box always on top of the parent window? (I
do not want to use "stayontop" since it will always keep the dialog box
on top even if I switch to another window). I want to appear only when
I switch to application.

2. Since the background process is a long process, I need to close the
window when the program returns control back. Right now I am killing
the active form and it seems to closing the dialog box. Is it a correct
approach? Is it possible to find a Form with its name and then close
that form in c#, like c++
winhandle = FindWindow("title");

Thanks in advance.
DBC
 
G

Guest

If I'm understanding you correctly you just need to display the child Form
(the one that you display to the user to show the process is running) as a
dialog using the ShowDialog() method instead of the Show() method. This will
mean that the second form is always ontop of the first one (the one with the
button).
 
D

DBC User

Thank you and it worked for my question (1). Could you please let me
know how can I get (2) accomplised?
 
G

Guest

It depends. If you are getting a callback from the thread that is running the
long process then you could just make the Form that is ontop an object level
field/variable and then when the callback event is called just call the Close
method of that form...

Something like this pseudocode:

class MyClass
{
ProcessingForm processingForm;

public void DoLongRunningEvent()
{
// Code to execute long running process...
// I am assuming that this is an asyhcnronous
// call through a delegate or something like this
// which allows you to specify a callback method

start long running process;

processingForm = new ProcessingForm();
processingForm.ShowDialog();
}

// Callback Method from the long running process
// this will execute on the same thread as the long running process if
// you have been using asynchronous delegates
private void Callback(IAsyncCallback ....)
{
if(processingForm != null && processingForm.Visible = true)
{
processingForm.Close();
}

}
}


This is just an example and pseudo code but it should give you some ideas.

Hope this helps too.
--
Brian Delahunty
Ireland

http://briandela.com/blog

INDA SouthEast - http://southeast.developers.ie/ - The .NET usergroup I
started in the southeast of Ireland.
 
D

DBC User

Thank you. Yes I am using the same model except, I didn't want to
declare the form as global variable in the class. But With your
suggestion I made it work and is working fine. One additional question
if you have time, is there a method where I can scan the available
windows to see if a particular window is open (like in c++
FindWindow())?

But other than that, thanks for the help.
 
G

Guest

Off the top of my head I don't think there is a FindWindow equivalent.
However, you could just use FindWindow itself by p/invoking it. Here is the
p/invoke signature:

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);


And here is some sample code:

======================
//C#
//Open Up blank Notepad First !
string lpszParentClass = "Notepad";
string lpszParentWindow = "Untitled - Notepad";
string lpszClass = "Edit";

IntPtr ParenthWnd = new IntPtr(0);
IntPtr hWnd = new IntPtr(0);
ParenthWnd = FindWindow(lpszParentClass,lpszParentWindow);
if (ParenthWnd.Equals(IntPtr.Zero))
Console.WriteLine("Notepad Not Running");
else
{
hWnd = FindWindowEx(ParenthWnd,hWnd,lpszClass,"");
if (hWnd.Equals(IntPtr.Zero))
Console.WriteLine("What the F??? Notepad doesn't have an edit component
?");
else
{
Console.WriteLine("Notepad Window: " + ParenthWnd.ToString());
Console.WriteLine("Edit Control: " + hWnd.ToString());
}
}
=======================

I've taken this from here:
http://www.pinvoke.net/default.aspx/user32/FindWindow.html

Hope this helps again :)


--
Brian Delahunty
Ireland

http://briandela.com/blog

INDA SouthEast - http://southeast.developers.ie/ - The .NET usergroup I
started in the southeast of Ireland.
 

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