How do I activate confirm the form by enter? [closed]

-1

I'm doing a C # program that plays within TextBox into Grid , but I'm only getting through a button. I want to know how do I for the provider user the first given, hit enter, skip for age, and after that hit enter again information go to 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++;

        }
    }
}
    
asked by anonymous 26.07.2017 / 20:19

3 answers

2

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

It may be that some other event is more appropriate depending on the case, on the Leave page, all of which are executed during the control process. You may need some more specific control. You have to understand the purpose of choosing the right event. It does not look like this, but it could be Validating .

See the documentation for see everything this control can do. Everything you want to know has in the documentation, always look for 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 it is , and probably the code should have other problems .

    
26.07.2017 / 20:34
1

I use this function for this, I apply all controls to use 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);
                            }
                        };
                }
            }
        }
    }

  }

Call the function in the Form constructor:

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

ps. I removed some peculiarities of my function code, please take a test.

    
26.07.2017 / 20:42
0

You can create an event for textBox for when the user press enter and call the function with that.

Use the variable ActiveControl to check 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's commented, put your code to add to Grid . I did not want to do this for you because your code is pretty weird about this and I do not know the way you are using it.

Tip: You do not have to use array to do this, after all, in your code, it will only use the index [0] of the array, so you'd better use a normal (unique) / p>     

26.07.2017 / 20:34