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
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
There's a couple of ways:
Code:
For Counter As Integer = 0 to ListBox.Items.Count - 1
    Messagebox.Show(ListBox.Items(Counter).ToString)
Next
or
Code:
Messagebox.Show(ListBox.GetItemText(ListBox.Items(ListBox.SelectedIndex)))
 

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
This shows how to send each line to a txt file then print it automatically:
Code:
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:
Code:
Proc.StartInfo.Verb = "PrintTo"
Proc.StartInfo.Arguments = """" & Printer_Name & """"
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
This shows how to send each line to a txt file then print it automatically:
Code:
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:
Code:
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.
 

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
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?
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
Well that was kinda viscous! What up JB?
Sure, print the text to what? A MessageBox? The screen? A control on the form? A printer? He didn't specify anything.
 

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
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.
 

sam.

New member
Joined
Mar 17, 2010
Messages
2
Programming Experience
Beginner
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
 
Top Bottom