I need to insert some text as the first character of my textbox when a button is clicked.
Here is what I have tried:
private void btnInsert_Click(object sender, EventArgs e)
{
txtMainView.Text.Insert(0, "TEST");
}
This fails to insert the text when I click the button. Anyone have an idea what I am doing wrong?
txtMainView.Text = $"TEST{txtMainView.Text}";
You can also go with
txtMainView.Text = "TEST" + txtMainView.Text;
as an alternative.
txtMainView.Text = txtMainView.Text.Insert(0, "TEST");
Strings are immutable in .NET Framework so each operation creates a new instance, obviously does not change original string itself!
For more details on String
class see MSDN page Strings (C# Programming Guide)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With