Clears text only when got focus

Pankaj tripathi 185 Reputation points
2023-11-17T14:20:26.04+00:00
  private static void Txb_GotFocus(object sender, EventArgs e)
        {
            var txb = (TextBox)sender;
            var intfc = (ICustomTextBox)sender;
            if (txb.Parent != null)
            {
                OriginalBackColors[txb] = txb.Parent.BackColor;
                txb.BackColor = intfc.FocusBackColor;
                if (!string.IsNullOrEmpty(txb.Text))
                {
                    txb.Select(0, 0);
                   
                }
            }
        }

in the above event i want to add a check that if the textbox got focus and it has some data , the cursor should be at very first position . at the same time , i also want that in the same case (if the textbox got focus and it has some data ) , when the user starts typing in it , all the subsequent text should be cleared . so for example

if the textbox has data = "pankaj" ,when it gets focus again and the cursor gets at before p . so now when he starts typing for example he types N , then pankaj should gets clears out .

  private static void Txb_TextChanged(object sender, EventArgs e)
        {
            var txb = (TextBox)sender;
            var intfc = (ICustomTextBox)sender;
          
                if (intfc.UseUpper && txb.TextLength > 0)
                {
                    string prevText = txb.Text;
                    string nextText = ToUseUpper(prevText);
                    if (nextText != prevText)
                    {
                        int selectionStart = txb.SelectionStart;
                        int selectionLength = txb.SelectionLength;
                        txb.Text = nextText;
                        txb.Select(selectionStart, selectionLength);
                    }
                }
            
        }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,907 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,097 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 47,501 Reputation points Microsoft Vendor
    2023-11-22T08:09:26.96+00:00

    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();
        }
    }
    

    11-22

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.