打印文件PrintDocument--我做错了什么?必须是愚蠢的东西 [英] Print Documents PrintDocument-- what am I doing wrong? Must besomething stupid

查看:104
本文介绍了打印文件PrintDocument--我做错了什么?必须是愚蠢的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用完打印纸试图调试这个...它必须是琐碎的,但是我无法弄清楚 - 你能吗?为什么我不打印

文本,而只是初始字符串howdy?


在屏幕上,当我打开文件时,整个内容实际上显示的文件是

...所以为什么我以后不能打印它?所有这些代码

我从一本书(Chris Sells)和网上得到的。解决方案是

可以找到这样的事实:尽管我做了它,但是stringbuilder没有在''使用'支架之外保留
信息

''全球''。


关键字搜索// !!!下面看看我认为问题出在哪里。


RL


使用系统;

使用System.Collections .Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.IO;

使用System.Windows.Forms;

使用System.Diagnostics;

名称空间MyNameSpace1

{

公共部分类MyForm:表格

{

string myPrintFilename;

StringBuilder myGlobalStringBuilder; //!这是假设

是MyForm形式的全局,对吧?


string strModified; // = String.Copy(strOriginal); //未使用


public MyForm()

{

InitializeComponent();

myGlobalStringBuilder = new StringBuilder(howdy); // !!!

打印的唯一内容就是''你好''!


}


private void toolStripButton1_Click(object sender,EventArgs

e)

{

流myStream;

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.Title ="打开文本文件" ;;

openFileDialog1.InitialDirectory = @" c:\" ;;

openFileDialog1.Filter =" txt files(* .txt)| * .txt | All

files(*。*)| *。*" ;;


if(openFileDialog1.ShowDialog()== DialogResult.OK)

{

试试

{

if((myStream = openFileDialog1.OpenFile())!=

null)

{

using(myStream)

{

StreamReader sr =

File.OpenText(openFileDialog1.FileName);

string s = sr.ReadLine() ;

StringBuilder sb = new StringBuilder();

while(s!= null)

{

sb 。附加(s);

s = sr.ReadLine();


myGlobalStringBuilder.Append(s); //! ???为什么myGlobal不是

附加在这里?

}

sr.Close();

textBox1.Text = sb.ToString(); //这个

有效,在屏幕上显示文件文字textBox1


}

}

}

catch(例外情况)

{

MessageBox.Show(&;错误:无法读取文件来自

disk(myStream); Err:" + ex.Message);

}

}

}


private void printToolStripButton_Click(object sender,

EventArgs e)

{

if(myPrintFilename!=" ")

{

this.printDocument1.DocumentName =

this.myPrintFilename;


this.printDocument1.Print();

}

}


private void printDocument1_PrintPage(object sender,

System.Drawing.Printing.PrintPageEventArgs e)

{

// p。 292 Chris Sells

//绘制到包装印刷品的e.Graphics对象

目标


图形g = e。图形;

使用(Font font = new Font(Lucida Console,48))

{

string mylocalstring;


mylocalstring =

myGlobalStringBuilder.ToString(); //!只打印Howdy -

初始字符串 - 永远不会来自myGlobalStringBuilder的附加字符串 -

为什么?


if(myGlobalStringBuilder.Length!= 0)

{

g.DrawString(mylocalstring,font,Brushes.Blue,0,

0 );

}

}


}

}

}

I am running out of printing paper trying to debug this...it has to be
trivial, but I cannot figure it out--can you? Why am I not printing
text, but just the initial string "howdy"?

On the screen, when I open a file, the entire contents of the file is
in fact being shown...so why can''t I print it later? All of this code
I am getting from a book (Chris Sells) and the net. The solution is
to be found in the fact that stringbuilder is not retaining
information outside the ''using'' bracket, despite the fact I made it
''global''.

Keyword search //!!! below to see where I think the problem lies.

RL

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
namespace MyNameSpace1
{
public partial class MyForm : Form
{
string myPrintFilename;
StringBuilder myGlobalStringBuilder; //!!! this is supposed
to be global to the form MyForm, right?

string strModified; // = String.Copy(strOriginal); //not used

public MyForm()
{
InitializeComponent();
myGlobalStringBuilder = new StringBuilder("howdy"); //!!!
the only thing that gets printed is ''howdy''!

}

private void toolStripButton1_Click(object sender, EventArgs
e)
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "Open text file";
openFileDialog1.InitialDirectory = @"c:\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All
files (*.*)|*.*";

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) !=
null)
{
using (myStream)
{
StreamReader sr =
File.OpenText(openFileDialog1.FileName);
string s = sr.ReadLine();
StringBuilder sb = new StringBuilder();
while (s != null)
{
sb.Append(s);
s = sr.ReadLine();

myGlobalStringBuilder.Append(s); //!!! ??? Why is myGlobal not
appending here?
}
sr.Close();
textBox1.Text = sb.ToString(); //this
works, to show the file text on the screen textBox1

}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: could not read file from
disk (myStream); Err: " + ex.Message);
}
}
}

