简体   繁体   English

将多行文本框的行添加到字符串/数组 VB.NET

[英]Add lines of a multiline textbox into a string / array VB.NET

I should replace txtstringnump1 up to 20 textboxes, it creates a list where it is a can write the values until there is still a textbox.我应该将 txtstringnump1 替换为最多 20 个文本框,它会创建一个列表,可以在其中写入值,直到还有一个文本框。 how do i do this?我该怎么做呢?

 Dim AllNumbers1 As New List(Of IEnumerable(Of ArrayList))
        Dim str As String
        'Set your string value
        For i As Integer = 0 To Globals.TBIntDrawsXCount - 1
            str = TxtBoxIntDrawsX.Lines(i)
            'Define  new string array containing elements of str divided by space..
            Dim strWords As String() = str.Split(",")
            Dim myArr As Integer() = strWords(0).ToString
     TxtStringNumP1.Text &= strWords(0) & Environment.NewLine
            TxtStringNumP2.Text &= strWords(1) & Environment.NewLine
            TxtStringNumP3.Text &= strWords(2) & Environment.NewLine
            TxtStringNumP4.Text &= strWords(3) & Environment.NewLine
            TxtStringNumP5.Text &= strWords(4) & Environment.NewLine
            TxtStringNumP6.Text &= strWords(5) & Environment.NewLine
            TxtStringNumP7.Text &= strWords(6) & Environment.NewLine
            TxtStringNumP8.Text &= strWords(7) & Environment.NewLine
            TxtStringNumP9.Text &= strWords(8) & Environment.NewLine
            TxtStringNumP10.Text &= strWords(9) & Environment.NewLine
            TxtStringNumP11.Text &= strWords(10) & Environment.NewLine
            TxtStringNumP12.Text &= strWords(11) & Environment.NewLine
            TxtStringNumP13.Text &= strWords(12) & Environment.NewLine
            TxtStringNumP14.Text &= strWords(13) & Environment.NewLine
            TxtStringNumP15.Text &= strWords(14) & Environment.NewLine
            TxtStringNumP16.Text &= strWords(15) & Environment.NewLine
            TxtStringNumP17.Text &= strWords(16) & Environment.NewLine
            TxtStringNumP18.Text &= strWords(17) & Environment.NewLine
            TxtStringNumP19.Text &= strWords(18) & Environment.NewLine
            TxtStringNumP20.Text &= strWords(19) & Environment.NewLine
        Next

The Lines property of the TextBox IS an array of the lines. TextBoxLines属性是一个行数组。 You should have seen that for yourself when you read the documentation for the TextBox class, which you should have done before posting here.当您阅读TextBox class 的文档时,您应该已经看到了这一点,您应该在此处发布之前完成这些操作。

This code adds lines to succeeding textboxes.此代码将行添加到后续文本框。 When the last textbox is reached, it starts over with the first one:当到达最后一个文本框时,它会从第一个文本框重新开始:

Dim tbIndex As Integer = 0

Dim lines As String() = TxtBoxIntDrawsX.Lines
For i As Integer = 0 To Globals.TBIntDrawsXCount - 1
    Dim str = lines(i)
    Dim strWords As String() = str.Split(","c)
    For Each word As String In strWords
        Dim tb = DirectCast(Controls.Item("TxtStringNumP" & (tbIndex + 1)), TextBox)
        tb.Text &= word & Environment.NewLine
        tbIndex = (tbIndex + 1) Mod 20
    Next
Next

I'm using two tricks:我正在使用两个技巧:

  1. I look up the textbox by name in the Form's Controls collection.我在 Form 的Controls集合中按名称查找文本框。
  2. I use a modulo opertation to limit the index in the range 0.. 19我使用模运算将索引限制在 0.. 19 范围内

I'm not sure if this is what you need.我不确定这是否是您需要的。 If you need something else, please provide a better description.如果您需要其他内容,请提供更好的描述。


Note: updated according to @jmcilhinney's suggestion.注意:根据@jmcilhinney 的建议更新。 The lines array is stored before the loop to avoid creating a new array by the TextBox each time. lines 数组存储在循环之前,以避免每次由 TextBox 创建一个新数组。

This code assumes that the required number of lines is available.此代码假定所需的行数可用。 The line Dim str = lines(i) will generate an index-out-of-bounds exception if the TextBox does not contain enough lines.如果 TextBox 不包含足够的行,则行Dim str = lines(i)将生成索引越界异常。 The code would be more robust with an appropriate check.通过适当的检查,代码会更加健壮。

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

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