473,383 Members | 1,876 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,383 software developers and data experts.

bring printdialogbox to front

13
Hi,

I have this problem of the printdialogbox hiding behind my main form. The very first time i call the printdialogbox it comes to the top but subsequent calls always appear behind the main form.

How do you bring the printdialogbox to the front everytime it is called?
Thanks,
ET
Nov 4 '09 #1

✓ answered by Plater

PrintPreviewDialog has a .Show() and .ShowDialog()
PrintDialog only has .ShowDialog()

ShowDialog() is "supposed" to be topmost (of its owner form)

8 7614
EddieT
13
Just solved the problem i face which is kind of an isolated problem. anyway here is what happened.

The problem is due to the calling of the printdialogbox within a timerelapsed event - meaning when I hit the print button, the printdialogbox does not immediately launch. i needed a delay of 0.1seconds and so i added a timerelpased event of 0.1s. After 0.1s i start to call the printdialogbox within the timerelapsed routine. Because the printdialogbox is nested in a timerelapsed routine, the printdialogbox doesnt want to appear in front of my form.

My solution then was to call the printdialogbox immediately after the print button is hit but not the print action. I then start the 0.1seconds timer. After 0.1second elapses, the timerelapsedevent is called for which then only I call the printdoc.print to initiate the printing. Doing this avoids the printdialogbox being called inside the timerelapsed routine and this seems to have got rid of the problem.

here is my code for anyone who faced this problem.
Expand|Select|Wrap|Line Numbers
  1.  
  2. private void printToolStripMenuItem_Click(object sender, EventArgs e)
  3.         {
  4.             PrintDialog PrintDialog1 = new PrintDialog();
  5.             PrintDialog1.Document = printDoc;
  6.             if (PrintDialog1.ShowDialog() == DialogResult.OK)
  7.             {
  8.                 System.Timers.Timer timer = new System.Timers.Timer(100);
  9.                 timer.Elapsed += new ElapsedEventHandler(timer_Elapsed_Print);
  10.                 timer.Start();
  11.             }
  12.  
  13.         }
  14.  
  15.         private void timer_Elapsed_Print(object sender, ElapsedEventArgs e)
  16.         {
  17.             System.Timers.Timer tm = (System.Timers.Timer)sender;
  18.             tm.Stop();
  19.             tm.Dispose();
  20.             Graphics g1 = this.CreateGraphics();
  21.             Image MyImage = new Bitmap(this.image.Width, this.image.Height, g1);
  22.             Graphics g2 = Graphics.FromImage(MyImage);
  23.             IntPtr dc1 = g1.GetHdc();
  24.             IntPtr dc2 = g2.GetHdc();
  25.             BitBlt(dc2, 0, 0, this.image.Width, this.image.Height, dc1, 0, 46, 13369376);
  26.             g1.ReleaseHdc(dc1);
  27.             g2.ReleaseHdc(dc2);
  28.             MyImage.Save(@"c:\PrintPage.wmf", ImageFormat.Wmf);
  29.             FileStream fileStream = new FileStream(@"c:\PrintPage.wmf", FileMode.Open, FileAccess.Read);
  30.             this.streamToPrint = fileStream;
  31.             this.streamType = "Image";
  32.             printDoc.Print();
  33.             fileStream.Close();
  34.         }
  35.  
  36.  
  37.         private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  38.         {
  39.             System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
  40.             int width = image.Width;
  41.             int height = image.Height;
  42.  
  43.             if (((double)width / (double)(e.PageBounds.Width - 40)) > ((double)height / (double)e.PageBounds.Height))
  44.             {
  45.                 width = (e.PageBounds.Width - 40);
  46.                 height = Convert.ToInt16((double)height * (double)(e.PageBounds.Width - 40) / (double)image.Width);
  47.             }
  48.             else
  49.             {
  50.                 height = e.PageBounds.Height;
  51.                 width = Convert.ToInt16((double)width * (double)e.PageBounds.Height / (double)image.Height);
  52.             }
  53.             System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(0, 0, width, height);
  54.             e.Graphics.DrawImage(image, destRect, 0, 0, Convert.ToInt16(image.Width),
  55.                 Convert.ToInt16(image.Height), System.Drawing.GraphicsUnit.Pixel);
  56.         }
  57.  
