Question

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

Was it helpful?

Solution

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");
}

OTHER TIPS

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top