private void printToolStripButton_Click(object sender,
EventArgs e)
{
if (myPrintFilename != "")
{
this.printDocument1.DocumentName =
this.myPrintFilename;

this.printDocument1.Print();
}
}

private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
//p. 292 Chris Sells
//draw to the e.Graphics object that wraps the print
target

Graphics g = e.Graphics;
using (Font font = new Font("Lucida Console", 48) )
{
string mylocalstring;

mylocalstring =
myGlobalStringBuilder.ToString(); //!!! only prints "Howdy"--the
initial string--never the appended string from myGlobalStringBuilder--
why?

if (myGlobalStringBuilder.Length != 0)
{
g.DrawString(mylocalstring, font, Brushes.Blue, 0,
0);
}
}

}
}
}

推荐答案

raylopez99< ra ******** @ yahoo.comwrote:
raylopez99 <ra********@yahoo.comwrote:

我用完打印纸试图调试这个......它必须是琐碎的,但是我无法弄明白 - 你能吗?为什么我不打印

文本,而只是初始字符串howdy?
I am running out of printing paper trying to debug this...it has to be
trivial, but I cannot figure it out--can you? Why am I not printing
text, but just the initial string "howdy"?



您的代码有4个问题...

1.变量myPrintFilename未设置为

openFileDialog1.FileName 2.当你使用AppendLine 3时你正在使用Append

。你需要在

sr.ReadLine之后使用myGlobalStringBuilder在sb.Append之后(应该是

sb.AppendLine)4。你正在使用Lucinda 48而不是什么

更小......


在屏幕上,当我打开一个文件时,该文件的全部内容实际上都显示了......所以为什么可以''我以后打印出来吗?所有这些代码

我从一本书(Chris Sells)和网上得到的。解决方案是

可以找到这样的事实:尽管我做了它,但是stringbuilder没有在''使用'支架之外保留
信息

''全球''。


关键字搜索// !!!下面看看我认为问题出在哪里。


RL


使用系统;

使用System.Collections .Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.IO;

使用System.Windows.Forms;

使用System.Diagnostics;


命名空间MyNameSpace1

