自已写的帮儿子出口算题的程序
儿子开学要上二年级了,每天数学做业的都要家长出一些口算题.今天写了一个程序可以出题并打印.自已对.net打印这部分不是很熟.现在只能打印单页.请哪位高手帮我改一下能打印多页.
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 WindowsApplication1
{
public partial class Form1 : Form
{
private Font printFont;
private Random x;
public Form1()
{
InitializeComponent();
}
public string getString()
{
x = new Random();
int a, b,c,d,e,f;
string g = string.Empty;
string s = string.Empty;
for (int i = 0; i < 30; i++)
{
a = x.Next(5, 100);
b = x.Next(5, 100);
c = x.Next(5, 100);
d = x.Next(5, 100);
e = x.Next(5, 100);
f = x.Next(5, 100);
if (a > b)
{
g = a.ToString().PadRight(2) + " - " + b.ToString().PadRight(2) + " = " + "\t";
}
else
{
g = a.ToString().PadRight(2) + " + " + b.ToString().PadRight(2) + " = " + "\t";
}
if (c > d)
{
g += c.ToString().PadRight(2) + " - " + d.ToString().PadRight(2) + " = " + "\t";
}
else
{
g += c.ToString().PadRight(2) + " + " + d.ToString().PadRight(2) + " = " + "\t";
}
if (e > f)
{
g += e.ToString().PadRight(2) + " - " + f.ToString().PadRight(2) + " = " + "\t";
}
else
{
g += e.ToString().PadRight(2) + " + " + f.ToString().PadRight(2) + " = " + "\t";
}
s += g + "\r\n";
}
return s;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = null;
textBox1.Text = getString();
}

private void button2_Click(object sender, EventArgs e)
{
printFont = new Font("宋体", 20,FontStyle.Bold);
printDocument1 = new System.Drawing.Printing.PrintDocument();
printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
}

void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
int count = 0;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics);

// Print each line of the file.
char[] c = { '\n' };
string[] a = textBox1.Text.Split(c);
foreach(string s in a)
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(s + "\r\n", printFont, Brushes.Black,
leftMargin, yPos);
count++;
}
ev.HasMorePages = false;
}
}
}
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 WindowsApplication1
{
public partial class Form1 : Form
{
private Font printFont;
private Random x;
public Form1()
{
InitializeComponent();
}
public string getString()
{
x = new Random();
int a, b,c,d,e,f;
string g = string.Empty;
string s = string.Empty;
for (int i = 0; i < 30; i++)
{
a = x.Next(5, 100);
b = x.Next(5, 100);
c = x.Next(5, 100);
d = x.Next(5, 100);
e = x.Next(5, 100);
f = x.Next(5, 100);
if (a > b)
{
g = a.ToString().PadRight(2) + " - " + b.ToString().PadRight(2) + " = " + "\t";
}
else
{
g = a.ToString().PadRight(2) + " + " + b.ToString().PadRight(2) + " = " + "\t";
}
if (c > d)
{
g += c.ToString().PadRight(2) + " - " + d.ToString().PadRight(2) + " = " + "\t";
}
else
{
g += c.ToString().PadRight(2) + " + " + d.ToString().PadRight(2) + " = " + "\t";
}
if (e > f)
{
g += e.ToString().PadRight(2) + " - " + f.ToString().PadRight(2) + " = " + "\t";
}
else
{
g += e.ToString().PadRight(2) + " + " + f.ToString().PadRight(2) + " = " + "\t";
}
s += g + "\r\n";
}
return s;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = null;
textBox1.Text = getString();
}
private void button2_Click(object sender, EventArgs e)
{
printFont = new Font("宋体", 20,FontStyle.Bold);
printDocument1 = new System.Drawing.Printing.PrintDocument();
printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
}
void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
int count = 0;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics);
// Print each line of the file.
char[] c = { '\n' };
string[] a = textBox1.Text.Split(c);
foreach(string s in a)
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(s + "\r\n", printFont, Brushes.Black,
leftMargin, yPos);
count++;
}
ev.HasMorePages = false;
}
}
}


浙公网安备 33010602011771号