Loop generates non-fatal problem

I’m trying to print labels from my app.

When I do not include a loop to print multiple labels based on the value of qty (an integer), I get a single label printing fine.

When I include a loop to print as many times as my quantity, I get the right number of labels printing, but the application generates a dialog box that says “This application has encountered a non-fatal problem. Report Now/Ignore” and then hangs until I force quit.

I suspect it may have something to do with variables becoming localized to the loop, but I can’t seem to figure out what to move around to make this work. Thoughts? Solution?

// print the label

Var ps As New PrinterSetup
Var g As Graphics
ps.Settings = App.s

If TextField_qty.Text.IsNumeric Then
  Var x as Integer
  Var qty as Integer = TextField_qty.text.ToInteger
  For x = 0 to qty 
    g = PrinterSetup.OpenPrinter(ps)
    g.FontName = "IDAutomationHC39M Free Version"
    g.FontSize = 12
    If g <> Nil Then
      // Print bar code label
      g.DrawText(TextArea_BarCode.text,15,50)
    end if  
  Next
end if

So again to clarify, the code above prints without error if I remove the For/Next statements, but of course only prints a single label.

A for/next loop automatically increments the counter x in this case. Try removing the line x=x+1.

Thanks for catching that. Doesn’t fix the issue but I’ve removed it since it’s distracting. I’ve actually also tried using While loops and Do loops as well, all with the same resulting problem.

Are you printing one label per “page”? I see that after each page’s worth of labels I do g.NextPage() to move to a new page. But printing doesn’t start until I do g = Nil.

In your case you’re perhaps printing all the labels on the same page on top of each other, after which when the method returns, g is destroyed and printing of the one label happens.

Why are you opening the printer in the loop?

  1. open the printer

  2. print your labels in a loop, using g.nextpage to move to a new page as required.

  3. after the loop, do g = Nil to get it all printed.

4 Likes

As Tim says move the line
g = PrinterSetup.OpenPrinter(ps)
to above the for next line

1 Like

I just refactored your code here. Not tested. But if something fails you will have an idea.

// print the labels

Var ps As New PrinterSetup
ps.Settings = App.s     // assuming it's correct...
Var g As Graphics = PrinterSetup.OpenPrinter(ps)

If g <> Nil And TextField_qty.Text.IsNumeric  Then

  Var qty as Integer = TextField_qty.text.ToInteger
  Var code As String = TextArea_BarCode.text
  g.FontName = "IDAutomationHC39M Free Version"
  g.FontSize = 12

  Do Until qty < 1
      // Print bar code label
      g.DrawText(code,15,50)
      g.NextPage()
      qty = qty - 1
  Loop

End if

THANK YOU @Rick_Araujo ! This fixed it. But even better is how you refactored my code. Xojo is my first step back into programming since Visual Basic circa 1995 or taking classes in C before VB… it’s easy for community members anywhere to be annoyed by “newbie” questions but the support here truly makes a difference. :slight_smile:

1 Like