Inserting at top of richtextbox

守給你的承諾、 提交于 2019-12-08 02:15:23

问题


Whats wrong with this code? Trying to get my text to insert at the beginning of the textbox rather than at the bottom.

private void execute_Click(object sender, EventArgs e){
  startFinshBox.Text = "Start Time: " + printTime()+"";
  startFinshBox.Text.Insert(0,printTime()+": Retrieving Results...\n");
}

But it will not insert the second line into the rtb. I have tried with startFinishBox.SelectionStart = 0 as well, and it made no difference. Am I missing something else?

Thanks, Psy


回答1:


startFinshBox.Text is a string, which is an immutable type in C#. string.Insert() will return the modified string as a result, but it your code you discard it. To make it work, you have to change the code to:

private void execute_Click(object sender, EventArgs e){
  startFinshBox.Text = "Start Time: " + printTime()+"";
  startFinshBox.Text = startFinshBox.Text.Insert(0,printTime()+": Retrieving Results...\n");
}



回答2:


The SelectionStart property on a TextBox will determine where text will be selected or inserted from.

Use this code to insert code at the start of the text box control:

TextBox.SelectionStart = 0;
TextBox.SelectedText = "Start time: " + printTime();


来源:https://stackoverflow.com/questions/1818875/inserting-at-top-of-richtextbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!