简体   繁体   中英

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. 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. You should have seen that for yourself when you read the documentation for the TextBox class, which you should have done before posting here.

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.
  2. I use a modulo opertation to limit the index in the range 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. The lines array is stored before the loop to avoid creating a new array by the TextBox each time.

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. The code would be more robust with an appropriate check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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