简体   繁体   English

在标签页中更新textbox.text

[英]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. 我认为行Me.Controls(tbxname).text = strarray(x,1)不能在选项卡控件中处理您的TextBox,您可能需要找出如何在选项卡页中解决文本框的问题。

Something like 就像是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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