dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] Save and Load Button Question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Resolved [RESOLVED] Save and Load Button Question

    I have a save and load button that works fine but I have 3 issues/questions relating to it.

    1) Right now it only saves player1.text. I want it to save players1 - 8. Is there an easy way to add the other .text in? Or maybe just save the whole form?

    2) When a player is added, the player*.text becomes visible. But on load of the app it defaults to 1 player visible. Is there a way to make the load button make the other boxes visible if they have a player name from the save file?

    3) Is there a way to designate a default folder location on the save?

    Save BTN
    Code:
    Private Sub savebtn_Click(sender As Object, e As EventArgs) Handles savebtn.Click
            Dim SaveFile As New SaveFileDialog()
    
            SaveFile.Filter = "Text Files (*.txt)|*.txt"
            SaveFile.Title = "Save"
    
            If SaveFile.ShowDialog() = DialogResult.OK Then
                Dim Write As New System.IO.StreamWriter(SaveFile.FileName)
                For Each s As Object In player1.Text
                    Write.Write(s)
                    Write.WriteLine()
                Next s
                Write.Dispose()
                Write.Close()
                MsgBox("Campaign Saved")
            End If
        End Sub
    Load BTN

    Code:
      Private Sub loadbtn_Click(sender As Object, e As EventArgs) Handles loadbtn.Click
            Dim OpenFile As New OpenFileDialog
            OpenFile.FileName = ""
            OpenFile.Filter = "Text Files (*.txt)|*.txt"
            OpenFile.Title = "Open"
            OpenFile.ShowDialog()
            Try
                Dim Read As New System.IO.StreamReader(OpenFile.FileName)
                player1.Text = Read.ReadToEnd
                Read.Close()
            Catch ex As Exception
    
            End Try
        End Sub

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,544

    Re: Save and Load Button Question

    For your first question, yes there is a simpler way. Store your controls in a collection, then iterate through the collection:
    Code:
    Dim players() As Control = {player1, player2, player3, player4, player5, player6, player7, player8}
    
    For Each player As Control In players
        '...
    Next
    For your second question... I don't understand what you mean.

    For your third question, yes you can set a default location. Set the InitialDirectory to your desired folder and FileName to the desired default file name prior to showing the Dialog.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,301

    Re: Save and Load Button Question

    For your second question, set the visibility of each textbox if it has text, eg:
    Code:
    If player2.Text <> "" Then
      player2.Visible = True
    End If
    You can combine this with the collection idea above.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Save and Load Button Question

    Thanks for the Tips. Got it working. Posting code in case anyone wants to know.


    Save Button
    Code:
    Private Sub savebtn_Click(sender As Object, e As EventArgs) Handles savebtn.Click
            'Saves a text file to campaign folder
            Dim SaveFile As New SaveFileDialog()
    
            SaveFile.Filter = "Text Files (*.txt)|*.txt"
            SaveFile.Title = "Save"
    
            If SaveFile.ShowDialog() = DialogResult.OK Then
                Dim Write As New System.IO.StreamWriter(SaveFile.FileName)
                Dim players() As Control = {player1, player2, player3, player4, player5, player6, player7, player8}
    
                For Each player As Control In players
                    Write.Write(player.Text)
                    Write.WriteLine()
                Next
                Write.Dispose()
                Write.Close()
                MsgBox("Campaign Saved")
    
            End If
        End Sub
    Load Button

    Code:
    Private Sub loadbtn_Click(sender As Object, e As EventArgs) Handles loadbtn.Click
            'loads a saved file to form
            Dim players() As Control = {player1, player2, player3, player4, player5, player6, player7, player8}
            Dim OpenFile As New OpenFileDialog
            OpenFile.FileName = ""
            OpenFile.Filter = "Text Files (*.txt)|*.txt"
            OpenFile.Title = "Open"
            OpenFile.ShowDialog()
            Try
                Dim Read As New System.IO.StreamReader(OpenFile.FileName)
                Dim readData As String
                Dim element As Int32 = 0
                While (Read.EndOfStream = False)
                    readData = Read.ReadLine
                    players(element).Text = readData
                    element += 1
                End While
                'Sets Player fiels Visible or not based on load
                If player2.Text <> "" Then
                    player2.Visible = True
                Else
                    player2.Visible = False
                End If
                If player3.Text <> "" Then
                    player3.Visible = True
                Else
                    player3.Visible = False
                End If
                If player4.Text <> "" Then
                    player4.Visible = True
                Else
                    player4.Visible = False
                End If
                If player5.Text <> "" Then
                    player5.Visible = True
                Else
                    player5.Visible = False
                End If
                If player6.Text <> "" Then
                    player6.Visible = True
                Else
                    player6.Visible = False
                End If
                If player7.Text <> "" Then
                    player7.Visible = True
                Else
                    player7.Visible = False
                End If
                If player8.Text <> "" Then
                    player8.Visible = True
                Else
                    player8.Visible = False
                End If
                Read.Close()
            Catch ex As Exception
            End Try
        End Sub

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,301

    Re: [RESOLVED] Save and Load Button Question

    That looks good


    Note that you can simplify this code:
    Code:
                If player2.Text <> "" Then
                    player2.Visible = True
                Else
                    player2.Visible = False
                End If
    As .Visible takes a Boolean value, and the condition of an If statement is a Boolean value, you can do it like this:
    Code:
                player2.Visible = (player2.Text <> "")
    Not only is this shorter, but it is marginally quicker too.

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