Hi @Pankaj tripathi , Welcome to Microsoft Q&A,
I tested using tab to get focus, but you may need to change it if you want to use the mouse to enter focus.
You have too much code, I looked at the requirements in your question.
Wrote the following example using enter and keypress events:
private void textBox1_Enter(object sender, EventArgs e)
{
var txb = (System.Windows.Forms.TextBox)sender;
if (!string.IsNullOrEmpty(txb.Text))
{
txb.Select(0, 0);
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
var txb = (TextBox)sender;
int cursorPosition = txb.SelectionStart;
// If the cursorPosition is 0 and the pressed key is a letter, number, symbol key, +-*/ or space bar, clear the content in the TextBox and then enter
if (cursorPosition == 0 && (char.IsLetterOrDigit(e.KeyChar) || char.IsSymbol(e.KeyChar) || e.KeyChar == '+' || e.KeyChar == '-' || e.KeyChar == '*' || e.KeyChar == '/' || e.KeyChar == ' '))
{
txb.Clear();
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.