Reputation: 111
Background:
I am writing a C# form program with MSVS 2010. The form has 2 textboxes: textBox1 (input, single line) and textBox2 (output, multiple lines).
I want to enter a string in textBox1 and when a condition is met it prints some text to textBox2. I want to be able to enter multiple inputs and print output to textBox2 and not erase the previous output.
Questions:
Upvotes: 0
Views: 9588
Reputation: 1753
sure, all can be done:
1) "enter a string in textBox1 and when a condition is met "
this can be done in several ways, if condition is user derived event (such as key press) this can be done using events. if condition is checked separably by program then simple
if(condition){
textBox1.Text += output;
}
will suffice.
2) "I want to be able to enter multiple inputs"
multiple inputs simply means reading more textboxes
3) "print output to textBox2 and not erase the previous output."
this can be done as follows:
// append at end
textBox1.Text += output;
// append at start
textBox1.Text = output + textBox1.Text;
Upvotes: 1
Reputation: 14471
Yes that can be done. You just need to concatenate the existing text in textBox2 with the new text you want to add.
Here is a very simple way to do this:
textBox2.Text = textBox2.Text + Environment.NewLine + "new text"
Upvotes: 1
Reputation: 447
Yes, it can be done. Just append the text from textBox1 to the existing text in textBox2. You can set up some kind of event handler to check if the condition is met.
Upvotes: 1
Reputation: 14794
You probably want to use an ItemsControl
to display each line, then you just add the new line to the List
source.
http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.aspx
Upvotes: 0
Reputation: 65116
All you need to do is keep appending to the text property.
string output = "Hi!";
outputBox.Text += output + Environment.NewLine;
This is, of course, after you've given your textbox a more meaningful name than textBox2
Bonus: You can also prepend to the text if you prever new output to appear at the top:
outputBox.Text = output + Environment.NewLine + outputBox.Text;
Upvotes: 2