Note that there are some explanatory texts on larger screens.

plurals
  1. POprint data from multicolumn listview c#
    text
    copied!<p>i view all the data receive in multicolumn listbox. when i click print button, it will print out all the data review in listbox.</p> <p>this is my code:</p> <p><strong>1.</strong> at first, the user will search keyword based on ID. then it will appear in multicolumn listview.this data is search in an xml file.</p> <pre><code>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&lt;XmlNode&gt;().FirstOrDefault(n =&gt; n.Name == "Date").FirstChild.Value; string id = xn.OfType&lt;XmlNode&gt;().FirstOrDefault(n =&gt; 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&lt;XmlNode&gt;().FirstOrDefault(n =&gt; n.Name == "ID").FirstChild.Value; listviewitem.SubItems.Add(id); string level = xn.OfType&lt;XmlNode&gt;().FirstOrDefault(n =&gt; n.Name == "Level").FirstChild.Value; listviewitem.SubItems.Add(level); string name = xn.OfType&lt;XmlNode&gt;().FirstOrDefault(n =&gt; n.Name == "Name").FirstChild.Value; listviewitem.SubItems.Add(name); </code></pre> <p><strong>2.</strong> in the same method, i add up the print event button</p> <pre><code> 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----------------- </code></pre> <p><strong>3.</strong> this is printBut_click.</p> <pre><code>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); } } </code></pre> <p><strong>4.</strong> this is printPage event </p> <pre><code>// 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 &lt; linesPerPage &amp;&amp; ((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; } </code></pre> <p>when i run this code, it appears <code>object reference not set to an instance of an object</code>. <img src="https://i.stack.imgur.com/y2OwK.png" alt="enter image description here"></p> <p>so, let me repeat the flow of this application.</p> <pre><code>User insert keyword. If keyword match, view data from xml file into multicolumn listview. printBut will print all the data view in listview. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload