dcsimg
Results 1 to 4 of 4

Thread: Save/Load Listbox Porting Code From VB6

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    78

    Save/Load Listbox Porting Code From VB6

    ok I used to try to learn to program in visual basic 6 and I am trying to port some of the old code over to vb .net and having some issues that I can't seem to figure out. First is the SaveListBox Sub, I tried to figure out what was going wrong here but it seemed to have too many issues with it and I am not up to date on how to do many things with vb .net yet, still learning, I need to re-learn the basics again but help me figure out what is happening here with these.

    Code:
    Public Sub SaveListBox(Directory As String, TheList As ListBox)
        Dim SaveList As Long
        On Error Resume Next
        Open Directory$ For Output As #1
        For SaveList& = 0 To TheList.ListCount - 1
            Print #1, TheList.List(SaveList&)
        Next SaveList&
        Close #1
    End Sub
    ok next I am having one problem with this one line of code which is:

    Code:
    OpenFileDialog1.CancelError = True
    not sure what is used in vb .net instead of CancelError = True
    I always thought this was some strange vb6 error because I was confused as to why if cancel was clicked on the common file dialog it would error.. is it just me or was that weird? I would have figured if you cancel it would have just shut the window.. no error.. ok well anyway..

    ok but here is that code section just so you can see how I am trying to use it:

    Code:
    On Error GoTo Err
            OpenFileDialog1.FileName = ""
            OpenFileDialog1.CancelError = True
            OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt"
            OpenFileDialog1.Title = "Load Text File..."
            OpenFileDialog1.ShowDialog()
            Call LoadListbox(OpenFileDialog1.FileName, ListBox1)        
    Err:
        End Sub
    ok having a hard time with this sub also, this code is outdated as well:

    Code:
    Public Sub LoadListbox(Directory As String, TheList As ListBox)
            Dim MyString As String
            On Error Resume Next
            Open Directory$ For Input As #1
            While Not EOF(1)
                Input #1, MyString$
            DoEvents
                TheList.AddItem MyString$
            Wend
            Close #1
        End Sub
    Thanks, I hope to figure out what exactly is going on with these, thanks..

  2. #2
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,436

    Re: Save/Load Listbox Porting Code From VB6

    Hi,

    I'll give you a start with collecting Infos about Files, there are plenty samples out there about saving
    a Listbox to a Textfile, so I will leave that one to you.

    here a sample getting the Files..
    Code:
    Imports System.IO
    
    Public Class Form1
    
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
            Dim mypath As String = "D:\sample Chris"
            Dim ListOfFiles As New List(Of FileInfo)
            CollectInfo(mypath, ListOfFiles)
        End Sub
        Private Sub CollectInfo(ByVal path As String, ByRef FileInfoList As List(Of FileInfo))
            Dim fileinfos As FileInfo()
            Try
                fileinfos = New DirectoryInfo(path).GetFiles("*")
            Catch ex As System.UnauthorizedAccessException
                ListBox1.Items.Add("Cannot List files in " & path)
                Exit Sub
            End Try
            For Each FileInfo1 As FileInfo In fileinfos
                FileInfoList.Add(FileInfo1)
                ListBox2.Items.Add(FileInfo1)
    
            Next
            For Each dir As DirectoryInfo In New DirectoryInfo(path).GetDirectories
                CollectInfo(dir.FullName, FileInfoList)
                ListBox3.Items.Add(dir.FullName)
    
            Next
        End Sub
    
    End Class
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    78

    Re: Save/Load Listbox Porting Code From VB6

    Thanks for the info Chris, I appreciate it but I don't need the file info, although I may need to check that out anyways always useful to know how things are done but what I am doing is just saving a list from a populated listbox control as well as loading a textfile list into the listbox. I checked some examples my eyes are tired but I just basically want to update the code I already have there if possible. I just could not figure out how to fix the code I already have. It works fine in vb6 just not in vb .net..

  4. #4
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,436

    Re: Save/Load Listbox Porting Code From VB6

    Hi,

    don't try to Convert !! start all over again.

    here for saving a Listbox...
    Code:
    Imports System.Text
    
    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
            Dim sav As New SaveFileDialog
            sav.Filter = "Textdatei|*.txt"
            If sav.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim sb As New StringBuilder()
                For Each Item As Object In Me.ListBox2.Items
                    sb.AppendLine(Item.ToString)
                Next
                File.WriteAllText(sav.FileName, sb.ToString)
            End If
        End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width