473,378 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

header and footer

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
3 1790
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shiz | last post by:
I currently have a Form with Option buttons (to give you the option of sorting by name, or by Field, etc), these are referenced in a module which then formats the report contingent on the option...
3
by: John Crowley | last post by:
I keep running into this over and over again... I want a block server control that renders a header and footer, and child controls in between. But I don't want a templated control, for the...
2
by: LilBuh | last post by:
hi there :) i ve been looking for some time a way to print an html file from vb.net i came up with this code for printing and removing the header and footer from IE then once the printing is done...
0
by: LilBuh | last post by:
Hello :) this is a repost... i ve been looking for a way since last week ... i have a template html file that i modify from my code... i need to print this file without IE's Header and Footer or...
7
by: Neil Jarman | last post by:
Hi, I would like to allow users to print a page, but I want to suppress the header and footer that appear by default. I gather its done with a style sheet. Please advise. Many Thanks,
7
by: xkeops | last post by:
Thinking of creating a website, most of the pages will have a general toolbar menu, a content and a footer. The content will be the only one who's gonna change but the rest (header,footer) will...
11
by: Grischa Brockhaus | last post by:
Hi, I'm trying to produce a div layout containing a header on the top with fixed height, a footer on the bottom using fixed height and a content layer using what's left of the browsers window. ...
0
by: srk | last post by:
hi to all, i need some help , my problem is that i have to generate a word document withe the data from data base and it should be displayed as tables in word and i have to insert some header and...
1
by: KoosHopeloos | last post by:
L.S., I'm trying to make a layout which is completely fixed in width and height if needed by using three divs rows (header, content, footer) which have each 3 div again to be able to play around...
3
by: Noorain | last post by:
I designed a site. i want to header,footer,left & right column fixed but body information only scrolling. this site screen to be 800/600 px. i designed this way but when i used position fixed all...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.