Reputation:
I have 5 text boxes marked with NX1, NX2, NX3 .... NX5.
I have a Textbox marked with Textbox2 that contains lines like:
2 4 6 8 11
1 2 3 4 12
3 4 7 9 13
4 5 7 9 14
Is there a possibility to enter the first word / number from TextBox2 Lines (0) in NX1? i.e. First Word (number 2 in NX1), (Second Word) number 4 in NX2, number 6 in NX3 and number 8 in NX4. and so on.
I tried this:
Dim mytext As String
Dim counter As Integer
mytext = TextBox1.Text
For counter = 1 To Len(TextBox1.Text)
If Mid$(mytext, counter, 1) = " " Then
GoTo NextPart
End If
Next counter
NextPart:
MsgBox("'" + Mid$(mytext, 1, counter - 1) + "'")
MsgBox("'" + Mid$(mytext, 2, counter - 1) + "'")
Upvotes: 0
Views: 82
Reputation: 32233
Get the TextBox.Lines(0)
. That's the first line of text. If the text parts are separated by a white space, just Split()
the text (white space is the default separator).
If it's not, specify the separator: Split("[SomeChar]"c)
.
You'll get 5 strings in a String()
array. Assing String(0)
to NX1
etc.
Dim FirstLine As String = TextBox2.Lines(0)
Dim AllParts As String() = FirstLine.Split()
NX1.Text = AllParts(0)
NX2.Text = AllParts(1)
'(...)
Repeat the same procedure if you need the other text lines.
You could also use LINQ
to perform the string assignments:
AllParts.Select(Function(s, i) Me.Controls("NX" & (i + 1)).Text = s).ToList()
Or, assemble everything in one expression:
TextBox2.Lines(0).Split().Select(Function(s, i) Me.Controls("NX" & (i + 1)).Text = s).ToList()
A description of this method:
[TextBox].Lines(0) => Returns the first line of text in a TextBoxBase control
Split() => Splits the text content using the default separator.
Returns an array of String
Select((string, int)) => Selects each string returned by the Split() method and updates
the index of each iteration.
Performs an action: sets a control`s Text property.
ToList() => Materializes the Enumerable collection returned by Select()
If omitted, in this case, the iteration is performed just once
Me.Controls("NX" & (i + 1)).Text = s
:
Returns a Form
's Control which name is "NX"
+ a number and assigns a string to its Text property.
Upvotes: 1
Reputation: 112482
You can use String.Split()
to split the string.
Dim columns = TextBox2.Lines(0).Split()
For i = 0 To columns.Length - 1
Controls.Item("NX" & (i + 1)).Text = columns(i)
Next
The Lines
property of the TextBox
returns a string array containing the lines of the entered text.
String.Split()
with no parameters splits a string at white spaces and returns a string array containing the parts (the columns of the first lines in this case).
The Controls
property of the form returns a controls collection. The indexed Item
property of the controls collection accepts either an Integer
index or a control name as String
.
Upvotes: 0