Autor Beitrag
JoKaBo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 157



BeitragVerfasst: Mo 11.07.11 16:15 
Hallo,

Ich Möchte den inhalt einer RichTextBox Drucken. Habe auch schon einen code aber es wird nichts gedruckt weil, das printDocument1 gefüllt werden muss. Habe auch schon in der MSDN gesucht aber nichts gefunden weiss jemand wie man ein prindDocument füllt?

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
 PrintDialog pdlg = new PrintDialog();
            pdlg.Document = printDocument1;




            if (pdlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    printDocument1.Print();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Druken Fehlgeschlagen " + ex.Message);

                }
            }
        }
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4795
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mo 11.07.11 17:21 
Hallo JoKaBo,

leider ist das Drucken einer RichTextBox im .NET-Framework nicht standardmäßig vorhanden.
Dein Ansatz mit PrintDocument ist schon mal richtig, du mußt dann nur noch das PrintDocument.OnPrintPage-Ereignis behandeln, s. z.B.
www.codeproject.com/...impleprintingcs.aspx
(Nachteil: die Formatierung muß man komplett selber implementieren bzw. ist so einfach gar nicht möglich.)

Und der User 'juetho' (der auch in diesem Forum aktiv war/ist) hat eine allgemeine Druckkomponente entwickelt: www.mycsharp.de/wbb2...d.php?threadid=29704

Ein anderer Ansatz basiert auf Funktionen der WinAPI (wie es auch die MFC macht):
msdn.microsoft.com/e...ibrary/ms996492.aspx
JoKaBo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 157



BeitragVerfasst: Mo 11.07.11 18:58 
Okey ich schau mahl ...
Kann man eine richTexBox vil. Uber ein anderes programm drucken ?
Z.B word oder exel also wenn ich auf drucken drüke soll es über
Word,exel oder ... gedruckt werden
JoKaBo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 157



BeitragVerfasst: Mi 13.07.11 11:39 
hallo,
ich habe es soweit hin beckommen nur noch ein Fehler
welches control gehört zu:
lines

oder ist das kein control?? ich habe auf der seite nicht gefunden(welches control ,.. das ist)
hier mahl der ganze code:
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
 private void printToolStripMenuItem_Click(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 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 (lines < lines.Length)
            {
                e.Graphics.DrawString(lines[lines++],
                     richTextBox1.Font, brush, x, y);
                y += 15;

                if (y >= e.MarginBounds.Bottom)
                {
                    e.HasMorePages = true;
                    return;
                }
                else
                {
                    MessageBox.Show("");
                }
            }
        }
    }
}
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4795
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mi 13.07.11 12:48 
Hallo,

'lines' wird in dem Code in der Methode OnBeginPrint initialisiert und ist vom Typ 'string[]'.
Kopiere also auch noch diese Methode und abonniere diese dann für 'printDocument1.OnBeginPrint'.
Und dann füge noch die Membervariable
ausblenden C#-Quelltext
1:
private string[] lines;					

zu deiner Form hinzu.

P.S: Der Brush sollte auch noch disposed werden (ist also auch falsch im Originalcode):
ausblenden C#-Quelltext
1:
2:
3:
4:
using(Brush brush = new SolidBrush(richTextBox1.ForeColor))
{
  ...
}

bzw. auch im OnBeginPrint initialisieren und im OnEndPrint (die du noch hinzufügen müßtest) wieder disposen.

Und um das über ein externes Programm zu drucken, müßtest du den RTB-Text als Datei abspeichern und den Dateinamen (+Pfad) mittels der WinAPI-Funktion ShellExecute mit dem Parameter "print" ausführen. Von C# aus geht das nur mittels P/Invoke: pinvoke.net/default....32/ShellExecute.html
Anhand der Dateiendung (z.B. ".rtf") wird dann das dafür registrierte Programm (z.B. WordPad oder Word) geöffnet und der entsprechende Befehl abgeschickt.
JoKaBo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 157



BeitragVerfasst: Mi 13.07.11 13:14 
Hallo,

