Backspace at TextBox.AppendText

  • Thread starter Thread starter Tamir Khason
  • Start date Start date
T

Tamir Khason

I have textbox. I do something in each Keypress (kindof validation) in order
not to rewrite all text inside I'm using AppendText method to write in. BUT
I'm unable to add backspace (\b); e.g I have to change each ZZ to Z so I
want to do myTextBox.AppendText("\bZ"); or (char)8+"Z" - Both writes black
square and Z. I know bout the issue of Win32, but is any way to overrde it?

TNX
 
Tamir,

A backspace character is still a character. Rather, when you detect the
second "Z", ignore it, don't append it to the textbox. This way, you don't
place the extra character, and you get the desired effect (one Z).

Hope this helps.
 
I'd happy to be simple like that ;) ZZ it only example. It can be "Sd" as
well ;)
I found workaronds:
1) SendKey.Send("{BS}"); //BAD GUY
2)
myTextbox.SelectionStart = myTextbox.TextLength-1;

myTextbox.SelectionLength = 1;

myTextbox.SelectedText="";

BOTH not quite like them.

Any other ideas?


--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "


Nicholas Paldino said:
Tamir,

A backspace character is still a character. Rather, when you detect
the second "Z", ignore it, don't append it to the textbox. This way, you
don't place the extra character, and you get the desired effect (one Z).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tamir Khason said:
I have textbox. I do something in each Keypress (kindof validation) in
order not to rewrite all text inside I'm using AppendText method to write
in. BUT I'm unable to add backspace (\b); e.g I have to change each ZZ to
Z so I want to do myTextBox.AppendText("\bZ"); or (char)8+"Z" - Both
writes black square and Z. I know bout the issue of Win32, but is any way
to overrde it?

TNX
 
Hi Tamir,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to replace all the ZZ with Z
in a TextBox when a key is pressed. If there is any misunderstanding,
please feel free to let me know.

Besides ignoring the second Z character, I think we can also use the
String.Replace method. When each key is pressed, we can handle the KeyUp
event to check if there is ZZ in the string. If yes, replace them with Z.
Here is a code snippet.

private void textBox1_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
string s = this.textBox1.Text.Replace("zz","z");
this.textBox1.Text = s;
}

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Thank you for response.
Two problems:
1) As I mentioned earlier - it not have to be double symbol - it can be
anything
2) It possible more then one ZZ (even legal) in the text
 
Hi Tamir,

1) In this case, you have to write several Replace in the event handler.
2) Replace method will replace all the ZZ in the text.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi Tamir,

I too am having a similar problem. It seems the code that follows does the
job:

myTextbox.SelectionStart = myTextbox.TextLength-1;

myTextbox.SelectionLength = 1;

myTextbox.SelectedText="";

But when I keep hitting the backspace button, to the begining, where there
are no more characters left, an error gets thrown. Any further solutions you
could throw my way?

MikeY
 
* Problem of Z *
If only Z has to be replaced, best could be to do a Index search for the
text in textbox.
Condition 1- Text is typed in textbox
....this could be handled in Key event (or text change) and look for Z in
textbox. If Z is already there clear the last type.

Condition 2 - Copy and paste in textbox
Catch text change event and index search for Z, if it is there,
String.Replace it with single Z.

* Problem of Backspace *
Give attn to the condition when there is no text (Length = 0). You are
subtracting 1 in that condition. Check if (TextBox.TextLength > 0) then you
do math on it.

Thanks.
 
Back
Top