Print of values in MsgBox + WinForms

Sometimes you want to take print out of the message which occurs in MessageBox.
For example, When a user fills up a form and when he clicks Ok he gets print out of the values entered and his register ID.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PrintMessageBoxFunctionality
{
public partial class Form1 : Form
{
private StringReader myReader=null;
public Form1()
{
InitializeComponent();
cmbCity.SelectedIndex = 0;
}

private void btnSave_Click(object sender, EventArgs e)
{
////FirstNameValidation();
////CityValidation();
try
{
StringBuilder formData = new StringBuilder();
formData.Append("Firstname -" + txtFirstname.Text.Trim() + "\n");
formData.Append("Lastname -" + txtLastName.Text.Trim() + "\n");
formData.Append("City -" + cmbCity.SelectedItem);
//cmbcity.SelectedValue

if (MessageBox.Show(formData.ToString(), "Are You Sure want to Save?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
//ok pressed
myReader = new StringReader(formData.ToString());
PrintDialog objPd = new PrintDialog();
objPd.Document = printDocumentMessageBoxTest;
if (objPd.ShowDialog() == DialogResult.OK)
{
this.printDocumentMessageBoxTest.Print();
myReader.Dispose();
}
}
else
{
//cancel pressed
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(),"Print Error:");

}
finally
{
myReader.Dispose();
}

}

private void printDocumentMessageBoxTest_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPosition = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
Font printFont = txtFirstname.Font;
SolidBrush myBrush = new SolidBrush(Color.Black);
// Work out the number of lines per page, using the MarginBounds.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
// Iterate over the string using the StringReader, printing each line.
while (count < linesPerPage && ((line = myReader.ReadLine()) != null))
{
// calculate the next line position based on the height of the font according to the printing device
yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics));
// draw the next line in the rich edit control
ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
count++;
}
// If there are more lines, print another page.
if (line != null)
{
ev.HasMorePages = true;
}
else
{
ev.HasMorePages = false;
}
myBrush.Dispose();
}
}
}


Comments

Author: Sachin09 Jun 2009 Member Level: Gold   Points : 0

Thanks for this nice article..
It helped me a lot
Thanks

Author: Deepika Haridas13 Jun 2009 Member Level: Gold   Points : 0

Hi,


Thank you for all your valuable responses..

Regards,
Deepika



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: