Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear field before entering input while using TextChanged?

Tags:

c#

winforms

So what I am trying to do is to be able to continuously type into all of 10 text boxes. I start from the first one, then after 3 characters were typed change focus to next and after 3 characters were typed focus on the next.

I have done that part, but what if there is text already in second text box? I have tries Clear() but it creates a bug for which won't let me type more than 1 character.

So when all text boxes are filled with junk data, so when I start typing at first text box 123 then moves to second text box 456 third 789 ect. but while next field is cleared first.

I am trying to work with this:

    public PingIPRange()
    {
        InitializeComponent();

        txtF1.TextChanged += new EventHandler(NextField);
        txtF2.TextChanged += new EventHandler(NextField);
        txtF3.TextChanged += new EventHandler(NextField);
        txtF4.TextChanged += new EventHandler(NextField);

        txtT1.TextChanged += new EventHandler(NextField);
        txtT2.TextChanged += new EventHandler(NextField);
        txtT3.TextChanged += new EventHandler(NextField);
        txtT4.TextChanged += new EventHandler(NextField);

        txtInterval.TextChanged += new EventHandler(NextField);
        txtRepeat.TextChanged += new EventHandler(NextField);
    }

    private void NextField(object sender, EventArgs e)
    {
        if (txtF1.TextLength == 3)
        {
            txtF2.Focus();              
        }
        if (txtF2.TextLength == 3)
        {
            txtF3.Focus();
        }
        if (txtF3.TextLength == 3)
        {
            txtF4.Focus();
        }
        if (txtF4.TextLength == 3)
        {
            txtT1.Focus();
        }
        if (txtT1.TextLength == 3)
        {
            txtT2.Focus();
        }
        if (txtT2.TextLength == 3)
        {
            txtT3.Focus();
        }
        if (txtT3.TextLength == 3)
        {
            txtT4.Focus();
        }
        if (txtT4.TextLength == 3)
        {
            txtInterval.Focus();
        }
        if (txtInterval.TextLength == 3)
        {
            txtRepeat.Focus();
        }
        if (txtRepeat.TextLength == 3)
        {
            btnPing.Focus();
        }
    }
}
like image 310
NewHelpNeeder Avatar asked Feb 19 '26 05:02

NewHelpNeeder


2 Answers

Add the Enter event for the TextBoxes, and do a SelectAll on the text.

txtF2.Enter += SelectTextOnEnter;
txtF3.Enter += SelectTextOnEnter;
// etc.


private void SelectTextOnEnter(object sender, EventArgs e) { 
  ((TextBox)sender).SelectAll();
}

Also, your NextField method doesn't quite do what you think it does. Make sure your controls TabIndex values are in the correct order, then try changing your code to this:

private void NextField(object sender, EventArgs e) {
  if (((TextBox)sender).Text.Length == 3)
    this.SelectNextControl((Control)sender, true, true, false, false);
}

It will choose the next control based on the tab order of the form.

like image 50
LarsTech Avatar answered Feb 20 '26 19:02

LarsTech


I would think that setting the the textbox Text property to an empty string prior to calling the Focus method would do what you want.

if (txtF1.TextLength == 3)
{
    txtF2.Text = String.Empty;
    txtF2.Focus();
}
.
.
.
like image 45
JOpuckman Avatar answered Feb 20 '26 18:02

JOpuckman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!