我查看多列列表框中收到的所有数据。当我点击打印按钮时,它将打印出列表框中的所有数据查看。
这是我的代码:
1。首先,用户将根据ID搜索关键字。然后它将出现在多列listview中。这个数据是在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.