Nov 4 '09 #2
tlhintoq
3,525 Expert 2GB
Did you try setting the .TopMost property to true?
Nov 4 '09 #3
EddieT
13
Dialogboxes do not have .TopMost property
Nov 4 '09 #4
Plater
7,872 Expert 4TB
If you are not allowing the user to change anything in the printdialog, why bother showing it at all?

Also, I think if you specify the owner form in the .ShowDialog(IWin32Window Owner) function overload it will do a better job at popping on top?
Nov 4 '09 #5
tlhintoq
3,525 Expert 2GB
Dialogboxes do not have .TopMost property
Some do.
Expand|Select|Wrap|Line Numbers
  1. PrintPreviewDialog myppd = new PrintPreviewDialog();
  2. myppd.TopMost = true;
  3.  
I tend to do print previews in my app so I have gotten used to using it. I didn't realize that the PrintDialog doesn't have this property. Weird really when you think about it.
Nov 4 '09 #6
Plater
7,872 Expert 4TB
PrintPreviewDialog has a .Show() and .ShowDialog()
PrintDialog only has .ShowDialog()

ShowDialog() is "supposed" to be topmost (of its owner form)
Nov 4 '09 #7
EddieT
13
ShowDialog() is "supposed" to be topmost (of its owner form)
I agree, but i did something abnormal which is having the printdialog.showdialog() called from a timerelapsed event(meaning i call the printdialog.showdialog() some delay later). I need to do this because i am 'sreencapturing' my picture box image and without the delay i will see the print menu being part of my 'screen capture'. The delay just provides enough time for the print menu to disaapear before 'screen capture'. This is a cheap way of printing images on the picture box. Until i have time to do it properly i will keep it this way.

This leads to another question:
Can you have any Form placed on Top of the Task Manager without manually changing the Options-->AlwaysOnTop setting to False in the Task Manager menu?
Or better still is it possible to use Csharp to change the Options-->AlwaysOnTop to False from my program?

(Task Manager are usually set by default to be OnTop of all Windows.)
Nov 4 '09 #8
tlhintoq
3,525 Expert 2GB
@Plater
Great tip! I tried this with a different dialog type (a MessageBox) that would occassionally be stuck behind some other application and therefore not be seen.

I found that combining this along with raising my application to TopMost makes sure that the MessageBox gets the user's attention.

Expand|Select|Wrap|Line Numbers
  1. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  2. {
  3.     this.TopMost = true; // so that our dialog will be on top and seen.
  4.     System.Windows.Forms.DialogResult result = MessageBox.Show(this,"Are you sure", "Close?", 
  5.                                                                MessageBoxButtons.YesNo,
  6.                                                                MessageBoxIcon.Question);
  7.     if (result == System.Windows.Forms.DialogResult.No) e.Cancel = true; // Cancel the close
  8. }
Nov 5 '09 #9

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

Similar topics

7
by: tom | last post by:
Hi, I want a listbox below a hidden calendar control. The problem is that the listbox will shadow the calendar when the calendar is visible. How can I bring the calendar from back to front? ...
0
by: ACaunter | last post by:
HI there.. I have a dropdown menu for my asp.net website (not a combo box with the down arrow).. its for Home, Compant, ... , Contact Us... and then when the mouse goes over it, the sub menus are...
9
by: DraguVaso | last post by:
Hi, I want my application to bring another application to the Front. I thought best way to do this was by the Process-model: Dim c As Process = Process.GetCurrentProcess() Dim p As Process...
2
by: Don | last post by:
I have two programs. One is supposed to bring the other (which is already running either minimized or at the bottom of the window z-order) to the front so that it becomes the foremost application....
3
by: M O J O | last post by:
Hi, I have an application where I've implemented a global hotkey, so no matter what other application is in front (have focus), my app will react when the key combination is pressed. This works...
5
by: Iain Bishop | last post by:
I have a simple form with 4 command buttons and 1 label. The label is sometimes visible and sometimes not. When it is visible I want it to be in front of the buttons. I've tried bringing the label...
1
by: Marcel Brekelmans | last post by:
Hello, I have an application with a notifyIcon. When my application's main form is hidden by some other window I would like to bring it in front by single-clicking the NotifyIcon. However, I...
2
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, I asked here yesterday about bringing a form to front with hotkey while using different application then mine (meaning, when i'm using outlook, and pressing ALT+T it will bring a form from...
1
by: swethak | last post by:
Hi, How to do the bring front and bring back an images using javascript.
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.