Results 1 to 7 of 7

Thread: Export a List Box

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2003
    Location
    London
    Posts
    7

    Export a List Box

    I have some code that exoprts the contents of a List box to a txt fle,

    It does about 882 lines then stops writing to the file, even though the code still loops through the rest of the list box which has 902 items in it.

    Thanks in Advnace.

    Paul.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: Export a List Box

    Originally posted by paulho
    I have some code that exoprts the contents of a List box to a txt fle,

    It does about 882 lines then stops writing to the file, even though the code still loops through the rest of the list box which has 902 items in it.

    Thanks in Advnace.

    Paul.
    Hello, welcome to the forums this looks like your first post. There is no question in your post and there is not enough information to answer the implied question of what is wrong with your code. When asking questions you should try and be specific, post code, and explain exactly what the problem is providing either what should happen but doesn't or any error messages you receive. So if you want to post the code you are using to export the listbox then we'll take a look and figure out why it stops writing to the file.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2003
    Location
    London
    Posts
    7
    Here is the code:

    If MsgBox(Prompt:="Would you like to save results to txt file ?", _
    Buttons:=MsgBoxStyle.YesNo + vbQuestion, _
    Title:="Save Results") = MsgBoxResult.Yes Then
    lstComputers.SelectedIndex = 0
    Dim oFile As System.IO.File
    Dim oWrite As System.IO.StreamWriter
    Dim i As Integer
    oWrite = oFile.CreateText(Application.StartupPath & "Export.txt")
    For i = 1 To lstComputers.Items.Count - 1
    oWrite.WriteLine(lstComputers.Text)
    'If lstComputers.SelectedIndex <> lstComputers.Items.Count - 1 Then
    lstComputers.SelectedIndex = lstComputers.SelectedIndex + 1
    'End If
    Next
    lstComputers.SelectedIndex = 0
    End If

    This VB.Net is all new to me. I've only been using it for 3 days.

    Paul.

  4. #4
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Re: Export a List Box

    Originally posted by Edneeis
    Hello, welcome to the forums this looks like your first post. There is no question in your post and there is not enough information to answer the implied question of what is wrong with your code. When asking questions you should try and be specific, post code, and explain exactly what the problem is providing either what should happen but doesn't or any error messages you receive. So if you want to post the code you are using to export the listbox then we'll take a look and figure out why it stops writing to the file.
    damn it, why do you have to be all nice?

  5. #5
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by paulho
    Here is the code:
    VB Code:
    1. If MsgBox(Prompt:="Would you like to save results to txt file ?", _
    2.                     Buttons:=MsgBoxStyle.YesNo + vbQuestion, _
    3.                     Title:="Save Results") = MsgBoxResult.Yes Then
    4.             lstComputers.SelectedIndex = 0
    5.             Dim oFile As System.IO.File
    6.             Dim oWrite As System.IO.StreamWriter
    7.             Dim i As Integer
    8.             oWrite = oFile.CreateText(Application.StartupPath & "Export.txt")
    9.             For i = 1 To lstComputers.Items.Count - 1
    10.                 oWrite.WriteLine(lstComputers.Text)
    11.                 'If lstComputers.SelectedIndex <> lstComputers.Items.Count - 1 Then
    12.                 lstComputers.SelectedIndex = lstComputers.SelectedIndex + 1
    13.                 'End If
    14.             Next
    15.             lstComputers.SelectedIndex = 0
    16.         End If

    This VB.Net is all new to me. I've only been using it for 3 days.

    Paul.
    [vbcode][/vbcode]

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: Re: Re: Export a List Box

    Originally posted by kasracer
    damn it, why do you have to be all nice?
    His post count was 1. You have to make them a regular customer before the abuse starts.

    There isn't a need to preface any parameters with the parameter name (i.e. Prompt:=) unless you want to. That is more of a VBA type syntax. Also there isn't a need to select an item in the listbox you can loop through all of its items via the Items property which is a collection of those items. Here is an example to learn from:
    VB Code:
    1. If MsgBox("Would you like to save results to txt file ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Save Results") = MsgBoxResult.Yes Then
    2.             'create file
    3.             Dim filepath As String = IO.Path.Combine(Application.StartupPath, "Export.txt")
    4.             Dim sw As New IO.StringWriter(New IO.FileStream(filepath, IO.FileMode.CreateNew))
    5.             'loop through the items
    6.             'there isn't a need to select them though
    7.             Dim item As String 'assuming you have strings in the listbox
    8.             For Each item In lstComputers.Items
    9.                 'write the item to a line in the file
    10.                 sw.WriteLine(item)
    11.             Next
    12.             'close file
    13.             sw.Close()
    14.         End If

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2003
    Location
    London
    Posts
    7
    Thanks for that. Its working now. And also thanks for correcting the post i'll know for next time.

    Paul.

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