[英]Word input from one line in the textbox to another
I have 5 text boxes marked with NX1, NX2, NX3 .... NX5. 我有5个文本框,标有NX1,NX2,NX3 .... NX5。
I have a Textbox marked with Textbox2 that contains lines like: 我有一个标有Textbox2的文本框,其中包含如下行:
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? 是否可以从NX1中的TextBox2行(0)输入第一个单词/数字? ie First Word (number 2 in NX1), (Second Word) number 4 in NX2, number 6 in NX3 and number 8 in NX4.
例如,第一个单词(NX1中的数字2),(第二个单词)NX2中的数字4,NX3中的数字6和NX4中的数字8。 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) + "'")
Get the TextBox.Lines(0)
. 获取
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). 如果文本部分由空格分隔,则只需对文本进行
Split()
(空白是默认的分隔符)。
If it's not, specify the separator: Split("[SomeChar]"c)
. 如果不是,请指定分隔符:
Split("[SomeChar]"c)
。
You'll get 5 strings in a String()
array. 您将在
String()
数组中获得5个字符串。 Assing String(0)
to NX1
etc. 将
String(0)
NX1
到NX1
等
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: 您还可以使用
LINQ
来执行字符串分配:
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
: 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. 返回一个
Form
控件,其名称为"NX"
+一个数字,并将字符串分配给它的Text属性。
You can use String.Split()
to split the string. 您可以使用
String.Split()
拆分字符串。
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. TextBox
的Lines
属性返回一个包含输入文本行的字符串数组。
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). 不带参数的
String.Split()
在空白处分割字符串,并返回包含部分(在这种情况下为第一行的列)的字符串数组。
The Controls
property of the form returns a controls collection. 窗体的
Controls
属性返回一个控件集合。 The indexed Item
property of the controls collection accepts either an Integer
index or a control name as String
. 控件集合的索引
Item
属性接受Integer
索引或控件名称作为String
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.