在VB Net中控制文本框行

时间:2019-01-01 23:46:53

标签: vb.net

我想更改我的代码,该代码现在使用多个文本框,但是基本上我只需要一个文本框。为了不占用太多资源,我想限制代码,并且所有操作都在文本框(多行)中完成。

Private Sub Button1_Click(ByVal sender As Object, ByVal e As 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)
        TextBox6.Text = strLines.Length
        TextBox6.Text = Val(TextBox6.Text)
        sr.Close()
        Dim TB As TextBox
        For i As Integer = 1 To Val(TextBox6.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
            Dim notRepeatedCharacter = firstBoxList.Union(secondBoxList).ToList
            notRepeatedCharacter.RemoveAll(Function(x) intersectionList.Contains(x))
            TB = Me.Controls.Find("txtIntNonI" & i, True).FirstOrDefault
            For Each str As String In firstBoxList
                TB.AppendText(str & ",")
            Next
        Next
    Catch ex As Exception
    End Try

因此,此代码:TB = Me.Controls.Find("txtIntDraw" & i, True).FirstOrDefault 将需要成为

Output: txtIntDraw.Lines (i)

和相同 TB = Me.Controls.Find ("txtIntNonI" & i, True) .FirstOrDefault

Output: TxtIntNonI.Lines (i)

但是,如果这样做,我仍然需要更改此代码:

Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
        AllNumbers1.AddRange(CType(Me.Controls("txtIntDraw" & x), TextBox).Text.Split(CChar(",")))
End Sub

,这应该在多行文本框中完成。 (TxtIntDraw.Lines(i))

谢谢。所以我的问题是如何编写此代码,以便可以控制文本框行?

1 个答案:

答案 0 :(得分:1)

使用StringBuilder。谷歌.net类以获取更多详细信息。

'At the top of the file
Imports System.Text

Private Sub FillTextBox()
    Dim sb As New StringBuilder()
    'inside the loop
    sb.AppendLine("what ever")
    'and after the loop
    'This will prevent the textbox from having to repaint on every iteration
    TextBox1.Text = sb.ToString
End Sub

希望这能给您足够的帮助,以便您可以将其集成到代码中。