简体   繁体   中英

Multiple outputs to a textbox in C#

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:

  1. My question is can this be done?
  2. If not can you use multiple text boxes and move the output to the next higher value text box?

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;

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.

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"

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM