(已解决)在VB.net中的RichTextBox中拆分行 [英] (Solved) Split Lines in RichTextBox in VB.net

查看:230
本文介绍了(已解决)在VB.net中的RichTextBox中拆分行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello guys



我想问一下如何在Richtextbox中拆分行。

我有一个作为客户端工作的程序。服务器发送客户数据



ex:



船速:40公里/小时



方向:西北







数据被发送到客户端并在客户表格上显示在Richtexbox上。



我现在要做的是将行拆分为文本框



ex



文本框1显示40 KM / H



文本框2显示西北







有谁可以帮助我? (对不起语法不好)

希望我的问题不会让人困惑。



谢谢



编辑:



这是我试过的代码:

Hello guys

I want to ask how to split lines in Richtextbox.
I have a program that work as a client. The server sent client data

ex:

Ship Speed : 40 KM/H

Direction : North - West

etc

the data is sent to client and displayed on Richtexbox at Client Form.

What i want to do right now is to split lines to textboxes

ex

Text box 1 show 40 KM/H

Text box 2 show North - West

etc

Can anyone help me? (sorry for bad grammar)
Hope my question is not confusing.

Thank you



This is my code i tried:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim tbarry() As String = RichTextBox1.Lines.Clone

        TextBox1.Text = tbarry(0)
        TextBox2.Text = tbarry(1)
        TextBox3.Text = tbarry(2)





当我在新项目中尝试它时,它有效。但是当我试图将它实现到我的主程序时崩溃





it works when i try it in new project. But it crash when i tried to implement it to my main program

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Listener.Pending = True Then
            Message = ""
            Client = Listener.AcceptTcpClient()

            Dim Reader As New StreamReader(Client.GetStream())
            Dim tbarry() As String = RichTextBox1.Lines.Clone
            While Reader.Peek > -1
                Message = Message + Convert.ToChar(Reader.Read()).ToString
            End While
            RichTextBox1.ForeColor = Color.Black
            RichTextBox1.Text += Message + vbCrLf
            RichTextBox1.Text = RichTextBox1.Text.Replace(RichTextBox1.Text, Message + vbCrLf)
            RichTextBox2.ForeColor = Color.Black
            RichTextBox2.Text += Message + vbCrLf
            RichTextBox2.Text = RichTextBox2.Text.Replace(RichTextBox2.Text, Message + vbCrLf)



            TextBox1.Text = tbarry(0)
            TextBox2.Text = tbarry(1)
            TextBox3.Text = tbarry(2)
            
        End If

    End Sub





它说索引超出了数组的范围



it said "Index was outside the bounds of the array"

推荐答案

它只是意味着由于某种原因,RTF文本少于3行。在调试器下运行它,你会看到。永远不要假设数组或某些集合中的元素数量。索引索引访问的任何内容之前,请务必检查元素的数量。



-SA
It simply means that the RTF text, by some reason, has less then 3 lines. Run it under the debugger and you will see. Never assume number of elements in array or some collection. Before indexing of anything which is accessed by index, always check the number of elements.

—SA


这篇关于(已解决)在VB.net中的RichTextBox中拆分行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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