我创建了一些文本框,我想将这些文本框的所有元素转移到单个文本框中。
假设我们有一个包含以下元素的文本框:
txtIntDraw1.Text = "1,2,7,8"
txtIntDraw2.Text = "1,3,10,11"
txtIntDraw3.Text = "1,4,10,11"
我的文本框的名称为TextBox5.Text
。
我想要输出:
Textbox5.Lines(0) = "1,2,7,8"
Textbox5.Lines(1) = "1,3,10,11"
Textbox5.Lines(2) = "1,4,10,11"
问题是,我的文本框是动态创建的:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim newbox As TextBox
For i As Integer = 1 To Val(TextBox4.Text) - 2
newbox = New TextBox
newbox.Size = New Drawing.Size(100, 20)
newbox.Location = New Point(10, 10 + 25 * (i - 1))
newbox.Name = "txtIntDraw" & i
newbox.Text = newbox.Name 'Connect it to a handler, save a reference to the array & add it to the form control.
TabControl2.TabPages(3).Controls.Add(newbox)
newbox.Clear()
Next
End Sub
这就是所有代码:
Private Sub Button1_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Mydpi.Text = My.Application.Info.DirectoryPath + ("\itemInfo.txt")
txtIntDraws.Text = System.IO.File.ReadAllText(Mydpi.Text)
Dim sr As New IO.StreamReader(Mydpi.Text)
Dim strLines() As String = Strings.Split(sr.ReadToEnd, Environment.NewLine)
TextBox4.Text = strLines.Length
TextBox4.Text = Val(TextBox4.Text)
sr.Close()
Button4.PerformClick()
Button5.PerformClick()
Dim TB As TextBox
For i As Integer = 1 To Val(TextBox4.Text)
Dim firstBoxList = txtIntDraws.Lines(i).Split(",").ToArray
Dim secondBoxList = txtIntDraws.Lines(i + 1).Split(",").ToList()
Dim intersectionList = firstBoxList.Intersect(secondBoxList)
TB = Me.Controls.Find("txtIntDraw" & i, True).FirstOrDefault
For Each str As String In intersectionList
TB.AppendText(str & ",")
Next
TB = Me.Controls.Find("txtIntNonI" & i, True).FirstOrDefault
Dim notRepeatedCharacter = firstBoxList.Union(secondBoxList).ToList
notRepeatedCharacter.RemoveAll(Function(x) firstBoxList.Contains(x))
notRepeatedCharacter.RemoveAll(Function(x) intersectionList.Contains(x))
For Each str As String In notRepeatedCharacter
TB.AppendText(str & ",")
Next
Next
Catch ex As Exception
End Try
Dim TB1 As TextBox
For i1 As Integer = 1 To Val(TextBox4.Text)
TB1 = Me.Controls.Find("txtIntDraw" & i1, True).FirstOrDefault
TextBox5.Text &= TB1.Text & Environment.NewLine
Next
End Sub
我尝试什么:
Dim TB1 As TextBox
For i1 As Integer = 1 To Val(TextBox4.Text)
TB1 = Me.Controls.Find("txtIntDraw" & i1, True).FirstOrDefault
TextBox5.Text &= TB1.Text & Environment.NewLine
Next
答案 0 :(得分:0)
请,请启用“严格选项”。工具->选项->项目和解决方案-> VB默认值 在线评论和说明
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strLines() As String = File.ReadAllLines(System.IO.File.ReadAllText(My.Application.Info.DirectoryPath + ("\itemInfo.txt")))
Dim ArrayLength As Integer = strLines.Length
CreateTextBoxes(ArrayLength)
'Use a StringBuilder to build the contents of textboxes
Dim sb As New StringBuilder()
Dim TB As TextBox
'Don't you want to read the first line?
'If you try to read to Lines(ArrayLength) it will be out of bounds
'because this a zero based array
For i As Integer = 0 To ArrayLength - 1
'This overload of .Split requires a Char argument, thus the c
'.Split returns an array so .ToArray is redundant
Dim firstBoxList = strLines(i).Split(","c)
Dim secondBoxList = strLines(i + 1).Split(","c)
Dim intersectionList = firstBoxList.Intersect(secondBoxList)
'Option Strict requires the cast
TB = DirectCast(Me.Controls.Find("txtIntDraw" & (i + 1).ToString, True).FirstOrDefault, TextBox)
For Each str As String In intersectionList
sb.Append(str & ",")
Next
TB.Text = sb.ToString
sb.Clear()
TB = DirectCast(Me.Controls.Find("txtIntNonI" & (i + 1).ToString, True).FirstOrDefault, TextBox)
Dim notRepeatedCharacter = firstBoxList.Union(secondBoxList).ToList
notRepeatedCharacter.RemoveAll(Function(x) firstBoxList.Contains(x))
notRepeatedCharacter.RemoveAll(Function(x) intersectionList.Contains(x))
For Each str As String In notRepeatedCharacter
sb.Append(str & ",")
Next
TB.Text = sb.ToString
sb.Clear()
Next
Dim TB1 As TextBox
'The contents of each txtIntDraw is added to TextBox5
For i As Integer = 0 To ArrayLength - 1
TB1 = DirectCast(Me.Controls.Find("txtIntDraw" & (i + 1).ToString, True).FirstOrDefault, TextBox)
sb.AppendLine(TB1.Text)
Next
TextBox5.Text = sb.ToString
End Sub
Private Sub CreateTextBoxes(LinesInFile As Integer)
For i As Integer = 1 To LinesInFile
Dim newbox As New TextBox
newbox.Size = New Drawing.Size(100, 20)
newbox.Location = New Point(10, 10 + 25 * (i - 1))
newbox.Name = "txtIntDraw" & i
'The following line does not connect a handler or save a reference to an array
'It only sets the Text property which you clear 2 lines down
'newbox.Text = newbox.Name 'Connect it to a handler, save a reference to the array & add it to the form control.
TabControl2.TabPages(3).Controls.Add(newbox)
'newbox.Clear()
Next
For i As Integer = 1 To LinesInFile
Dim newBox As New TextBox
'...set size and location
newBox.Name = "txtIntNonI" & i
'Add to where ever
Next
End Sub
此代码未经测试,因此可能需要一些调整。 编辑
删除了Try ... Catch ... End按照@Andrew Morton的评论尝试