Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I've created a restaurant POS and i'm having trouble with the listview printing

the target is to print in the receipt the following

Product Quantity Unit Price Total

I've managed through coding to print it, but I have another problem

using the following code

C#
graphics.DrawString("  " + listView1.Items[0].SubItems[0].Text + "      " + listView1.Items[0].SubItems[1].Text + "      " + listView1.Items[2].SubItems[0].Text + "      " + listView1.Items[0].SubItems[3].Text,  new Font("Arial Bold", 11),
                     new SolidBrush(Color.Black), startX, startY + Offset);


the problem is, this only prints one row of the listview

I've tried add multiple lines of the previous code to be able to print 6 rows

but it give an error when any of the rows are empty

how to make it print all items and subitems in a listview dynamically depending on the number of rows add by the user?

What I have tried:

here is the full code of the printing

C#
graphics.DrawString("  " + listView1.Items[0].SubItems[0].Text + "      " + listView1.Items[0].SubItems[1].Text + "      " + listView1.Items[2].SubItems[0].Text + "      " + listView1.Items[0].SubItems[3].Text,  new Font("Arial Bold", 11),
                     new SolidBrush(Color.Black), startX, startY + Offset);
Posted
Updated 22-Jun-23 12:56pm
v2
Comments
[no name] 18-Feb-17 18:04pm    
"the problem is, this only prints one row of the listview", no that is not a problem, that is exactly what you told it to do.
"add multiple lines of the previous code to be able to print 6 rows", okay... really bad idea but okay.
"but it give an error when any of the rows are empty", so check for an empty row.
Joey Arnanzo 18-Feb-17 18:13pm    
Ok, perhaps I didn't really point out what I want

User click button, add quantity to textbox, click addtocart button and that goes to the listview

now the number of listview rows printed is static with the number of lines of I write in code

So if I put 6 lines for 6 rows, user must add 6 items to the listview to be able to print

I want to make it print the number of rows the user enters according to what he chooses

am I making any sense? don't know why it feels difficult to explain

Thanks for your reply anyway
[no name] 18-Feb-17 18:54pm    
"I didn't really point out what I want", yes I see what you want. What I don't see is a question. What is your question? Why are you unable to write a simple for loop? This is basic programming 101.....
Joey Arnanzo 18-Feb-17 19:00pm    
Well, a good thing that you do understand my question, and I'm sorry if I'm not good enough to understand basic programming, but I'm not a programmer, I'm just a guy trying to help a friend who can't afford paying an actual programmer, your tone is very condescending, but thank you for your help, I believe now I'm supposed to lookup how to a loop, am I right?
[no name] 18-Feb-17 19:32pm    
It seems "condescending" because we aren't a "teach me programming" web site. Yes you should look up how to write a loop. then get your friend a present. Get a book on basic programming and have him go through all of the exercises. Then he would know this stuff.

As we've already figured this out in comments etc, but for completeness, here's the code

C#
for (int i = 0; i < listView1.Items.Count; i++)
{
  // Not sure what/why these two are here 
  int ii = 1;
  ii++;

  // Draw the row details for ? receipt 
  graphics.DrawString(" " + listView1.Items[i].SubItems[0].Text + listView1.Items[i].SubItems[1].Text +   
    listView1.Items[i].SubItems[2].Text + listView1.Items[i].SubItems[3].Text, new Font("Arial Bold", 11), 
    new SolidBrush(Color.Black), startX, startY + Offset);

  // Move the next print position 'down the page' ie, y axis increases from top to bottom
  Offset = Offset + 20;
}
 
Share this answer
 
v2
where offset is declare in program please help me to solve it

e.Graphics.DrawString(lvProductItems.Items[i].SubItems[0].Text + " " + lvProductItems.Items[i].SubItems[1].Text + " " + lvProductItems.Items[i].SubItems[2].Text + " " + lvProductItems.Items[i].SubItems[3].Text+ " " + lvProductItems.Items[i].SubItems[4].Text+ " " + lvProductItems.Items[i].SubItems[5].Text, new Font("Cambria", 9,FontStyle.Bold), new SolidBrush(Color.Black), 15,215 + offset);

error is showing in offset
 
Share this answer
 
Comments
Dave Kreskowiak 22-Jun-23 23:06pm    
You posted your question as an answer to an old question. DO NOT DO THAT!

Open your own question, with proper details, by going to the Quick Answers menu above and clicking "Ask a question".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900