+ Reply to Thread
Results 1 to 8 of 8

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

  1. #1
    sam. is offline VB.NET Forum Newbie sam. is on a distinguished programming path ahead
    .NET Framework
    .NET 3.0
    Join Date
    Mar 2010
    Age
    19
    Posts
    2
    Reputation
    0

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

    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

  2. #2
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist
    .NET Framework
    .NET 2.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Age
    26
    Posts
    3,897
    Reputation
    524

    Default

    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)))
    Currently using: VS 2005 & 2008 Pro w/sp1 & VS 2010 Ultimate on Win7 Ultimate x64.


    There are 3 kinds of people in the world: Those who can count and those who can't.
    4 out of 3 people have trouble with fractions.

    Windows has a 64 bit GUI for a set of 32 bit extensions on a 16 bit shell for an 8 bit OS using a 4 bit kernel made by a 2 bit company that can't stand 1 bit of competition.

  3. #3
    newguy's Avatar
    newguy is offline VB.NET Forum Idol newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    609
    Reputation
    216

    Default

    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 & """"
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  4. #4
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist
    .NET Framework
    .NET 2.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Age
    26
    Posts
    3,897
    Reputation
    524

    Default

    Quote Originally Posted by newguy View Post
    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.
    Currently using: VS 2005 & 2008 Pro w/sp1 & VS 2010 Ultimate on Win7 Ultimate x64.


    There are 3 kinds of people in the world: Those who can count and those who can't.
    4 out of 3 people have trouble with fractions.

    Windows has a 64 bit GUI for a set of 32 bit extensions on a 16 bit shell for an 8 bit OS using a 4 bit kernel made by a 2 bit company that can't stand 1 bit of competition.

  5. #5
    newguy's Avatar
    newguy is offline VB.NET Forum Idol newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    609
    Reputation
    216

    Default

    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?
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  6. #6
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist JuggaloBrotha VB.NET gold medalist
    .NET Framework
    .NET 2.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Age
    26
    Posts
    3,897
    Reputation
    524

    Default

    Quote Originally Posted by newguy View Post
    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.
    Currently using: VS 2005 & 2008 Pro w/sp1 & VS 2010 Ultimate on Win7 Ultimate x64.


    There are 3 kinds of people in the world: Those who can count and those who can't.
    4 out of 3 people have trouble with fractions.

    Windows has a 64 bit GUI for a set of 32 bit extensions on a 16 bit shell for an 8 bit OS using a 4 bit kernel made by a 2 bit company that can't stand 1 bit of competition.

  7. #7
    newguy's Avatar
    newguy is offline VB.NET Forum Idol newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame newguy puts e.f. hutton to shame
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    609
    Reputation
    216

    Default

    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.
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  8. #8
    sam. is offline VB.NET Forum Newbie sam. is on a distinguished programming path ahead
    .NET Framework
    .NET 3.0
    Join Date
    Mar 2010
    Age
    19
    Posts
    2
    Reputation
    0

    Default

    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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts