header and footer

T

Tony Johansson

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
 
T

Tony Johansson

Hello!

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

//Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top