![](/img/trans.png)
[英]Trying to add UserControl to a TabPage inside of another TabPage get NullReferenceException
[英]updating textbox.text in a tabpage
我有一個帶有大量文本框的應用程序,基本上是一種要填寫的表格。 標簽控件中有幾個標簽頁。 在保存數據時,我只需要遍歷文本框,獲取它們的名稱和文本,然后將其放入帶有'|'的文本文件中即可。 作為分隔符。 效果很好,我可以在文本文件中看到我所有的文本框和數據。 現在出現了問題。 當我讀回文件(以將保存的數據加載回表單)時,它會遍歷控件名稱,並將文本更改為文件中的內容。 它可以與表單上的文本框配合使用,但是當它到達選項卡頁上的第一個文本框時會失敗。 我該如何解決? 而且,如果有更好的方法來保存數據,我將不知所措。
這是寫入文件的代碼:
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
這是讀取文件並更新文本框的代碼:
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
我希望有足夠的信息。 在此先感謝您的幫助!
我認為行Me.Controls(tbxname).text = strarray(x,1)
不能在選項卡控件中處理您的TextBox,您可能需要找出如何在選項卡頁中解決文本框的問題。
就像是
Me.TabControl1.TabPages(0).Controls(tbxname).text = starray(x,1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.