Clearing a Textbox Leaves an Invisible Character

Clearing a TextBox leaves an invisible character

For some reason, the textbox keeps a carriage return. Adding e.Handled = true to the method which calls Input() solves this issue.

private void tUser_KeyPress(object sender, KeyPressEventArgs e)
if (e.KeyChar == (char)Keys.Enter)
e.Handled = true;

e.Handled prevents the enter message from being processed after this handler has finished (source).

C# Auto Clearing Winform Textbox

Hook into the KeyPress event on the TextBox, and when it encounters the Enter key, run your hardware setting code, and then highlight the full text of the textbox again (see below) - Windows will take care of clearing the text with the next keystroke for you.

TextBox1.Select(0, TextBox1.Text.Length);

Showing a 'Clear Text' button after detecting the first character in an Input Box

Use oninput to detect when user is interacting with the textbox

$('input').on('focus input', function () {
}).blur(function () {

other option is use type="search" for browsers that support it. Some already add the button and handles this.


Removing hidden characters from within strings

You can remove all control characters from your input string with something like this:

string input; // this is your input string
string output = new string(input.Where(c => !char.IsControl(c)).ToArray());

Here is the documentation for the IsControl() method.

Or if you want to keep letters and digits only, you can also use the IsLetter and IsDigit function:

string output = new string(input.Where(c => char.IsLetter(c) || char.IsDigit(c)).ToArray());

How to display characters remaining for Textbox?

You could attach input event to your textarea then every time the user types or remove a character you could perform the (max - current_characters) operation and show the result in a span :

var max = 150;var textarea = document.querySelector('textarea');var info = document.querySelector('#info');
//Init the count for the first timeinfo.textContent = max - textarea.value.length;
textarea.addEventListener('input', function() { info.textContent = max - this.value.length;})
<textarea maxlength="150" rows="5" cols="50" wrap="hard">
In 150 characters or less, tell me something about yourself...
</textarea><br> Remaining <span id="info"></span> characteres

After setting ActiveX textbox to empty value, previous text briefly appears in box before disappearing again

This is common textbox behaviour. Unless the textbox is selected again, the changes aren't actually completed. I can see how this is inconvenient if used by other users and data entered is confidential. After numerous tests, the only working way I found around this is to trigger this flicker after clearing by making VBA select and deselect the input box, and simulating new user input with sendkeys.

Sub textbox1clear()
Dim sel
sel = Selection.Address
Application.Sendkeys(" ")
Sheet1.TextBox1.Value = ""
End Sub

I have built in a selection recall to make it (slightly) more seamless if the sub is called with a button. However if this is omitted and no selection is made after this sub is ran, the text will flicker again when the textbox is deselected.

Also note this should always be called manually, if it is called from a Textbox_LostFocus like I did, it will enter a continuous loop as long as the textbox isn't selected.

It's not a very elegant solution, but if data protection is priority, it will do the trick.

If this is not sufficient, you might want to hide your input altogether if this is an option.

Check Whether a TextBox is empty or not

You should use String.IsNullOrEmpty() to make sure it is neither empty nor null (somehow):

if (String.IsNullOrEmpty(textBox1.Text))
// Do something...

More examples here.

For practical purposes you might also consider using String.IsNullOrWhitespace() since a TextBox expecting whitespace as input probably negates any purpose, except in case of, say, letting the user pick a custom separator for stuff.

Related Topics

Leave a reply