{

公共部分类MyForm:表格

{

string myPrintFilename;

StringBuilder myGlobalStringBuilder; //!这是假设

是MyForm形式的全局,对吧?


string strModified; // = String.Copy(strOriginal); //未使用


public MyForm()

{

InitializeComponent();

myGlobalStringBuilder = new StringBuilder(howdy); // !!!

打印的唯一内容就是''你好''!


}


private void toolStripButton1_Click(object sender,EventArgs

e)

{

流myStream;

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.Title ="打开文本文件" ;;

openFileDialog1.InitialDirectory = @" c:\" ;;

openFileDialog1.Filter =" txt files(* .txt)| * .txt | All

files(*。*)| *。*" ;;


if(openFileDialog1.ShowDialog()== DialogResult.OK)

{

试试

{

if((myStream = openFileDialog1.OpenFile())!=

null)

{

using(myStream)

{

StreamReader sr =

File.OpenText(openFileDialog1.FileName);

string s = sr.ReadLine();

StringBuilder sb = new StringBuilder();

while(s!= null)

{

sb.Append(s);

s = sr.ReadLine();


myGlobalStringBuilder.Append(s); //! ???为什么myGlobal不是

附加在这里?

}

sr.Close();

textBox1.Text = sb.ToString(); //这个

有效,在屏幕上显示文件文字textBox1


}

}

}

catch(例外情况)

{

MessageBox.Show(&;错误:无法读取文件来自

disk(myStream); Err:" + ex.Message);

}

}

}


private void printToolStripButton_Click(object sender,

EventArgs e)

{

if(myPrintFilename!=" ")

{

this.printDocument1.DocumentName =

this.myPrintFilename;


this.printDocument1.Print();

}

}


private void printDocument1_PrintPage(object sender,

System.Drawing.Printing.PrintPageEv entArgs e)

{

// p。 292 Chris Sells

//绘制到包装印刷品的e.Graphics对象

目标


图形g = e。图形;

使用(Font font = new Font(Lucida Console,48))

{

string mylocalstring;


mylocalstring =

myGlobalStringBuilder.ToString(); //!只打印Howdy -

初始字符串 - 永远不会来自myGlobalStringBuilder的附加字符串 -

为什么?


if(myGlobalStringBuilder.Length!= 0)

{

g.DrawString(mylocalstring,font,Brushes.Blue,0,

0 );

}

}


}


}

}
On the screen, when I open a file, the entire contents of the file is
in fact being shown...so why can''t I print it later? All of this code
I am getting from a book (Chris Sells) and the net. The solution is
to be found in the fact that stringbuilder is not retaining
information outside the ''using'' bracket, despite the fact I made it
''global''.

Keyword search //!!! below to see where I think the problem lies.

RL

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;

namespace MyNameSpace1
{
public partial class MyForm : Form
{
string myPrintFilename;
StringBuilder myGlobalStringBuilder; //!!! this is supposed
to be global to the form MyForm, right?

string strModified; // = String.Copy(strOriginal); //not used

public MyForm()
{
InitializeComponent();
myGlobalStringBuilder = new StringBuilder("howdy"); //!!!
the only thing that gets printed is ''howdy''!

}

private void toolStripButton1_Click(object sender, EventArgs
e)
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "Open text file";
openFileDialog1.InitialDirectory = @"c:\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All
files (*.*)|*.*";

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) !=
null)
{
using (myStream)
{
StreamReader sr =
File.OpenText(openFileDialog1.FileName);
string s = sr.ReadLine();
StringBuilder sb = new StringBuilder();
while (s != null)
{
sb.Append(s);
s = sr.ReadLine();

myGlobalStringBuilder.Append(s); //!!! ??? Why is myGlobal not
appending here?
}
sr.Close();
textBox1.Text = sb.ToString(); //this
works, to show the file text on the screen textBox1

}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: could not read file from
disk (myStream); Err: " + ex.Message);
}
}
}

private void printToolStripButton_Click(object sender,
EventArgs e)
{
if (myPrintFilename != "")
{
this.printDocument1.DocumentName =
this.myPrintFilename;

this.printDocument1.Print();
}
}

private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
//p. 292 Chris Sells
//draw to the e.Graphics object that wraps the print
target

Graphics g = e.Graphics;
using (Font font = new Font("Lucida Console", 48) )
{
string mylocalstring;

mylocalstring =
myGlobalStringBuilder.ToString(); //!!! only prints "Howdy"--the
initial string--never the appended string from myGlobalStringBuilder--
why?

if (myGlobalStringBuilder.Length != 0)
{
g.DrawString(mylocalstring, font, Brushes.Blue, 0,
0);
}
}

}

}
}



-

J. Moreno

--
J. Moreno


开8月28日,5:28 * am,J。Moreno< pl ... @ newsreaders.comwrote:
On Aug 28, 5:28*am, J. Moreno <pl...@newsreaders.comwrote:

raylopez99< raylope ... @ yahoo.comwrote :
raylopez99 <raylope...@yahoo.comwrote:

我用完打印纸试图调试这个...它必须是琐碎的b / b
,但是我无法想象它出去 - 你呢? *为什么我不打印

文本,而只是初始字符串howdy?
I am running out of printing paper trying to debug this...it has to be
trivial, but I cannot figure it out--can you? *Why am I not printing
text, but just the initial string "howdy"?



您的代码有4个问题...

* * 1。变量myPrintFilename没有被设置为

* * openFileDialog1.FileName 2.你正在使用Append应该是

