我在多列列表框中查看所有接收到的数据。当我单击打印按钮时,它将打印出列表框中的所有数据审查。
这是我的代码:
1.首先,用户会根据ID搜索关键词。然后它将出现在多列列表视图中。此数据在 xml 文件中搜索。
XmlDocument xml = new XmlDocument();
xml.Load("C:\\Users\\HDAdmin\\Documents\\Fatty\\SliceEngine\\SliceEngine\\bin\\Debug\\patient.xml");
XmlNodeList xnList = xml.SelectNodes("/main/patient");
foreach (XmlNode xn in xnList)
{
string date = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "Date").FirstChild.Value;
string id = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "ID").FirstChild.Value;
if (date == cari)
{
listviewitem = new ListViewItem(date);
listviewitem.SubItems.Add("Smith");
this.listView1.Items.Add(listviewitem);
}
if (id == cari)
{
listviewitem = new ListViewItem(date);
id = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "ID").FirstChild.Value;
listviewitem.SubItems.Add(id);
string level = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "Level").FirstChild.Value;
listviewitem.SubItems.Add(level);
string name = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "Name").FirstChild.Value;
listviewitem.SubItems.Add(name);
2.用同样的方法,我添加了打印事件按钮
this.components = new System.ComponentModel.Container();
this.printBut = new System.Windows.Forms.Button();
this.ClientSize = new System.Drawing.Size(504, 381);
this.Text = "Print Example";
printBut.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
printBut.Location = new System.Drawing.Point(32, 110);
printBut.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
printBut.TabIndex = 0;
printBut.Text = "Print the file.";
printBut.Size = new System.Drawing.Size(136, 40);
printBut.Click += new System.EventHandler(printBut_Click);
this.Controls.Add(printBut);
//-------------print-----------------
3.这是 printBut_click。
private void printBut_Click(object sender, EventArgs e)
{
try
{
StreamReader streamToPrint = new StreamReader("C:\\Users\\HDAdmin\\Documents\\Fatty\\SliceEngine\\SliceEngine\\bin\\Debug\\aboutREPCS.txt");
try
{
printFont = new Font("Arial", 10);
pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
4.这是 printPage 事件
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics);
// Print each line of the file.
while (count < linesPerPage &&
((line = streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
当我运行此代码时,它会出现object reference not set to an instance of an object
。
所以,让我重复一下这个应用程序的流程。
User insert keyword.
If keyword match, view data from xml file into multicolumn listview.
printBut will print all the data view in listview.