PrintDialog settings donot work properly
I am an experienced programmer new to visual programming. I am working on a Windows Form application using Visual C++ 2005. I am having a problem with PrintDialog. Whenever I run my application and print using the print dialog box, the settings I select in the print dialog box donot take effect for some reason. The entire multiple page document prints fine but if I try to print selected text or print the current page or a range of pages from the dialog box, these settings donot take effect. My code follows. Can someone tell me what I am doing wrong?
private: System::Windows::Forms::PrintDialog^ printDialog1;
private: System::Drawing::Printing::PrintDocument^ printDocument1;
private: System::Windows::Forms::DialogResult result1;
private: System::Drawing::Font^ printFont;
private: String^ printString;
private: StringReader^ myReader;
this->printDialog1->AllowCurrentPage = true;
this->printDialog1->AllowSelection = true;
this->printDialog1->AllowSomePages = true;
this->printDialog1->Document = this->printDocument1;
this->printDialog1->ShowHelp = true;
this->printDialog1->UseEXDialog = true;
this->printDocument1->PrintPage += gcnew System::Drawing::Printing::PrintPageEventHandler(this, &Form1::printDocument1_PrintPage);
private: System::Void printToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
{
printString = richTextBox1->Text;
myReader = gcnew StringReader(printString);
result1 = printDialog1->ShowDialog();
if (result1 == System::Windows::Forms::DialogResult::OK)
{
printDocument1->PrinterSettings = printDialog1->PrinterSettings;
printDocument1->Print();
}
myReader->Close();
}//printToolStripMenuItem_Click ends
private: System::Void printDocument1_PrintPage(System::Object^ sender, System::Drawing::Printing::PrintPageEventArgs^ e)
{
float linesPerPage = 0;
float yPosition = 0;
int count = 0;
int leftMargin = e->MarginBounds.Left;
int topMargin = e->MarginBounds.Top;
int nextChar = 0;
String^ line = "";
printFont = richTextBox1->Font;
SolidBrush^ myBrush = gcnew SolidBrush(Color::Black);
// Work out the number of lines per page, using the MarginBounds.
linesPerPage = e->MarginBounds.Height / printFont->GetHeight(e->Graphics);
// Iterate over the string using the StringReader, printing each line.
while((count < linesPerPage) && (nextChar >= 0))
{
line=myReader->ReadLine();
// calculate the next line position based on the height of the font according to the printing device
yPosition = topMargin + (count * printFont->GetHeight(e->Graphics));
// draw the next line in the rich edit control
e->Graphics->DrawString(line, printFont, myBrush, leftMargin, yPosition, gcnew StringFormat());
count++;
}
// If there are more lines, print another page.
nextChar = myReader->Peek();
if (String::IsNullOrEmpty(line) && (nextChar < 0))
e->HasMorePages = false;
else
e->HasMorePages = true;
}//printDocument1_PrintPage ends

