0

So, I'm trying to create a terminal of sort in C#. I've chosen a rich text box for the "shell" area and basically every time the user presses enter I add a symbol for the meantime. The problem is, however, that the cursor instead of being ahead of the symbol is on a new line. Here is the simple code:

terminal.AppendText("\n>");

So, basically, if space here is | then what I want is:

> |

but what I get is this:

>
|

I know this could have to do with the broader problem of how to add a new line when enter is pressed. What more is needed here?

Edit: The full code

if(e.KeyCode == Keys.Enter) 
{
   AddLine();
}

where AddLine() is:

private void AddLine() 
{
   terminal.AppendText("\n>");
}

Reminder: terminal is a rich text box in my windows form application. Also, the conditional statement is on the KeyDown() method of the rich text box.

Gaurav Mall
  • 2,372
  • 1
  • 17
  • 33
  • Do you mean something like in the animation [here](https://stackoverflow.com/a/51682585/7444103)? The `Enter Command` control is a RTB. – Jimi Mar 27 '20 at 15:25
  • @Jimi How can that help me? Please explain.... – Gaurav Mall Mar 27 '20 at 15:31
  • `every time the user presses enter` You have to suppress that key press. Show us the full code stub. – LarsTech Mar 27 '20 at 15:33
  • @LarsTech I edited my post. – Gaurav Mall Mar 27 '20 at 15:35
  • Well, you can preserve some space, using the `SelectionIndent` property and add a symbol that cannot be *touched* by a User *intervention*, since it's positioned in the inaccessible area created by the property. – Jimi Mar 27 '20 at 15:36
  • @Jimi I really can't understand what you are saying. Maybe because I never heard of the `SelectionIndent`. Can you post a answer where you explain how to do it? Thanks :) – Gaurav Mall Mar 27 '20 at 15:37
  • Add `e.SuppressKeyPress = true;` above your `terminal.AppendText("\n>");` line. – LarsTech Mar 27 '20 at 15:40
  • @LarsTech Oh it really works. Thanks for the answer! – Gaurav Mall Mar 27 '20 at 15:42

1 Answers1

1

You need to prevent the enter key from processing its action on the Text box. Try adding e.SuppressKeyPress = true; to your code:

private void Terminal_KeyDown(object sender, KeyEventArgs e) {
  if (e.KeyCode == Keys.Enter) {
    e.SuppressKeyPress = true;
    terminal.AppendText("\n>");
  }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225