Autor Beitrag
tomycat
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Do 25.02.16 21:35 
hallo,
Ziel der Geschichte ist, dass ein String "blablalba" auf Papier gedruckt werden soll.
das ist meine Grundlage:
msdn.microsoft.com/d...4%28v=vs.110%29.aspx

Neues Projekt erstellt, ein Button eingebaut und dann den Beispiel Code reingesetzt.Nur der Button heist anderst.
Der Namespace heist jetzt :
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:
using System;
using System.Drawing;
using System.IO;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        private Button printPreviewButton;

        private PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
        private PrintDocument printDocument1 = new PrintDocument();

        // Declare a string to hold the entire document contents.
        private string documentContents;

        // Declare a variable to hold the portion of the document that
        // is not printed.
        private string stringToPrint;

        public Form1()
        {
            this.printPreviewButton = new System.Windows.Forms.Button();
            this.printPreviewButton.Location = new System.Drawing.Point(1212);
            this.printPreviewButton.Size = new System.Drawing.Size(12523);
            this.printPreviewButton.Text = "Print Preview";
            this.printPreviewButton.Click += new System.EventHandler(this.button1_Click);
            this.ClientSize = new System.Drawing.Size(292266);
            this.Controls.Add(this.printPreviewButton);
            printDocument1.PrintPage +=
                new PrintPageEventHandler(printDocument1_PrintPage);
        }
        private void ReadDocument()
        {
            string docName = "testPage.txt";
            string docPath = @"c:\";
            printDocument1.DocumentName = docName;
            using (FileStream stream = new FileStream(docPath + docName, FileMode.Open))
            using (StreamReader reader = new StreamReader(stream))
            {
                documentContents = reader.ReadToEnd();
            }
            stringToPrint = documentContents;
        }

        void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            int charactersOnPage = 0;
            int linesPerPage = 0;

            // Sets the value of charactersOnPage to the number of characters 
            // of stringToPrint that will fit within the bounds of the page.
            e.Graphics.MeasureString(stringToPrint, this.Font,
                e.MarginBounds.Size, StringFormat.GenericTypographic,
                out charactersOnPage, out linesPerPage);

            // Draws the string within the bounds of the page.
            e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
            e.MarginBounds, StringFormat.GenericTypographic);

            // Remove the portion of the string that has been printed.
            stringToPrint = stringToPrint.Substring(charactersOnPage);

            // Check to see if more pages are to be printed.
            e.HasMorePages = (stringToPrint.Length > 0);

            // If there are no more pages, reset the string to be printed.
            if (!e.HasMorePages)
                stringToPrint = documentContents;
        }
        private void button1_Click(object sender, EventArgs e)
        //private void button1_Click(object sender, EventArgs e)
        {
            ReadDocument();
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();

        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}


F5 gedrückt und....

Fehler CS0115 "Form1.Dispose(bool)": Es wurde keine passende Methode zum Überschreiben gefunden. WindowsFormsApplication3 C:\Users\tomycat\Documents\Visual Studio 2015\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Form1.Designer.cs 14

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:
namespace PrintPreviewApp
{
    partial class Form1
    {
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Verwendete Ressourcen bereinigen.
        /// </summary>
        /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
        protected override void Dispose(bool disposing) // <------------------------------------------------------------- Dispose ist ROT
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Vom Windows Form-Designer generierter Code

        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(126116);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(7523);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284262);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
    }
}


Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt
Moderiert von user profile iconChristian S.: Topic aus Multimedia / Grafik verschoben am Do 25.02.2016 um 20:54
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 25.02.16 21:52 
Du nutzt in den Dateien unterschiedliche Namespaces, sodass die Deklaration der Form1-Klasse nicht mehr auf zwei Dateien aufgeteilt hast, sondern Du hast zwei Form1-Klassen deklariert. Und die in der Designer-Datei hat keinen Vorfahren und somit auch keine überschreibbare Methode mit dem Namen.

Wenn Du die Namespaces wieder angleichst, sollte es gehen.


P.S.: Nach 46 Beiträgen solltest Du das mit den C#-Tags aber langsam gelernt haben ;-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: So 28.02.16 18:10 
ok thx,
hmmmm ich habe neu angesetzt und habe was gefunden, das perfekt in mein Projekt passt.
www.youtube.com/watch?v=_C8OzJVqHb4

Dann habe ich mir die Mühe gemacht alles 1 zu 1 abtippen, ich will ja auch was lernen :-)
Dazu habe ich noch listView1, printdocument1 und printPreviewDialog1 per drag and drop reingezogen.

Button 1: listView füllen
Button 2: Druckervorschau geht nicht (Inhalt ist leer), ist aber nicht schlimm
Button 3: druckt sofort, aber ich KANN KEIN DRUCKER AUSWÄHLEN !!!

