How do I trigger confirm the form via enter?

Asked

Viewed 186 times

-1

I’m doing a C# program that plays what’s inside the TextBox into the Grid, But I’m only getting it through a button. I want to know how to make the supplier user the first data, press enter, skip to age, and after that press enter again the information go to the Grid.

    string[] Nome = new string[5];
    int[] Idade = new int[5];
    DateTime[] Data = new DateTime[2];



    public Form1()
    {
        InitializeComponent();
    }


    private void Form1_Load(object sender, EventArgs e)
    {

    }

    public void button1_Click(object sender, EventArgs e)
    {
        Acao();

    }



    public void Acao()
    {
        int i = 0;

        while (i < 1)
        {

            Nome[i] = textBox1.Text;
            Idade[i] = Convert.ToInt32(textBox2.Text);

            dtgCliente.Rows.Add(Nome[i], Idade[i]);
            textBox1.Text = "";
            textBox2.Text = "";

            i++;

        }
    }
}

3 answers

2

Probably what you want to do can be started in an event Leave of TextBox. This way every time you go out of control it will be fired and answer what you want without needing any button.

It may be that some other event is more appropriate depending on the case on Leave has everyone running during the control process. It may need some more specific control still. You have to understand the goal to choose the right event. It doesn’t seem, but it could be the Validating.

See the documentation to see everything this control can do. Everything you want to know has in the documentation, always look in it.

As an additional note Convert.ToInt32(textBox2.Text); does not work right. If the person does not enter a number an exception will occur, that’s not how you do it and probably the code must have other problems.

1

I use this function for this, apply to all controls to use the Enter to jump to the next.

Function code:

 public static class Funcoes 
 { 
    /// <summary>
    /// Ao pressionar ENTER no controle, ele pula para o próximo
    /// </summary>
    /// <param name="_ctrl">Controle (obs: todos os controles filhos, serão afetados)</param>
    public static void TrocaTabPorEnter(Control _ctrl)
    {
        if (_ctrl.HasChildren)
        {
            foreach (Control _child in _ctrl.Controls)
            {
                if (_ctrl.HasChildren)
                    TrocaTabPorEnter(_child);
            }
        }
        else
        {
            ///Não funciona para Numeric Up Down   _ctrl is Button ||
            if (_ctrl is RichTextBox || _ctrl is Button || _ctrl is TextBox || _ctrl is MaskedTextBox || _ctrl is ListBox || _ctrl is CheckBox ||  _ctrl is DateTimePicker || _ctrl is ComboBox || _ctrl is NumericUpDown || _ctrl is TrackBar || _ctrl is RadioButton || _ctrl is TabPage)
            {
                TextBox tb;
                if (_ctrl is TextBox)
                {
                    tb = ((TextBox)_ctrl);
                    if (!tb.Multiline)
                    {
                        /// inibe a ação do Enter para evitar o comportamento de
                        /// Accept em alguns casos
                        _ctrl.KeyDown += delegate(object sender, KeyEventArgs e)
                        {
                            if (e.KeyCode == Keys.Enter)
                            {
                                e.SuppressKeyPress = true;
                                _ctrl.FindForm().SelectNextControl(_ctrl, !e.Shift, true, true, true);
                            }
                        };
                    }
                    else
                    {
                        tb.ScrollBars = ScrollBars.Both; 
                        _ctrl.KeyDown += delegate(object sender, KeyEventArgs e)
                        {
                            if (e.KeyCode == Keys.Enter && e.Control)
                            {
                                e.SuppressKeyPress = true;
                                _ctrl.FindForm().SelectNextControl(_ctrl, !e.Shift, true, true, true);
                            }
                        };


                    }

                }
                else
                {
                        /// inibe a ação do Enter para evitar o comportamento de
                        /// Accept em alguns casos
                        _ctrl.KeyDown += delegate(object sender, KeyEventArgs e)
                        {
                            if (e.KeyCode == Keys.Enter)
                            {
                                e.SuppressKeyPress = true;
                                //((Control)sender).SelectNextControl(_ctrl, !e.Shift, true, true, true);
                                _ctrl.FindForm().SelectNextControl(_ctrl, !e.Shift, true, true, true);
                            }
                        };
                }
            }
        }
    }

  }

Calling the function in the Form constructor:

 public Form1()
 {
        InitializeComponent();
        Funcoes.TrocaTabPorEnter(this);
 }

ps. I removed some particularities from my function code, please do a test.

0

You can create an event for the textBox when the user presses enter and calls the function with this.

Use the variable ActiveControl to verify in which textBox the form is focused:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        acao()
    }
}

private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        acao()
    }
}

void acao(){
    if (this.ActiveControl == textBox1)
    {
        this.ActiveControl = textBox2;
    }
    else
    {
        //Colocar no Grid
    }
}

In the part that is commented, put your code to add to Grid. I didn’t want to do this for you because your code is pretty weird about it and I don’t know the way you’re using it.

Tip: There’s no reason to use array to do this, after all, in your code, it will only use index [0] array, so it would be better if you used a normal variable (single).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.