In modern programming, there is a tendency to separate the data (=model) from how this data is displayed (=view). This enables changing how you display the data without having to change the model. You can also change the model, without having to change the view.
For this separation, we need adapter code that couples the Model to the View: quite often called the ViewModel. The abbreviation for this idea is MVVM. Google this if you want to k know more about the theory behind this.
"I have a .txt with content"
First of all: you form is not interested where your data comes from, hide this in your model.
string GetText() {... // Read from file }
If later you decide that text should be read from a different source, for instance from a database, or from the internet, or maybe you decide that you ask the operator to type the text, the rest of your model and view won't have to change.
Besides: it will be easier to unit test this part.
"inserted in textBox1 and I already show the content to textBox2. I want to move the part else { a = c; } to another textBox3 and delete this part in textBox2"
I think you mean to say, that from the text that you fetched in GetText(), you want to show the "else" part (TODO: define the else part), in textBox3, and show the complete text without the "else" part in textBox2. The complete text should be "inserted" in textBox1.
So we need a method to separate your test into two: the "Else" part and the rest. Furthermore we need procedures to "Insert" the complete text, the "Else" text and the "Rest" text:
void InsertText (string text)
{
// not sure what "Insert" means. Do you mean replace?
// or add to the current cursor position?
this.textBox1.Text = ...
}
void ShowElsePart(string elseText)
{
this.textBox3.Text = elseText;
}
void ShowRest(string restText)
{
this.textBox2.Text = restText;
}
So I've hidden how I view "Else" part, the "Rest" part and what I mean with "Insert text". If later you decide you don't want to show it in a text Box, but for instance in a RichTextBox, or in a Listview, only one procedure needs to change.
We need to divide the text into the "Else" part and the "Rest".
You defined the "Else" part as the part else { a = c; }, so without the ** and more important: without new lines, or was this just the description, and could 'else { a = c; }be different text:else a = c;, or else { x = y; }or evenelse { while (x) {...}`?
This uncertainty is a good example of why you should separate the model from the view: if you decide to change your definition of the "Else" part, outsiders don't have to change.
void DivideText(string input, our string elsePart, out string rest)
{
// TODO implement
}
First we'll put it all together, then we'll do the implementation:
void DisplayInput()
{
string text = this.GetText();
this.InsertText(text);
this.DivideText(text, out string elseText, out string restText);
this.ShowElsePart(elseText);
this.ShowRest(restText);
}
Apart from that it is fairly easy to understand what happens, this code is highly reusable, easy to change and easy to text. If you want to read your text from a database, instead of plain text file, only one procedure needs to change. If you don't want to insert the text in textBox1, but in a file: only one place to change. If you define "Else" part differently: again only place to change.
Now about the "Else" part. According to your definition, "Else" is fairly simple:
const string elsePart = "else { a = c; }"
int elseStartIndex = text.IndexOf(elsePart, 0);
if (elseStartIndex < 0)
{
// elsePart not found
string restText = text;
}
else
{
string restText = text.Remove(elseStartIndex, elsePart.Length;
}
It could be that you meant something else with "Else" part: everything from the first else until the first }.
int indexStartElse = text.IndexOf("else", 0);
// TODO: what if no else?
int indexEndElse = text.IndexOf("}", indexStartElse);
// TODO: what if no }?
int elseTextLength = indexEndElse - indexStartElse;
string elseText = text.SubString(indexStartElse, elseTextLength);
string restText = text.Remove(indexStartElse, elseTextLength);