Was muss ich ändern, dass ich den Drucker auswählen kann?
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:
        private void button1_Click(object sender, EventArgs e)
        {
            ListViewItem leo = new ListViewItem(textBox1.Text);
            leo.SubItems.Add(textBox2.Text);
            leo.SubItems.Add(textBox3.Text);
            leo.SubItems.Add(textBox4.Text);
            listView1.Items.Add(leo);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            printPreviewDialog1.Show(); // Druckervorschau
        }

        private void button3_Click(object sender, EventArgs e)
        {
            printDocument1.Print();    // druckt sofort !!!!!!!!!!!!!!!!!!!!!!
  





        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Pen p = new Pen(Brushes.Black, 2.5f);

            int Höhe = 0;

            #region
            Rectangle ff = new Rectangle(752530030);
            Font d = new Font("Microsoft San Serif",20);
            e.Graphics.DrawString("Ihre Kundenliste", d, Brushes.Black, ff);

            #endregion

            for (int x = 0; x < listView1.Items.Count; x++)
            {
                #region
                Rectangle rt = new Rectangle();
                rt = listView1.Items[0].SubItems[1].Bounds; // übernimmt Rechteck der ersten Zeile, Spalte 0
                rt.X = 100;
                rt.Y = 100 + Höhe;
                e.Graphics.FillRectangle(Brushes.DarkGray, rt); // füllt Hintergrund
                e.Graphics.DrawRectangle(p, rt);
                e.Graphics.DrawString(listView1.Items[x].SubItems[0].Text.ToString(), listView1.Font, Brushes.Black, rt);
                #endregion

                #region
                Rectangle sb = new Rectangle();
                sb = listView1.Items[0].SubItems[1].Bounds; // übernimmt Rechteck der ersten Zeile, Spalte 0
                sb.X = rt.X + rt.Width;
                sb.Y = 100 + Höhe;
                e.Graphics.FillRectangle(Brushes.DarkGray, sb); // füllt Hintergrund
                e.Graphics.DrawRectangle(p, sb); //zeichnet Ramenslinie
                e.Graphics.DrawString(listView1.Items[x].SubItems[1].Text.ToString(), listView1.Font, Brushes.Black, sb);
                #endregion

                #region
                Rectangle df = new Rectangle();
                df = listView1.Items[0].SubItems[2].Bounds; // übernimmt Rechteck der ersten Zeile, Spalte 0
                df.X = sb.X + sb.Width;
                df.Y = 100 + Höhe;
                e.Graphics.FillRectangle(Brushes.DarkGray, df); // füllt Hintergrund
                e.Graphics.DrawRectangle(p, df); //zeichnet Ramenslinie
                e.Graphics.DrawString(listView1.Items[x].SubItems[2].Text.ToString(), listView1.Font, Brushes.Black, df);
                #endregion

                #region
                Rectangle lb = new Rectangle();
                lb = listView1.Items[0].SubItems[3].Bounds; // übernimmt Rechteck der ersten Zeile, Spalte 0
                lb.X = df.X + df.Width;
                lb.Y = 100 + Höhe;
                e.Graphics.FillRectangle(Brushes.DarkGray, lb); // füllt Hintergrund
                e.Graphics.DrawRectangle(p, lb); //zeichnet Ramenslinie
                e.Graphics.DrawString(listView1.Items[x].SubItems[3].Text.ToString(), listView1.Font, Brushes.Black, lb);
                #endregion

                Höhe = Höhe + df.Height;  // für eine neue Zeile

            }

        }

    }
}


Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: So 28.02.16 18:41 
Zitat:
Was muss ich ändern, dass ich den Drucker auswählen kann?


Was man immer tun sollte wenn man etwas hat was ungefähr tut was man will aber nicht genau das. Man liest die Dokumentation.

Hint: die Funktion PrintDocument.Print ist die die nur ungefähr das macht was du willst. Vielleicht enthält die Doku ja hilfreiches.

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: So 28.02.16 21:13 
danke,
für alle die den Beitrag lesen und das gleiche Problem haben :-)

Das Sysmbol PrintDialog1 in das Projekt reinziehen U N D den Code hinzufügen.
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
printDialog1.Document = printDocument1;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
    printDocument1.Print();    // druckt sofort !!!!!!!!!!!!!!!!!!!!!!
}


soooooooooo einfach kann das sein :-)

Moderiert von user profile iconTh69: Code- durch C#-Tags ersetzt
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: So 06.03.16 22:21 
thx,all.
Noch eine Frage:
Was muss ich ändern
ausblenden C#-Quelltext
1:
2:
3:
Rectangle kontotext = new Rectangle(2051100500500);
Font dxu = new Font("Microsoft San Serif"8);
e.Graphics.DrawString("Commerzbank: Koblenz: IBAN: DE3xxxxxxxx:     Umsatzsteuer-Id-Nr. DE212345678", dxu, Brushes.Black, kontotext);


Was muss ich ändern, dass die Textzeile auf Seite 2 gedruckt wird?

Moderiert von user profile iconTh69: Code- durch C#-Tags ersetzt
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: So 06.03.16 22:28 
Zitat:
Was muss ich ändern, dass die Textzeile auf Seite 2 gedruckt wird?


Am gezeigten Code nix. Du mußt den halt erst beim 2.ten Aufruf des PrintPage Events ausführen.

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Mo 07.03.16 22:49 
ähhh, kannst du mir bitte ein Beispiel machen.
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mo 07.03.16 23:02 
Brauch ich nicht. Beispiele gibt es schon in der Doku ;)

Schau dir die Verwendung des PrintPage Events in der Doku zu PrintDocument.PrintPage an.
Insbesondere die Verwendung der HasMorePages Property aus den an den Event übergebenen PrintPageEventArgs.

Für diesen Beitrag haben gedankt: tomycat