从单个文本框中读取文本,并在几个文本框中分开显示. (按行读取) [英] read text from single textbox and display it in separated in a few textbox. (read by lines)
本文介绍了从单个文本框中读取文本,并在几个文本框中分开显示. (按行读取)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
私有 子 Button1_Click( ByVal 发件人 As 系统.对象, ByVal e As System.EventArgs)句柄 Button1.Click
Dim form2 As 新 Form2
form2.Show()
Dim 文本阅读器 As New System.IO. StreamReader(TextBox1.Text)
Dim str1,str2,str3 As 字符串
str1 = textreader.ReadLine()
str2 = textreader.ReadLine()
str3 = textreader.ReadLine()
' reader.Close()
form2.TextBox1.Text = str1
form2.TextBox2.Text = str2
form2.TextBox3.Text = str3
结束 子
解决方案
要在多行上输入文本,必须将TextBox
的MultiLine
属性设置为True
.当MultiLine
属性设置为True
时,TextBox
的Lines
属性返回在TextBox
中输入的array of lines
.使用此属性,可以访问TextBox
中输入的每一行,并在Form2
的TextBoxes
中进行设置,如下所示
' TextBox1.MultiLine = True 私有 子 Button1_Click( ByVal 发件人 As 系统.对象, ByVal e As System.EventArgs)句柄 Button1.Click Dim form2 As 新 Form2 form2.TextBox1.Text = IF (TextBox1.Lines.Length> = 1 ,TextBox1.Lines( 0 )," ) form2.TextBox2.Text = IF (TextBox1.Lines.Length> = 2 ,TextBox1.Lines( 1 )," ) form2.TextBox3.Text = IF (TextBox1.Lines.Length> = 3 ,TextBox1.Lines( 2 )," ) form2.ShowDialog() 结束 子
听起来像多行文本框以及要分解的每一行文字.现在,文本框的宽度无法确定行,因为后面的代码永远不会有任何想法.假设每行之后都有一个换行符(输入),则可以将其用作分隔每行的信号/标记.
在此处查看如何基于换行符进行拆分: VB.NET在新版本上进行拆分行 [ ^ ]
文本文件可以包含3行以上...
例子:
http://msdn.microsoft.com/en-us/library/ezwyzy7b.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/94223t4d [ ^ ]
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=132 [ ^ ]
http://www.csharphelp.com/2005/12/simple-text -file-operations-in-c/ [Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim form2 As New Form2 form2.Show() Dim textreader As New System.IO.StreamReader(TextBox1.Text) Dim str1, str2, str3 As String str1 = textreader.ReadLine() str2 = textreader.ReadLine() str3 = textreader.ReadLine() 'reader.Close() form2.TextBox1.Text = str1 form2.TextBox2.Text = str2 form2.TextBox3.Text = str3 End Sub解决方案For entering the text on multiple lines theMultiLine
property ofTextBox
is to be set toTrue
. When theMultiLine
property is set toTrue
, theLines
property ofTextBox
returns anarray of lines
entered in theTextBox
. Using this property each line entered in theTextBox
can be accessed and set in theTextBoxes
of theForm2
as shown below
'TextBox1.MultiLine = True Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim form2 As New Form2 form2.TextBox1.Text = IF(TextBox1.Lines.Length >= 1,TextBox1.Lines(0),"") form2.TextBox2.Text = IF(TextBox1.Lines.Length >= 2,TextBox1.Lines(1),"") form2.TextBox3.Text = IF(TextBox1.Lines.Length >= 3,TextBox1.Lines(2),"") form2.ShowDialog() End Sub
Sounds like a multiline textbox used and each line of text to be broken down. Now, width of the textbox cannot determine a line as in code behind you will never have any idea about it. Assuming there would be a newline (enter) after every line, you can use that as a signal/flag to separate each line.
Look here on how to split based on newline: VB.NET split on new lines[^]
Text files can contain more than 3 lines...
Examples:
http://msdn.microsoft.com/en-us/library/ezwyzy7b.aspx[^]
http://msdn.microsoft.com/en-us/library/94223t4d[^]
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=132[^]
http://www.csharphelp.com/2005/12/simple-text-file-operations-in-c/[^]
这篇关于从单个文本框中读取文本,并在几个文本框中分开显示. (按行读取)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文