tags:

views:

80

answers:

2

Hey guys,

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

+3  A: 

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");
}
Grzenio
Ahh, I knew it would have been something simple like that, thanks!
Psytronic
A: 

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();
Mike J