473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Forcing a dialog box on top when the current form does not have focus.

Here is my problem, I have a Windows form (which may in the future be switched to WPF form) that gets minimized and hidden to the tray. At a set time an event is fired in a new thread which shows a dialog box that needs to be displayed on top of any current application windows for the user to interact with.

I've spent several hours researching and playing with various methods trying to get this to work. I would prefer not to resort to direct win32 API calls, but if I need to, its a possibility I haven't ruled out. (I'm currently using C# on Windows Vista SP1)

Any ideas on how to do this?
Aug 6 '08 #1
6 2603
Curtis Rutland
3,256 Expert 2GB
Perhaps you can create your own dialog form that has it's TopMost property set to true. Then just launch that when you need to.
Aug 6 '08 #2
Nope... doesn't work. If the parent form does not have focus, the dialog (even with topmost set to true) doesn't automatically get focus over top of other windows (firefox for example).
Aug 7 '08 #3
Curtis Rutland
3,256 Expert 2GB
Hmm. Well, that's no good. I'd never tried it, it was just an idea I had.
Aug 7 '08 #4
Hmm. Well, that's no good. I'd never tried it, it was just an idea I had.
No problem,

I finally figured out a solution which involves hitting the unmanaged User32.DLL, not the prettiest solution when your writting in .NET, but it gets the job done.

C# functions to get and set the focus of the window on top of all the other windows:
Expand|Select|Wrap|Line Numbers
  1.         //     CLR   wrapping of unmanaged functions
  2.  
  3.         public static int GetWindowHandle(System.String WindowTitle)
  4.         {
  5.             //a return value of 0 indicates that the function did not find the window
  6.             return FindWindow(null, WindowTitle);
  7.         }
  8.  
  9.         public static void SetWindowFocus(int windowHandle)
  10.         {
  11.             SetForegroundWindow(windowHandle);
  12.         }
  13.  
  14.  
  15.         //DLL external functions hitting User32.dll
  16.  
  17.         [DllImport("User32.dll")]
  18.         private static extern Int32 FindWindow(String lpClassName, String lpWindowName);
  19.  
  20.  
  21.         [DllImport("User32.dll")]
  22.         private static extern Boolean SetForegroundWindow( Int32 hWnd );
  23.  
  24.  
  25.  
Aug 7 '08 #5
Curtis Rutland
3,256 Expert 2GB
Good to know. I'll probably use that solution myself.
Aug 7 '08 #6
Plater
7,872 Expert 4TB
What happens if you toss a MessageBox In there? Those darn things love to popup over the top of everything else. And clicking the "ok" button them will force your application into focus.
Aug 7 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dalan | last post by:
I designed a dialog box with a combo to select individual vendors from a form and its related data for print ouput. Though the dialog box seems to work okay, I apparently do not have the filtering...
2
by: aaj | last post by:
Hi all I have a small but rather annoying problem with continuos forms, and am wondering if anyone can suggest a method of getting over it. The front end is Access 2002 with the BE being SQL...
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
3
by: Gordan A Ziza | last post by:
Hello everyone, We are developing a MDI application and what we would require is the following: 1. To open a MDI child form as Form1 2. To open another MDI child form as Form2 that...
1
by: pw | last post by:
Hi, I have a form (with an ActiveX control on it - don't know if that is the problem) and I can not see the dialog/message boxes. There is an error with my form and I can not see what it is...
0
by: Ralstoj | last post by:
Hi I am programing in Autocad with VB Autodesk have not given users access to new note function in Autocad CIVIL3d API. I am trying to work round this by creating notes using the sendkey...
11
by: Zytan | last post by:
I have created a new form from the main form. When I close the main form with the 'x' close button, its Form.FormClosed event is run, but not the dialog's. Is this normal? It is ok /...
5
by: GraffixNYC | last post by:
We have a model dialog box(well a bunch of them) and it seems that when you cancel one of the in our application it will activate and bring the focus to another open window (not in our application)...
11
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.