Search code examples
c#texttextboxmessageboxtextchanged

How to fire Text.TextChanged event with timer


I do some validations regarding a path (if a file exists or not in the specified directory) and I need to fire up the TextChangedEvent after some time passed after the error to check again for that file. Here is the code that I use to check for this error:

 private void textBoxNovoSvrc_TextChanged(object sender, EventArgs e)
        {
            First++;
            bool ok = false;
            int same = 0;
            try
            {

                if (!String.IsNullOrWhiteSpace(textBoxNovoSvrc.Text) && Program.Funcoes.ExisteDirectoria(textBoxNovoSvrc.Text) == true)
                {
                    if (Erro2 == -1 || Erro2 == 0)

                        same = TextBoxSame(First);
                    if (same == 1)
                        return;
                    if (Global.VersaoInvalidaCli == true && Global.VersaoInvalidaSvrc == true)
                    {
                        Global.SvrcChanged = true;
                        buttonContinuar.PerformClick();

                    }
                    if (Program.Funcoes.ExisteFicheiro(textBoxNovoSvrc.Text + @"\OrcaService.exe.config") == true)
                    {
                        if (Global.VersaoInvalidaCli == true && Global.VersaoInvalidaSvrc == true)
                        {
                            buttonContinuar.PerformClick();
                            epDoesntExist1.Dispose();
                            epInvalidVersionSvrc.SetError(textBoxNovoSvrc, "Versão de Update Inválida!");
                            epInvalidVersionSvrc.Dispose();
                            epInvalidVersionCli.Dispose();
                            textBoxNovoCli.Text = Directory.GetParent(textBoxNovoSvrc.Text) + @"\Orca";
                            return;

                        }
                        if (textBox1 == textBoxNovoSvrc.Name || textBox2 == textBoxNovoSvrc.Name)
                        {
                            TextBoxes(textBox1, textBox2, true);
                        }
                        //Colocar aqui a versão mais recente do update;
                        string path = Directory.GetParent(textBoxNovoSvrc.Text).ToString();
                        //string Nome = System.IO.Path.GetFileName(path);
                        labelNovaVersãoServ.Text = Program.Funcoes.NovaVersao(path, Global.versionError);
                        Erro2 = 0;
                        ok = true;
                        errorProviderNoSvrc.Dispose();
                        epInvalidVersionSvrc.Dispose();
                        epDoesntExist2.Dispose();
                        Global.NovoServiço = textBoxNovoSvrc.Text;
                        textBoxNovoSvrc.BackColor = System.Drawing.Color.Green;
                        Continue++;
                        if (Continue >= 4)
                            buttonContinuar.Enabled = true;
                        else
                        {

                            buttonContinuar.Enabled = false;
                        }
                        btnBrowse2.Enabled = true;
                        textBoxNovoCli.Enabled = true;
                        textBoxNovoCli.Text = "";
                        textBoxNovoCli.Text = Directory.GetParent(textBoxNovoSvrc.Text).ToString() + @"\Orca";

                    }
                    else
                    {
                        ok = false;
                        textBoxNovoSvrc.BackColor = System.Drawing.Color.Red;
                        buttonContinuar.Enabled = false;
                        Erro2 = 1;
                        textBoxNovoSvrc.Focus();
                        epDoesntExist2.Dispose();
                        epInvalidVersionSvrc.Dispose();
                        errorProviderNoSvrc.SetError(textBoxNovoSvrc, "Ficheiro \"OrcaService.exe.config\" não existe na directoria");

                        return;

                    }

On the last else after errorProviderNoSvrc.SetError(textBoxNovoSvrc, "Ficheiro \"OrcaService.exe.config\" não existe na directoria");I need to fire a timer to wait around 5 seconds before firing up the textChanged event again. Is this possible to do or do I need to take another aproach such as firing up a message box asking to check again for the file and firing up the TextChanged event "manually" by storing the path on a var and than chaging the text of the textbox like this TexBoxNovoSvrc.Text = "" and than TextBoxNovoSvrc.Text = OldPath.


Solution

  • You can use a timer:

    at the begining(outside textBoxNovoSvrc_TextChanged), maybe in constructor

    Timer t = new Timer();
    t.Interval = 5000;
    t.Tick += textBoxNovoSvrc_TextChanged; 
    

    Just before the last return

    t.Start();
    

    And at the start of textBoxNovoSvrc_TextChanged:

    if(t.Enabled){
        t.Stop()
    }