Hallo,
ich habe ein programm und möcht da die richTextBox Drucken Was ist an diesem code falsch??
hier der code:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace Drucken
{
public partial class Drucken : Form
{
public Drucken()
{
InitializeComponent();
}
private string[] lines;
private int linesPrinted;
string filename = "";
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void OnBeginPrint(object sender,
System.Drawing.Printing.PrintEventArgs e)
{
char[] param = { '\n' };
if (printDialog1.PrinterSettings.PrintRange == PrintRange.Selection)
{
lines = richTextBox1.SelectedText.Split(param);
}
else
{
lines = richTextBox1.Text.Split(param);
}
int i = 0;
char[] trimParam = { '\r' };
foreach (string s in lines)
{
lines[i++] = s.TrimEnd(trimParam);
}
}
// OnPrintPage
private void OnPrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
int x = e.MarginBounds.Left;
int y = e.MarginBounds.Top;
Brush brush = new SolidBrush(richTextBox1.ForeColor);
while (linesPrinted < lines.Length)
{
e.Graphics.DrawString(lines[linesPrinted++],
richTextBox1.Font, brush, x, y);
y += 15;
if (y >= e.MarginBounds.Bottom)
{
e.HasMorePages = false;
return;
}
else
{
e.HasMorePages = true;
}
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
PrintDialog pdlg = new PrintDialog();
pdlg.Document = printDocument1;
if (pdlg.ShowDialog() == DialogResult.OK)
{
try
{
printDocument1.Print();
}
catch (Exception ex)
{
MessageBox.Show("Druken Fehlgeschlagen " + ex.Message);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openFile1 = new OpenFileDialog();
openFile1.Filter = "To Write 2010(*.twt)|*.twt| Textdokument (*.txt)|*.txt| Word Dokument (*.doc)|*.doc";
if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK && openFile1.FileName.Length > 0)
{
richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.UnicodePlainText);
richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.RichText);
this.filename = openFile1.FileName;
}
}
private void button3_Click(object sender, EventArgs e)
{
Close();
}
}
}