tags:

views:

217

answers:

4

Hi:

I am working on a project that I use textbox as telnet terminal. The terminal has "->" as the command prompt in the textbox. Is there a way to disable the delete or backspace once it reach the "->" prompt?

I don't want to delete the command prompt.

Thanks

+2  A: 

Two options:

  1. Make the prompt ("->") an image or label, instead of being part of the textbox.
  2. If it's a web app, handle the textchanged event in javascript and cancel the textchanged if it represents a deletion of the prompt. If its not a web app, do the same thing in c# rather than JS.
Yoenhofen
+4  A: 

Dave is right.

The best way to do this is to make a label on the left side of the textbox that says ->. You can remove the textbox's border and put them both in a white (or non-white) box to make it look real.

This will be much easier for you to develop and maintain, and will also be more user-friendly. (For example, the Home key will behave better)

SLaks
+1 - Apart from the home key, also consider mouse interaction, such as selecting all text and then pasting in something from the clipboard and other such actions.
Fredrik Mörk
Why is right side? Isn't the prompt always show up at the beginning of a line which is on the left side?We also need to update the position of the Label as more data coming in from telnet socket?
I meant the left side; sorry. Do you have a single multi-line textbox that includes both the prompt and the incoming data?
SLaks
I have multiple line.
A: 

You could always make sure that when deleting, the index of the character you're deleting is > 1 (since -> would occupy positions 0 & 1)

taylonr
this will have problems if someone adds characters before the prompt
SoloBold
True, I guess I was assuming that no characters would be allowed there. The best choice is to make a label.
taylonr
A: 

This is a naive example, but you should be able to figure it out from here. You can peak at the keydown event and cancel it, when desired.

private void testTextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Back && testTextBox.SelectionStart == 2)
    {
        e.SuppressKeyPress = true;
    }
}
Scott P
There are so many corner cases to take care of here that I hardly think it's worth the effort. Some examples: CTRL+A followed by DEL, right-click -> Select All, then right-click -> Delete, the cut/paste commands, and so on. The list can be made quite long...
Fredrik Mörk