从单个文本框中读取文本,并在几个文本框中分开显示. (按行读取) [英] read text from single textbox and display it in separated in a few textbox. (read by lines)

查看:105
本文介绍了从单个文本框中读取文本,并在几个文本框中分开显示. (按行读取)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们我是开发人员中的新手.我正在尝试制作一个简单的程序,以便用户在textbox(form1)中输入文本.然后,当他们单击按钮时,它将以新的表单(表单2)显示,其中包含一些文本框.form1中来自文本框的文本被行2分隔为form2中的文本框.例如,textbox(form1)中的第一行文本将显示在textbox1(form2)中,第二行将显示在textbox2(form2)中,依此类推.这是我的代码,但输出错误.


 私有  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 the MultiLine property of TextBox is to be set to True. When the MultiLine property is set to True, the Lines property of TextBox returns an array of lines entered in the TextBox. Using this property each line entered in the TextBox can be accessed and set in the TextBoxes of the Form2 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