如何更新richtextbox的现有文本而不覆盖现有文本C# [英] How to I update the existing text of a richtextbox without overwriting the existing text C#

查看:399
本文介绍了如何更新richtextbox的现有文本而不覆盖现有文本C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在Visual Studio 2015中设计和构建一个Windows窗体应用程序,该应用程序结合使用richtextbox来跟踪用户状态,就像已经使用/脱离的功能一样,只是一般的应用程序,但是当我尝试通过以下方式以编程方式将文本添加到文本框:



示例:

I am currently designing and constructing a windows forms application in visual studio 2015 that incorporates the use of a richtextbox to track the users status as in what functions have been engaged/disengaged, just general applications like that however when ever i try to add text to the textbox programmatically via :

Example:

richTextBox1.Text = Environment.NewLine + "";



由于我的应用程序的性质,我要求在整个应用程序中从多个来源(按钮)更新文本而不删除现有内容

按钮1:


due to the nature of my application purpose i require the text to be updated from multiple sources (buttons) throughout the application without removing existing content
Button 1 :

<pre lang="c#"> 
IP = flatTextBox1.Text;
richTextBox1.Text = "Action";



按钮2:


Button 2:

<pre lang="c#">
richTextBox1.Text = Environment.NewLine + "Action";



按钮3:


Button 3:

<pre lang="c#">
richTextBox1.Text = Environment.NewLine + "Action";



任何帮助将不胜感激



我的尝试:



按钮1:


any help will be appreciated

What I have tried:

Button 1 :

richTextBox1.Text = Environment.NewLine + "yo";





按钮2:



Button 2 :

flatTexrichTextBox1tBox2.Text = Environment.NewLine + "Your Target IP Has Been Set To : " + IP;





但它会一直覆盖它



but it keeps overwriting it

推荐答案

它是覆盖它,因为你告诉它!

最简单的方法是使用
It's overwriting it because you are telling it to!
The simplest way is to use
richTextBox1.Text += Environment.NewLine + "Action";

但是这不是一个好的解决方案 - 每次都会创建一个新字符串,因为字符串是不可变的(无法更改)。

更好的解决方案是使用StringBuilder,例如

but that is not a good solution - a new string will be created each time because strings are immutable (cannot be changed).
A better solution would be to use a StringBuilder e.g.

StringBuilder sb = new StringBuilder(richTextBox1.Text);
sb.Append(Environment.NewLine);
sb.Append("Action");
richTextBox1.Text = sb.ToString();


这篇关于如何更新richtextbox的现有文本而不覆盖现有文本C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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