Printing multiple textboxes from the form.
I want to print textboxes that stand next to eachother. one to the left, one to the right, each filling half the page, and also one directly under them.
I cant seem to get the text boes to shift in the alignment though like I can in my output display page. In that the windows form displays them properly but on print it just shoves them on the same lines.
Is there a way to manage it so textboxes print in the same location they are in the form?
Here is my print code. Bare with me I am a very early programmer and this project is actually the first I have ever printed from :)
private void printDocument2_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics g = e.Graphics;
Graphics g2 = e.Graphics;
textBox1.Text = "Closers: " + Closers +
"\r\n" +
"\r\n" +
"In Hundreds you have " + string.Format("{0:C}{1}", tHundreds, ".") +
"\r\nIn Fifties you have " + string.Format("{0:C}{1}", tFifties, ".") +
"\r\nIn Twenties you have " + string.Format("{0:C}{1}", tTwenties, ".") +
"\r\nIn Tens you have " + string.Format("{0:C}{1}", tTens, ".") +
"\r\nIn Fives you have " + string.Format("{0:C}{1}", tFives, ".") +
"\r\nIn Twos you have " + string.Format("{0:C}{1}", tTwos, ".") +
"\r\nIn Ones you have " + string.Format("{0:C}{1}", tOnes, ".") +
"\r\n" +
"\r\n" +
"\r\nYour total in bills is " + string.Format("{0:C}{1}", tBillTotal, ".") +
"\r\n" +
"\r\n" +
"\r\nIn Wrapped you have " + string.Format("{0:C}{1}", tWrapped, ".") +
"\r\nIn Gold/Silver Dollars you have " + string.Format("{0:C}{1}", tDollars, ".") +
"\r\nIn Half Dollars you have " + string.Format("{0:C}{1}", tHalfDollar, ".") +
"\r\nIn Quarters you have " + string.Format("{0:C}{1}", tQuarters, ".") +
"\r\nIn Dimes you have " + string.Format("{0:C}{1}", tDimes, ".") +
"\r\nIn Nickels you have " + string.Format("{0:C}{1}", tNickels, ".") +
"\r\nIn Pennies you have " + string.Format("{0:C}{1}", tPennies, ".") +
"\r\n" +
"\r\n" +
"\r\nYour total in change is " + string.Format("{0:C}{1}", tChangeTotal, ".") +
"\r\n" +
"\r\n" +
"\r\nYour total currency is " + string.Format("{0:C}{1}", tTotalCurrency, ".") +
"\r\n" +
"\r\n" +
"\r\nThe deposit is " + string.Format("{0:C}{1}", tDeposit, ".");
textBox2.Text = DateTime.Now.ToLongDateString() +
"\r\n" +
"\r\n" +
"In Hundreds you have " + string.Format("{0:C}{1}", tHundreds2, ".") +
"\r\nIn Fifties you have " + string.Format("{0:C}{1}", tFifties2, ".") +
"\r\nIn Twenties you have " + string.Format("{0:C}{1}", tTwenties2, ".") +
"\r\nIn Tens you have " + string.Format("{0:C}{1}", tTens2, ".") +
"\r\nIn Fives you have " + string.Format("{0:C}{1}", tFives2, ".") +
"\r\nIn Twos you have " + string.Format("{0:C}{1}", tTwos2, ".") +
"\r\nIn Ones you have " + string.Format("{0:C}{1}", tOnes2, ".") +
"\r\n" +
"\r\n" +
"\r\nYour total in bills is " + string.Format("{0:C}{1}", tBillTotal2, ".") +
"\r\n" +
"\r\n" +
"\r\nIn Wrapped you have " + string.Format("{0:C}{1}", tWrapped2, ".") +
"\r\nIn Gold/Silver Dollars you have " + string.Format("{0:C}{1}", tDollars2, ".") +
"\r\nIn Half Dollars you have " + string.Format("{0:C}{1}", tHalfDollar2, ".") +
"\r\nIn Quarters you have " + string.Format("{0:C}{1}", tQuarters2, ".") +
"\r\nIn Dimes you have " + string.Format("{0:C}{1}", tDimes2, ".") +
"\r\nIn Nickels you have " + string.Format("{0:C}{1}", tNickels2, ".") +
"\r\nIn Pennies you have " + string.Format("{0:C}{1}", tPennies2, ".") +
"\r\n" +
"\r\n" +
"\r\nYour total in change is " + string.Format("{0:C}{1}", tChangeTotal2, ".") +
"\r\n" +
"\r\n" +
"\r\nYour total currency is " + string.Format("{0:C}{1}", tTotalCurrency2, ".") +
"\r\n" +
"\r\n" +
"\r\nThe deposit is " + string.Format("{0:C}{1}", tDeposit2, ".");
Font messageFont = new Font("Arial Black", 12, System.Drawing.GraphicsUnit.Point);
g.DrawString(textBox1.Text, messageFont, Brushes.Black, 25, 25);
g2.DrawString(textBox2.Text, messageFont, Brushes.Black, 25, 25);
}
//closes method.It prints just fine it just prints them in the same spot on the page.
Rick DollPosted Jun 27, 2007, 12:53 AM
Rick DollPosted Jun 26, 2007, 11:49 PM
I must simply be missing something here.
I used part of your code there, took out the checkbox stuff, renamed wages to textBox1(name of the box) and tried to have it just print that single box and it never prints. Tells me Object Referance not set to an instance of an object with this error string:
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at Money_Talks.DayTotals.DrawAll(Graphics g) in C:\Documents and Settings\Main\My Documents\Visual Studio 2005\Projects\Money Talks\Money Talks\DayTotals.cs:line 399
at Money_Talks.DayTotals.printDocument2_PrintPage(Object sender, PrintPageEventArgs e) in C:\Documents and Settings\Main\My Documents\Visual Studio 2005\Projects\Money Talks\Money Talks\DayTotals.cs:line 502
at System.Drawing.Printing.PrintDocument.OnPrintPage(PrintPageEventArgs e)
at System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e)
at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document)
at System.Drawing.Printing.PrintController.Print(PrintDocument document)
at System.Drawing.Printing.PrintDocument.Print()
at Money_Talks.DayTotals.OutputMethod() in C:\Documents and Settings\Main\My Documents\Visual Studio 2005\Projects\Money Talks\Money Talks\DayTotals.cs:line 380
at Money_Talks.DayTotals.DayTotals_Load(Object sender, EventArgs e) in C:\Documents and Settings\Main\My Documents\Visual Studio 2005\Projects\Money Talks\Money Talks\DayTotals.cs:line 392
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
My code I have in that DrawAll method now is:
private void DrawAll(Graphics g)
{
// Create the source rectangle from the BackgroundImage Bitmap Dimensions
RectangleF srcRect = new Rectangle(0, 0, this.BackgroundImage.Width, BackgroundImage.Height); // Create the destination rectangle from the printer settings holding printer page dimensions int nWidth = printDocument2.PrinterSettings.DefaultPageSettings.PaperSize.Width; int nHeight = printDocument2.PrinterSettings.DefaultPageSettings.PaperSize.Height; RectangleF destRect = new Rectangle(0, 0, nWidth, nHeight / 2); // Draw the image scaled to fit on a printed pageg.DrawImage(
this.BackgroundImage, destRect, srcRect, GraphicsUnit.Pixel); // Determine the scaling factors of each dimension based on the bitmap and the printed page dimensions // These factors will be used to scale the positioning of the contro contents on the printed form float scalex = destRect.Width / srcRect.Width; float scaley = destRect.Height / srcRect.Height; Pen aPen = new Pen(Brushes.Black, 1); // Cycle through each control. Determine if it's a checkbox or a textbox and draw the information inside // in the correct position on the form for (int i = 0; i < this.Controls.Count; i++){
// Check if its a TextBox type by comparing to the type of one of the textboxes if (Controls[i].GetType() == this.textBox1.GetType()){
// Unbox the Textbox TextBox theText = (TextBox)Controls[i]; // Draw the textbox string at the position of the textbox on the form, scaled to the print pageg.DrawString(theText.Text, theText.Font,
Brushes.Black, theText.Bounds.Left * scalex, theText.Bounds.Top * scaley, new StringFormat());}
}
}
Mike GoldPosted Jun 26, 2007, 3:06 PM