Ich Galube ich bin zu blöd dafür. habe jetzt alles so gemacht wie es auf dr seite stand trotzdem ist das falschder ganze code

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
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 Form1 : Form
    {
        private string[] lines;
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, System.EventArgs e)
        {

        }

        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);
            //hir ist alles falsch (ind der zeile 
            while (lines < lines.Length)
            {
                e.Graphics.DrawString(linesi[lines++],
                     richTextBox1.Font, brush, x, y);
                y += 15;

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

                else
                {
                    MessageBox.Show("");
                }
            }
        }

        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);
            }
        }

        private void buttonDrucken_Click(object sender, EventArgs e)
        {
            PrintDialog pdlg = new PrintDialog();
            pdlg.Document = printDocument1;
            //hir kan nich darauf zu gegriffen werden (irgendwas mit der sicherheitsebene  
            printDocument1.OnBeginPrint();



            if (pdlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    printDocument1.Print();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Druken Fehlgeschlagen " + ex.Message);

                }
            }
        }
    }
}
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4795
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mi 13.07.11 13:36 
Hallo,

auf deinen ersten Satz gebe ich jetzt mal keine Antwort ;-)

Hast du denn mal den gesamten Sourcecode von der Seite heruntergeladen?
Denn dort im Artikel steht ja
Zitat:

Below is some of the sample codes.


Fürs erste reicht es, wenn du die Zeilen
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
if (printDialog1.PrinterSettings.PrintRange == PrintRange.Selection)
{
    lines = richTextBox1.SelectedText.Split(param);
}
else

bei dir mal auskommentierst (Drucken eines Bereichs ist ja erstmal nebensächlich ;-))
JoKaBo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 157



BeitragVerfasst: Mi 13.07.11 13:57 
jetzt habe ich mahl den ganzen code ;)
2 fehler weren da noch. habe jetzt mahl das ganze projeckt hoch geladen.
fehler:
ausblenden C#-Quelltext
1:
  while (linesPrinted < lines.Length)					

ausblenden C#-Quelltext
1:
 e.Graphics.DrawString(lines[linesPrinted++],					


ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
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 Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }



        private string[] lines;
        private string[] linesPrinted;


        private void button1_Click(object sender, EventArgs e)
        {
        }
        private void Form1_Load(object sender, System.EventArgs e)
        {
            // Write to richTe xtBox

            richTextBox1.Text = "                               " +
               DateTime.Now.Month + "/" + DateTime.Now.Day + "/" +
            DateTime.Now.Year + "\r\n\r\n";
            richTextBox1.AppendText("This is a greatly simplified Print " +
            "Document Method\r\n\r\n");
            richTextBox1.AppendText("We can write text to a richTextBox, " +
            "or use Append Text, " + "\r\n" + "or Concatenate a String, and " +
            "write that textBox. The " + "\r\n" + "richTextBox does not " +
            "even have to be visible. " + "\r\n\r\n" + "Because we use a " +
            "richTextBox it's physical dimensions are " + "\r\n" +
            "irrelevant. We can place it anywhere on our form, and set the " +
            "\r\n" + "Visible Property to false.\r\n\r\n");
            richTextBox1.AppendText("This is the document we will print. The " +
            "rich TextBox serves " + "\r\n" + "as a Cache for our Report, " +
            "or any other text we wish to print.\r\n\r\n");
            richTextBox1.AppendText("I have also included Print Setup " +
            "and Print Preview. ");
        }
        // Print Event

        private void miPrint_Click(object sender, System.EventArgs e)
        {
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                printDocument1.Print();
            }
        }

        // OnBeginPrint 

        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 = true;
                    return;
                }
                else
                {
                    e.HasMorePages = false;
                }
            }
        }
    }
}
Einloggen, um Attachments anzusehen!
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4795
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mi 13.07.11 14:27 
Hallo,

die Fehler solltest du aber selber lösen können...
Tip: der Datentyp paßt nicht

Für diesen Beitrag haben gedankt: JoKaBo
JoKaBo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 157



BeitragVerfasst: Mi 13.07.11 15:04 
sry kan ich leider net

vil. könt ihr es mir sagen
JoKaBo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 157



BeitragVerfasst: Fr 15.07.11 09:22 
Hi,

Habe etwas gegooglet aber weiss net was ich damit machen soll

Int32.parse(String)
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4795
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 15.07.11 10:52 
Hallo JokaBo,

um dieses Thema jetzt abzuschließen:
ausblenden C#-Quelltext
1:
private int linesPrinted; // Anzahl der aktuell gedruckten Zeilen					

Und in OnBeginPrint solltest du diese Variable dann immer mit 0 initialisieren (damit auch das mehrfache Drucken klappt).

Für diesen Beitrag haben gedankt: JoKaBo
JoKaBo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 157



