Hallo Gemeinde,
ich habe in einer ListBox einen Text stehen, den ich durch "\t" trenne.
Das ganze schaut optisch auch genau so aus wie ich es gerne haben möchte, jedoch wenn ich das ganze dann ausdrucken möchte, werden meine "\t" nicht übernommen.
Was muss ich anstellen, das der Drucker die formatierung behält?
ListBox -> Text1 Text2 Text3
Drucker -> Text1Text2Text3
private StringReader myReader;
protected void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPosition = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
Font printFont = this.listBox1.Font;
SolidBrush myBrush = new SolidBrush(Color.Black);
// Work out the number of lines per page, using the MarginBounds.
linesPerPage =
ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
// Iterate over the string using the StringReader, printing each line.
while (count < linesPerPage && ((line = myReader.ReadLine()) != null))
{
// calculate the next line position based on
// the height of the font according to the printing device
yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics));
// draw the next line in the rich edit control
ev.Graphics.DrawString(line, printFont,
myBrush, leftMargin,
yPosition, new StringFormat());
count++;
}
// If there are more lines, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
myBrush.Dispose();
}
private void druck_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
string strText = "";
foreach (object x in listBox1.Items)
{
strText = strText + x.ToString() + "\n";
}
myReader = new StringReader(strText);
if (printDialog1.ShowDialog() == DialogResult.OK)
{
this.printDocument1.Print();
}
}
Danke!
Gruß
T
Habe das Programm nun von einer ListBox auf eine ListView abgeändert und kann auch bereits einen Teil per Drucker ausgeben, jedoch nicht wirklich das was ich gerne hätte...
Ich habe in meiner ListView 3 Spalten und X Zeilen.
Der Inhalt sieht z.B. so aus
2 6 6
3 5 5
4 5 4
....
Wenn ich das ganze dann drucken möchte, spuckt er aber folgendes aus:
ListViewItem: {2}
ListViewItem: {3}
ListViewItem: {4}
....
Was muss ich anstellen, damit ich 1. alle Spalen und 2. nur den Inhalt, so wie er mir in der Box auch angezeigt wird, gedruckt bekomme? Der Code für das Drucken ist immer noch der aus dem vorherigen Post.
LG
T
Hallo,
eine ListView zu drucken ist nicht ganz so einfach wie bei einer ListBox, da du ja die einzelnen Spalten beachten mußt.
Schau dir mal die Links dazu auf CodeProject an:
Printable ListView
Easily turn a ListView into a nicely printed report, complete with print preview
Hallo TLoD,
leider muss man sich hier selbst um das Layout kümmern. Ich würde es mal nach folgendem Algo aufziehen:
Gruß
Danke, bin ich auch schon drüber gestoßen.
Die letztere Variante wollte ich mal getestet haben, jedoch fehlt mir dazu das Wissen...
Da ich deren Code nicht implementieren konnte, habe ich nun meinen in das Tool gepackt.
Ist zwar sehr unschön, aber funktioniert erstmal...