By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,448 Members | 1,484 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,448 IT Pros & Developers. It's quick & easy.

header and footer

P: n/a
Hello!

I have a class called Simple Editor below where most of it is from a book
that I'm reading.

If I want to add a Header and a Footer to each page printed.
We can just as an example want to have the filename printed on beginning and
the end of each page printed.
can somebody give me a hint how I best accomplish that?

The text that is to be printed is in a multiline textbox and is named
textBoxEdit.

public partial class SimpleEditorForm : Form
{
private string filename = "Untitled";
private string[] lines;
private int linesPrinted;
private Brush printBrush;

public SimpleEditorForm() //User defined C-tor
{ InitializeComponent(); }

protected void OpenFile()
{
try
{
textBoxEdit.Clear();
textBoxEdit.Text = File.ReadAllText(filename);
}
catch(IOException ex)
{
MessageBox.Show(ex.Message, "Simple Editor",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}

private void OnFileNew(object sender, EventArgs e)
{
filename = "Untitled";
SetFormTitle();
textBoxEdit.Clear(); // clear textbox
}

private void OnFileOpen(object sender, EventArgs e)
{
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
filename = dlgOpenFile.FileName;
SetFormTitle();
OpenFile();
}
}

private void OnFileSave(object sender, EventArgs e)
{
if (filename == "Untitled")
OnFileSaveAs(sender, e);
else
SaveFile();

}

private void OnFileSaveAs(object sender, EventArgs e)
{
if (dlgSaveFile.ShowDialog() == DialogResult.OK)
{
filename = dlgSaveFile.FileName;
SetFormTitle();
SaveFile();
}
}

private void SaveFile()
{
try
{
File.WriteAllText(filename, this.textBoxEdit.Text);
}
catch(IOException ex)
{
MessageBox.Show(ex.Message, "Simple Editor",
MessageBoxButtons.AbortRetryIgnore,MessageBoxIcon. Hand);
}
}

protected void SetFormTitle()
{
Text = new FileInfo(filename).Name + "- Simple Editor";
}

private void OnFilePrint(object sender, EventArgs e)
{
if (this.textBoxEdit.SelectedText != "")
dlgPrint.AllowSelection = true;

if (dlgPrint.ShowDialog() == DialogResult.OK)
printDocument.Print();
}

private void OnFilePrintPreview(object sender, EventArgs e)
{ dlgPrintPreview.ShowDialog(); }

private void OnFilePageSetup(object sender, EventArgs e)
{ dlgPageSetup.ShowDialog(); }

private void OnExit(object sender, EventArgs e)
{ Application.Exit(); }

private void OnPrintPage(object sender, PrintPageEventArgs e)
{
int x = e.MarginBounds.Left;
int y = e.MarginBounds.Top;

while (linesPrinted < lines.Length) //antal rader att printa
{
e.Graphics.DrawString(lines[linesPrinted++],
this.fontDialog.Font, printBrush, x, y);
y += textBoxEdit.Font.Height;

if (y >= e.MarginBounds.Bottom)
{
e.HasMorePages = true;
return;
}
}
}

private void OnBeginPrint(object sender, PrintEventArgs e)
{
char[] param = { '\n' };

if (dlgPrint.PrinterSettings.PrintRange == PrintRange.Selection)
lines = textBoxEdit.SelectedText.Split(param);
else
lines = textBoxEdit.Text.Split(param);
//int i = 0;
//char[] trimParam = { '\r' };

//foreach (string s in lines)
// lines[i++] = s.TrimEnd(trimParam);

if (this.dlgPrint.PrinterSettings.SupportsColor)
printBrush = new SolidBrush(textBoxEdit.ForeColor);
else
printBrush = Brushes.Black;
}

private void OnEndPrint(object sender, PrintEventArgs e)
{ lines = null; }
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
if (fontDialog.ShowDialog() == DialogResult.OK)
textBoxEdit.Font = fontDialog.Font;
}

private void colorToolStripMenuItem_Click(object sender, EventArgs
e)
{
if (colorDialog.ShowDialog() == DialogResult.OK)
textBoxEdit.ForeColor = colorDialog.Color;
}
}

//Tony
Jul 12 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sat, 12 Jul 2008 07:12:42 -0700, Tony Johansson
<jo*****************@telia.comwrote:
Hello!

I have a class called Simple Editor below where most of it is from a book
that I'm reading.

If I want to add a Header and a Footer to each page printed.
We can just as an example want to have the filename printed on beginning
and
the end of each page printed.
can somebody give me a hint how I best accomplish that?
Just calculate from the page boundary where you want the header and
footer, and then draw the text you want there.

Pete
Jul 12 '08 #2

P: n/a
Hello!

So you mean that the best way is just to use another DrawString to draw for
example the filename.

//Tony
"Peter Duniho" <Np*********@nnowslpianmk.comskrev i meddelandet
news:op***************@petes-computer.local...
On Sat, 12 Jul 2008 07:12:42 -0700, Tony Johansson
<jo*****************@telia.comwrote:
>Hello!

I have a class called Simple Editor below where most of it is from a book
that I'm reading.

If I want to add a Header and a Footer to each page printed.
We can just as an example want to have the filename printed on beginning
and
the end of each page printed.
can somebody give me a hint how I best accomplish that?

Just calculate from the page boundary where you want the header and
footer, and then draw the text you want there.

Pete

Jul 12 '08 #3

P: n/a
On Sat, 12 Jul 2008 10:11:47 -0700, Tony Johansson
<jo*****************@telia.comwrote:
So you mean that the best way is just to use another DrawString to draw
for
example the filename.
Yes, that's exactly what I mean.
Jul 12 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.