* *使用AppendLine 3.你有

* * * sr.ReadLine之后的myGlobalStringBuilder而不是sb.Append之后(应该是

* * sb.AppendLine)4。你正在使用Lucinda 48,而不是什么

* *较小...


There''s 4 things wrong with your code...
* *1. the variable myPrintFilename isn''t being set to
* *openFileDialog1.FileName 2. You are using Append when you should be
* *using AppendLine 3. You have the myGlobalStringBuilder after the
* * * sr.ReadLine instead of after the sb.Append (which should be
* *sb.AppendLine) 4. You''re using Lucinda 48, instead of something
* *smaller...



这里还有一个。这个:


if((myStream = openFileDialog1.OpenFile())!=

null)

{

使用(myStream)

{

StreamReader sr =

File.OpenText(openFileDialog1.FileName);

string s = sr.ReadLine();

StringBuilder sb = new StringBuilder();

while(s!= null)

{

sb.Append(s);

s = sr.ReadLine();


myGlobalStringBuilder.Append(s) ; //! ???为什么myGlobal不是

附加在这里?

}

sr.Close();

textBox1.Text = sb.ToString(); //这个

有效,在屏幕上显示文件文字textBox1


}

}


注意myStream从未实际使用过。此外,在using-expression之外初始化是毫无意义的。

There''s one more here. This:

if ((myStream = openFileDialog1.OpenFile()) !=
null)
{
using (myStream)
{
StreamReader sr =
File.OpenText(openFileDialog1.FileName);
string s = sr.ReadLine();
StringBuilder sb = new StringBuilder();
while (s != null)
{
sb.Append(s);
s = sr.ReadLine();

myGlobalStringBuilder.Append(s); //!!! ??? Why is myGlobal not
appending here?
}
sr.Close();
textBox1.Text = sb.ToString(); //this
works, to show the file text on the screen textBox1

}
}

Note how myStream is never actually used. Also, it is pointlessly
initialized outside the using-expression.


在printDocument1_PrintPage中设置一个断点,在
$行b $ bmylocalstring已设定。我相信你会看到

" myStringBuilder"的值是正确的。缺少回车将隐藏打印时

字符串正确的事实。我相信它会离开页面。

打印文档功能不会修复换行符,换行等。另外,当文本很长时,你需要

来担心下一页。打印是非常简单的.net,直到你掌握了它...


" raylopez99"写道:
Set a breakpoint in printDocument1_PrintPage, at the line where
"mylocalstring" is set. I believe you will see that the value is correct for
"myStringBuilder". The lack of carriage returns will hide the fact that the
string is correct when printed. I believe it is going off the page. The
print document functions won''t fix newlines, wrapping, etc. Also, you need
to worry about the next page when the text is long. Printing is very
complicated in .net, until you get the hang of it...

"raylopez99" wrote:

我用完了打印纸试图调试这个...它必须是琐碎的b $ b b b b b b b b b它出来了 - 你呢?为什么我不打印

文本,而只是初始字符串howdy?


在屏幕上,当我打开文件时,整个内容实际上显示的文件是

...所以为什么我以后不能打印它?所有这些代码

我从一本书(Chris Sells)和网上得到的。解决方案是

可以找到这样的事实:尽管我做了它,但是stringbuilder没有在''使用'支架之外保留
信息

''全球''。


关键字搜索// !!!下面看看我认为问题出在哪里。


RL


使用系统;

使用System.Collections .Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.IO;

使用System.Windows.Forms;

使用System.Diagnostics;


命名空间MyNameSpace1

