Results 1 to 10 of 10

Thread: [RESOLVED] Print data from a ListBox (?)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2009
    Location
    Scotland, UK
    Posts
    26

    Resolved [RESOLVED] Print data from a ListBox (?)

    Hi there. I was just wondering if there was a way for me to copy each item in a ListBox to a TextBox.

    I am making a Pin Number Generator Application and all the Pin numbers get added into a ListBox.

    I want to copy all the data in the ListBox into a TextBox. Prefferibly one pin on a line in the TextBox.

    This is so I could save all of the pin numbers into a text file.

    The TextBox will be called txtBox and the ListBox is called lstPass.

    Thanks to those who can help me. Also i would be greatful if you could comment the code.


    Thanks in advanced.
    Alistair.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Print data from a ListBox (?)

    The above is pretty simple. Maybe you should show us what you have tried. A vast majority of the people here can help on this one.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Hyperactive Member Jimq's Avatar
    Join Date
    Apr 2009
    Posts
    260

    Re: Print data from a ListBox (?)

    i think you wanna put all list items finaly in a text file, right
    here's d code
    Code:
    Dim i As Integer
    Dim fileNum As Integer
    
     fileNum = FreeFile()
        Open "C:\" & "\list.txt" For Output As #fileNum
            For i = 0 To List1.ListCount - 1
                Print #fileNum, List1.List(i)
            Next i
        Close #fileNum
    put this code into a command button or something...
    hope it will work

    Do reply

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2009
    Location
    Scotland, UK
    Posts
    26

    Re: Print data from a ListBox (?)

    Thanks LaVolpe. I have not tried anything because i have no idea how to do it.

    1) I have a commmand button that generates the passwords and populates the listbox.

    I want to add an another command button to add all of the items in the listbox into a textbox so i can save them to a text file.

    I don't know if there is a way of copying everything from the listbox into a file without a textbox.


    Thanks.
    Alistair.

  5. #5
    Hyperactive Member Jimq's Avatar
    Join Date
    Apr 2009
    Posts
    260

    Re: Print data from a ListBox (?)

    Have u read my reply!!!!!!\

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2009
    Location
    Scotland, UK
    Posts
    26

    Re: Print data from a ListBox (?)

    Thanks Jimq. Do i need to put:

    Dim i As Integer
    Dim fileNum As Integer

    Into the General Declerations or the CommandButton Sub? Or does it not really matter?

  7. #7
    Hyperactive Member Jimq's Avatar
    Join Date
    Apr 2009
    Posts
    260

    Re: Print data from a ListBox (?)

    nywhr u want

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2009
    Location
    Scotland, UK
    Posts
    26

    Re: Print data from a ListBox (?)

    Jimq: Does the file need to be made first before it can get saved?

    Sorry i'm a bit silly!

  9. #9
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Print data from a ListBox (?)

    Design a form with two command buttons, a list box and a text box. Set the MultiLine property of the textbox to True and and add a vertical scrollbar. Then apply this code:
    Code:
    Const ListSize = 20
    Private Sub Command1_Click()
    ' Write List Box Contents to File and to Text Box
    Open "MyDataFile" For Output As #1
    For I = 1 To List1.ListCount
        Text1.Text = Text1.Text & List1.List(I - 1) & vbCrLf
        Print #1, List1.List(I - 1)
    Next
    Close
    End Sub
    
    Private Sub Command2_Click()
    ' Read File and Build List Box with Contents
    Dim MyData As String, ArrItem() As String
    List1.Clear
    Text1.Text = vbNullString
    Open "MyDataFile" For Input As #1
    MyData = Input(LOF(1), #1)
    ArrItem = Split(MyData, vbCrLf)
    For I = 0 To UBound(ArrItem) - 1
        List1.AddItem ArrItem(I)
    Next
    Close
    MsgBox "List Box has " & List1.ListCount & " items."
    End Sub
    
    Private Sub Form_Load()
    ' Build List Box
    For I = 1 To ListSize
        List1.AddItem I
    Next
    Text1.Text = vbNullString
    End Sub
    Doctor Ed

  10. #10

    Thread Starter
    Junior Member
    Join Date
    May 2009
    Location
    Scotland, UK
    Posts
    26

    Re: Print data from a ListBox (?)

    Thanks everybody for their codes. I'm sure i will get it to work now.

    Keep up the good work.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width