简体   繁体   中英

updating textbox.text in a tabpage

I've got an app with numerous textboxes that's basically a form to fill out. There are several tabpages in a tab control. In saving the data, I simply loop through the textboxes, get their names and text and put that in a text file with a '|' as a seperator. That works great and I can see all my textboxes and the data in the text file. Now comes the problem. When I read the file back (to load saved data back into the form) it loops through the control name and changes the text to whatever was in the file. It works fine with the textboxes that are on the form, but fails when it gets to the first textbox that is on a tabpage. How can I fix that? And if there's a better way to save the data, I'm all ears.

Here's the code that writes the file:

    Private Sub savefile(file As String)
    Dim ctl As Control = Me
    Dim sw As System.IO.StreamWriter
    sw = My.Computer.FileSystem.OpenTextFileWriter(file, False)
    Do
        ctl = Me.GetNextControl(ctl, True)
        If ctl IsNot Nothing Then
            If TypeOf ctl Is TextBox Then
                sw.WriteLine(ctl.Name.ToString.Substring(3) & "|" & ctl.Text)
            End If
        End If
    Loop Until ctl Is Nothing
    sw.Close()
End Sub

Here's the code that reads the file and updates the textboxes:

    Private Sub ReadFile(filename)
    Dim strfilename As String = filename
    Dim num_rows, x, y As Integer
    Dim strarray(1, 1) As String
    Dim ctrl As Control = Me

    'Check if file exist
    If File.Exists(strfilename) Then
        Dim tmpstream As StreamReader = File.OpenText(strfilename)
        Dim strrecords() As String
        Dim strfields() As String

        'Load content of file to strLines array
        strrecords = tmpstream.ReadToEnd().Split(vbCrLf)

        ' Redimension the array.
        num_rows = UBound(strrecords)
        ReDim strarray(num_rows, 2)

        ' Copy the data into the array.
        For x = 0 To num_rows - 1
            strfields = strrecords(x).Split("|")
            For y = 0 To 1
                strarray(x, y) = strfields(y)
            Next
        Next

        ' Display the data in listbox
        For x = 0 To num_rows - 1
            Dim tbxname As String = strarray(x, 0)
            tbxname = Remove(tbxname) 'routine that removes invisible characters
            tbxname = "tbx" & tbxname
            MsgBox(tbxname) 'used to verify each file and which one fails
            Me.Controls(tbxname).Text = strarray(x, 1)
        Next
    Else : MsgBox("File doesn't exist!")
    End If

End Sub

I hope that's enough information. Thanks in advance for any help!!

I think the line Me.Controls(tbxname).text = strarray(x,1) does not address your TextBox in your tab control, you may need to find out how to address your textbox in the tab page.

Something like

Me.TabControl1.TabPages(0).Controls(tbxname).text = starray(x,1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM