Question Visual Basic: How to print the text from a list box

sam.

New member
Joined
Mar 17, 2010
Messages
2
Programming Experience
Beginner
i have made a basic program for college which makes a calculation and diplays the information in a listbox, how do i then print that text? :D
really appreciate any help
 
There's a couple of ways:
VB.NET:
For Counter As Integer = 0 to ListBox.Items.Count - 1
    Messagebox.Show(ListBox.Items(Counter).ToString)
Next
or
VB.NET:
Messagebox.Show(ListBox.GetItemText(ListBox.Items(ListBox.SelectedIndex)))
 
This shows how to send each line to a txt file then print it automatically:
VB.NET:
Using sw As New StreamWriter(filePath & "mytestprint.txt")
For Each line As String In ListBox1.Items
   sw.WriteLine(line)
Next
End Using
Dim Proc As New Process
Proc.StartInfo.FileName = filePath & "mytestprint.txt"
Proc.StartInfo.Verb = "Print"
Proc.StartInfo.CreateNoWindow = True
Proc.StartInfo.UseShellExecute = True
Proc.Start()
Proc.WaitForExit()
Proc.Dispose()
And this for sending it to a specific printer:
VB.NET:
Proc.StartInfo.Verb = "PrintTo"
Proc.StartInfo.Arguments = """" & Printer_Name & """"
 
This shows how to send each line to a txt file then print it automatically:
VB.NET:
Using sw As New StreamWriter(filePath & "mytestprint.txt")
For Each line As String In ListBox1.Items
   sw.WriteLine(line)
Next
End Using
Dim Proc As New Process
Proc.StartInfo.FileName = filePath & "mytestprint.txt"
Proc.StartInfo.Verb = "Print"
Proc.StartInfo.CreateNoWindow = True
Proc.StartInfo.UseShellExecute = True
Proc.Start()
Proc.WaitForExit()
Proc.Dispose()
And this for sending it to a specific printer:
VB.NET:
Proc.StartInfo.Verb = "PrintTo"
Proc.StartInfo.Arguments = """" & Printer_Name & """"
If he's wanting to print something, this is probably the worst way I've ever seen it done.

There's the PrintDocument and PrintDialog that's already in the Framework for printing to a printer, he can even use the PrintPreview or the PrintPreviewDialog control for testing and debugging the print routines without actually sending it to the printer.

But since he hasn't explained what he wants done, all I did was show him how he can get the text of the item(s) in the ListBox.
 
i have made a basic program for college which makes a calculation and diplays the information in a listbox, how do i then print that text?
really appreciate any help

Well that was kinda viscous! What up JB?
 
Yeah, so I just threw out an idea. I don't see anything wrong with my approach - if I don't want to bug the user with another dialog box when I know what I want to do and it works just fine for that. Yes we needed more info, but I did not mind offering up an idea anyway. There are many ways to skin a cat - but to flat criticize it... If it is so wrong tell me why. I am self taught and when I research things there is nothing there to me this way is the jacked up way.
 
cheers for all the help, right or wrong, i appreciate the time to answer! ill let you know which one works best for me when im back at college in a few days :D
 
Back
Top