BeitragVerfasst: Fr 22.07.11 14:54 
Hallo,

Ich habe es immer noch nicht hin bekommen :( hir noch mahl mein code
könntest du mahl bitte sage was jetzt falsch ist

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
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;


        private void button1_Click(object sender, EventArgs e)
        {

            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                printDocument1.Print();
            }
        }
        private void Form1_Load(object sender, System.EventArgs e)
        {
            // Write to richTe xtBox

            richTextBox1.Text = "                               " +
               DateTime.Now.Month + "/" + DateTime.Now.Day + "/" +
            DateTime.Now.Year + "\r\n\r\n";
            richTextBox1.AppendText("This is a greatly simplified Print " +
            "Document Method\r\n\r\n");
            richTextBox1.AppendText("We can write text to a richTextBox, " +
            "or use Append Text, " + "\r\n" + "or Concatenate a String, and " +
            "write that textBox. The " + "\r\n" + "richTextBox does not " +
            "even have to be visible. " + "\r\n\r\n" + "Because we use a " +
            "richTextBox it's physical dimensions are " + "\r\n" +
            "irrelevant. We can place it anywhere on our form, and set the " +
            "\r\n" + "Visible Property to false.\r\n\r\n");
            richTextBox1.AppendText("This is the document we will print. The " +
            "rich TextBox serves " + "\r\n" + "as a Cache for our Report, " +
            "or any other text we wish to print.\r\n\r\n");
            richTextBox1.AppendText("I have also included Print Setup " +
            "and Print Preview. ");
        }
        

        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 = true;
                    return;
                }
                else
                {
                    e.HasMorePages = false;
                }
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {

        }
    }
}
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4795
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 22.07.11 15:35 
Erstens schreibst du 'mal' falsch und zweitens kannst du anscheinend noch nicht einmal meine eine Code-Zeile richtig kopieren (da steht nichts von einem Array, sondern 'linesPrinted' ist einfach eine ganzzahlige Zählervariable)!
Und drittens solltest du bei jedem Beitrag dazuschreiben, was genau nicht funktioniert (Fehlermeldung etc.) - wobei ich in diesem Fall deinen Fehler auch so auf Anhieb gesehen habe...
JoKaBo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 157



BeitragVerfasst: Fr 22.07.11 17:28 
Hallo
vielen vielen Dank für die hilfe Th69
habs hin bekommen

Moderiert von user profile iconTh69: Status auf "Frage beantwortet" gesetzt - puh ;-)
JoKaBo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 157



BeitragVerfasst: Sa 23.07.11 11:06 
Hallo,

sry habe noch ne fragen

wie mache ich es das ich es mit dem printDialog mache also das ich das printdockument über das printDialog Drucke habe ja den Code
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
            PrintDialog pdlg = new PrintDialog();
            pdlg.Document = printDocument1;

            if (pdlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    printDocument1.Print();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Druken Fehlgeschlagen " + ex.Message);

                }
            }
        }


aber bei dem code kommt immer die Fehler Meldung:
Drucken fehlgeschlagen das Objekt muss den Type "Int32" haben
...
ok dan habe ich das gemacht

ausblenden C#-Quelltext
1:
private Int32 linesPrintet;					

Kein fehler aber trotzdem druckt er nicht

auch wen ich einfach nur
printDockumen1.Print
habe druckt er nicht mahl ein leeres Blatt und gibt ja nicht mahl den Befel Drucken an den drucker
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4795
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Sa 23.07.11 11:28 
Hallo,

die Fehlermeldung kommt, weil du doch selber diese Fehlermeldung anzeigst (im catch-Teil).
Schau dir einfach mal im Debugger den StackTrace an bzw. debugge deine Print-Methoden von Hand (F10/F11). Es liegt auf jeden Fall nicht an der Änderung von 'int' nach 'Int32' (denn dies sind intern identische Datentypen - 'int' ist einfach nur ein Aliasname für 'Int32').

Du hast höchstwahrscheinlich noch einige Änderungen bzgl. deines zuletzt gezeigten Codes (OnBeginPrint, OnPrintPage) gemacht, die den Fehler verursachen...
JoKaBo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 157



BeitragVerfasst: Sa 23.07.11 11:37 
Fehler eig. net habe den Code nochmahl neu eingefügt damit so etwas nicht entsteht hir noch mahl der Jetzige Code: was ist daran Falsch ??
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
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)
            {a
                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();
        }
    }
}