-1

I have a textbox where I am able to find and replace things with a click on the button, but it does not change the textbox itself. Below is the code I'm using to change some stuff to my need:

private void button2_Click_1(object sender, EventArgs e)
{
    var abc = textBox1.Text.Between("abc", "&");
    var def = textBox1.Text.Between("def", "&");
    var ghi = textBox1.Text.Between("ghi", "&");
    var jkl = textBox1.Text.Between("jkl", "&");
    var test = abc.Replace("" + abc + "", "any");
    var test1 = def.Replace("" + def + "", "any");
    var test2 = ghi.Replace("" + ghi + "", "any");
    var test3 = jkl.Replace("" + jkl + "", "any");
}

now I'm using that with a button but when I fire up the button it does not change anything in my textbox.

I've been googling around offcourse and found this.textBox1.Refresh(); but this also doens't change anything.

All help would be great, thanks in advance..

user1796805
  • 61
  • 1
  • 12
  • Strings are immutable. So if you want textboxes to show changed/replaced values, you have to assign those changed values back to `Textbox.Text`. – Michael Dec 27 '15 at 17:25
  • ok I understand but it is a Textbox with multiple lines and characters, when I do what you said it only printsout textBox1.Text = test; – user1796805 Dec 27 '15 at 17:33
  • Well you need to concatenate all results into one string and assign that one to `TextBox.Text`. Something along those lines `TextBox1.Text = string.Concat(new[] {test, test1, test2, test3});` – Michael Dec 27 '15 at 17:39

1 Answers1

0

You have to assigned the changed string back to your textBox1. Something like this.

Edit: assuming what you mean by in between is this

private string findBetweenMethod(string text, string start, string end){
    string betweenText = text;
    .... //process
    return betweenText;
}

private void button2_Click_1(object sender, EventArgs e)
{
    var abc = findBetweenMethod(textBox1.Text,"abc", "&"); //find what to change
    textBox1.Text = textBox1.Text.Replace(abc, "any"); //change it, assign it back to the textBox1
    var def = findBetweenMethod(textBox1.Text,"def", "&"); 
    textBox1.Text = textBox1.Text.Replace(def, "any");
    var ghi = findBetweenMethod(textBox1.Text,"ghi", "&"); 
    textBox1.Text = textBox1.Text.Replace(ghi, "any");
    var jkl = findBetweenMethod(textBox1.Text,"jkl", "&"); 
    textBox1.Text = textBox1.Text.Replace(jkl, "any");
}

Note: beware of your text pattern though. The above method is assuming clean, singular, pattern. That is, what you really want is in between "abc" and "&" (and so on) and there is no such pattern repeated elsewhere besides the intended one.

But the basic idea here is to assigned back the changed string to the textBox1, as explained by @sharpstudent and others

Community
  • 1
  • 1
Ian
  • 30,182
  • 19
  • 69
  • 107