{

公共部分类MyForm:表格

{

string myPrintFilename;

StringBuilder myGlobalStringBuilder; //!这是假设

是MyForm形式的全局,对吧?


string strModified; // = String.Copy(strOriginal); //未使用


public MyForm()

{

InitializeComponent();

myGlobalStringBuilder = new StringBuilder(howdy); // !!!

打印的唯一内容就是''你好''!


}


private void toolStripButton1_Click(object sender,EventArgs

e)

{

流myStream;

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.Title ="打开文本文件" ;;

openFileDialog1.InitialDirectory = @" c:\" ;;

openFileDialog1.Filter =" txt files(* .txt)| * .txt | All

files(*。*)| *。*" ;;


if(openFileDialog1.ShowDialog()== DialogResult.OK)

{

试试

{

if((myStream = openFileDialog1.OpenFile())!=

null)

{

using(myStream)

{

StreamReader sr =

File.OpenText(openFileDialog1.FileName);

string s = sr.ReadLine();

StringBuilder sb = new StringBuilder();

while(s!= null)

{

sb.Append(s);

s = sr.ReadLine();


myGlobalStringBuilder.Append(s); //! ???为什么myGlobal不是

附加在这里?

}

sr.Close();

textBox1.Text = sb.ToString(); //这个

有效,在屏幕上显示文件文字textBox1


}

}

}

catch(例外情况)

{

MessageBox.Show(&;错误:无法读取文件来自

disk(myStream); Err:" + ex.Message);

}

}

}


private void printToolStripButton_Click(object sender,

EventArgs e)

{

if(myPrintFilename!=" ")

{

this.printDocument1.DocumentName =

this.myPrintFilename;


this.printDocument1.Print();

}

}


private void printDocument1_PrintPage(object sender,

System.Drawing.Printing.PrintPageEv entArgs e)

{

// p。 292 Chris Sells

//绘制到包装印刷品的e.Graphics对象

目标


图形g = e。图形;

使用(Font font = new Font(Lucida Console,48))

{

string mylocalstring;


mylocalstring =

myGlobalStringBuilder.ToString(); //!只打印Howdy -

初始字符串 - 永远不会来自myGlobalStringBuilder的附加字符串 -

为什么?


if(myGlobalStringBuilder.Length!= 0)

{

g.DrawString(mylocalstring,font,Brushes.Blue,0,

0 );

}

}


}


}

}

I am running out of printing paper trying to debug this...it has to be
trivial, but I cannot figure it out--can you? Why am I not printing
text, but just the initial string "howdy"?

On the screen, when I open a file, the entire contents of the file is
in fact being shown...so why can''t I print it later? All of this code
I am getting from a book (Chris Sells) and the net. The solution is
to be found in the fact that stringbuilder is not retaining
information outside the ''using'' bracket, despite the fact I made it
''global''.

Keyword search //!!! below to see where I think the problem lies.

RL

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
namespace MyNameSpace1
{
public partial class MyForm : Form
{
string myPrintFilename;
StringBuilder myGlobalStringBuilder; //!!! this is supposed
to be global to the form MyForm, right?

string strModified; // = String.Copy(strOriginal); //not used

public MyForm()
{
InitializeComponent();
myGlobalStringBuilder = new StringBuilder("howdy"); //!!!
the only thing that gets printed is ''howdy''!

}

private void toolStripButton1_Click(object sender, EventArgs
e)
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "Open text file";
openFileDialog1.InitialDirectory = @"c:\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All
files (*.*)|*.*";

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) !=
null)
{
using (myStream)
{
StreamReader sr =
File.OpenText(openFileDialog1.FileName);
string s = sr.ReadLine();
StringBuilder sb = new StringBuilder();
while (s != null)
{
sb.Append(s);
s = sr.ReadLine();

myGlobalStringBuilder.Append(s); //!!! ??? Why is myGlobal not
appending here?
}
sr.Close();
textBox1.Text = sb.ToString(); //this
works, to show the file text on the screen textBox1

}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: could not read file from
disk (myStream); Err: " + ex.Message);
}
}
}

private void printToolStripButton_Click(object sender,
EventArgs e)
{
if (myPrintFilename != "")
{
this.printDocument1.DocumentName =
this.myPrintFilename;

this.printDocument1.Print();
}
}

private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
//p. 292 Chris Sells
//draw to the e.Graphics object that wraps the print
target

Graphics g = e.Graphics;
using (Font font = new Font("Lucida Console", 48) )
{
string mylocalstring;

mylocalstring =
myGlobalStringBuilder.ToString(); //!!! only prints "Howdy"--the
initial string--never the appended string from myGlobalStringBuilder--
why?

if (myGlobalStringBuilder.Length != 0)
{
g.DrawString(mylocalstring, font, Brushes.Blue, 0,
0);
}
}

}
}
}


这篇关于打印文件PrintDocument--我做错了什么?必须是愚蠢的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