Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
i have this code in c++ windows form application... but it print blank page... how to fix this?
what im doing wrong?
 
#pragma endregion
		// Declare the PrintDocument object.

	private: System::Void Button1_Click_1(System::Object^  sender, System::EventArgs^  e) {
   PrintDialog1->AllowSomePages = true;
 
   PrintDialog1->ShowHelp = true;
 
   PrintDocument ^docToPrint = gcnew PrintDocument;
   PrintDialog1->Document = docToPrint;
   if ( docToPrint == nullptr )
         System::Windows::Forms::MessageBox::Show(  "null" );
 
   
   
   if ( PrintDialog1 == nullptr )
         System::Windows::Forms::MessageBox::Show(  "pnull" );
 
   
   
   System::Windows::Forms::DialogResult result = PrintDialog1->ShowDialog();
   System::Windows::Forms::MessageBox::Show( result.ToString() );
   
   
 
   if ( result == System::Windows::Forms::DialogResult::OK )
   {
	   docToPrint->Print();
   }
 
			 }
private: System::Void document_PrintPage_1(System::Object^  sender, System::Drawing::Printing::PrintPageEventArgs^  e) {
   String^ text = "In document_PrintPage method.";
   System::Drawing::Font^ printFont = gcnew System::Drawing::Font( "Times New Roman",20,System::Drawing::FontStyle::Bold );
 
   e->Graphics->DrawString("EAK TEAM TEST!!!",printFont,Brushes::Black,50,50);
   e->Graphics->DrawString( text, printFont, System::Drawing::Brushes::Black, 10, 10 );
		 }
Posted 16-Apr-13 9:08am
(unknown member)
Comments
Sergey Alexandrovich Kryukov at 16-Apr-13 14:40pm
   
Did you try to debug it? Place a break point at DrawString, see what happens...
—SA
eakteam at 16-Apr-13 14:52pm
   
i try... but nothing
Sergey Alexandrovich Kryukov at 16-Apr-13 14:55pm
   
"but nothing" is not informative. If you need help, you have to collaborate...
—SA
ThePhantomUpvoter at 16-Apr-13 14:47pm
   
You need to actually read the text for the sample code you copy from, "This example requires that the button's Click event and the PrintPage event of docToPrint have been connected to the event-handling methods defined in this example." Did you wire the document print page event up?
eakteam at 16-Apr-13 14:55pm
   
Button1 even : this->Button1->Click += gcnew System::EventHandler(this,&Form1::Button1_Click_1);
printpage event : this->document->PrintPage += gcnew System::Drawing::Printing::PrintPageEventHandler(this, &Form1::document_PrintPage_1);
eakteam at 16-Apr-13 15:01pm
   
ThePhantomUpvoter thank you for ur time ... this is the full code :
 

#pragma once
 

namespace printing3 {
 
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Drawing::Printing;
using namespace System::IO;
 
///
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
///

public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
 
protected:
///
/// Clean up any resources being used.
///

~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::PrintDialog^ PrintDialog1;
private: System::Windows::Forms::Button^ Button1;
private: System::Drawing::Printing::PrintDocument^ document;
private: System::Windows::Forms::Button^ button2;
 

protected:
 
private:
///
/// Required designer variable.
///

System::ComponentModel::Container ^components;
 
#pragma region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

void InitializeComponent(void)
{
this->PrintDialog1 = (gcnew System::Windows::Forms::PrintDialog());
this->document = (gcnew System::Drawing::Printing::PrintDocument());
this->Button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// PrintDialog1
//
this->PrintDialog1->AllowCurrentPage = true;
this->PrintDialog1->AllowSomePages = true;
this->PrintDialog1->Document = this->document;
this->PrintDialog1->Tag = L"";
this->PrintDialog1->UseEXDialog = true;
//
// document
//
this->document->PrintPage += gcnew System::Drawing::Printing::PrintPageEventHandler(this, &Form1::document_PrintPage_1);
//
// Button1
//
this->Button1->Location = System::Drawing::Point(107, 79);
this->Button1->Name = L"Button1";
this->Button1->Size = System::Drawing::Size(75, 23);
this->Button1->TabIndex = 0;
this->Button1->Text = L"button1";
this->Button1->UseVisualStyleBackColor = true;
this->Button1->Click += gcnew System::EventHandler(this, &Form1::Button1_Click_1);
//
// button2
//
this->button2->Location = System::Drawing::Point(107, 129);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 1;
this->button2->Text = L"pritn direkt";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this->button2);
this->Controls->Add(this->Button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
 
}
#pragma endregion
// Declare the PrintDocument object.
 
private: System::Void Button1_Click_1(System::Object^ sender, System::EventArgs^ e) {
PrintDialog1->AllowSomePages = true;
 
PrintDialog1->ShowHelp = true;
 
PrintDocument ^docToPrint = gcnew PrintDocument;
PrintDialog1->Document = docToPrint;
if ( docToPrint == nullptr )
System::Windows::F

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 200
1 PIEBALDconsult 150
2 BillWoodruff 148
3 Jochen Arndt 135
4 DamithSL 130
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100