Problem in running tasks with two buttons in the same form in C #

3

I'm new to C #, so forgive some rookie error. I need to put two buttons on the same form, one to call one form, and another to execute a function I'm trying like this:

    private void button1_Click(object sender, EventArgs e)
    {

        dados_bd dados_formulario = new dados_bd();
        conection_database conexao = new conection_database();

        dados_formulario.NOME = textBox1.Text;
        dados_formulario.LOCAL_ARMAZENAMENTO = textBox2.Text;
        dados_formulario.DESCRICAO = textBox3.Text;

        conexao.cadastro(dados_formulario);

        textBox1.Text = "";
        textBox2.Text = "";
        textBox3.Text = "";
   }

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 teste = new Form2();
        teste.ShowDialog();
    }

If I put the function of calling another form on button1, it works, but when I set it the same for button 2, it does not. In fact, no function is available to button2. I would like some clarification on this.

I changed the code, as suggested above, but it did not work. So I put the suggestion in the function of button1 , which was the only one that worked, and the function that was previously assigned to it, in the button that did not respond.

But now the two buttons are calling form2 , even the function being assigned only to button1 :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            new Form2().Show();

       }

        public void button2_Click(object sender, EventArgs e)
        {
            dados_bd dados_formulario = new dados_bd();
            conection_database conexao = new conection_database();

            dados_formulario.NOME = textBox1.Text;
            dados_formulario.LOCAL_ARMAZENAMENTO = textBox2.Text;
            dados_formulario.DESCRICAO = textBox3.Text;

            conexao.cadastro(dados_formulario);

            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
        }
    }
}
    
asked by anonymous 03.05.2017 / 06:40

2 answers

2

Apparently there is not anything peculiar that makes the code work on the first button and does not work on the second. Anyway, I'd change the invocation a bit:

private void button2_Click(object sender, EventArgs e)
{
    new Form2().Show();
}

ShowDialog() is not good because form will be invoked as modal, which does not allow you to tinker with other forms while it is open.

Possibly the two buttons are pointing to the same event, in case:

    private void button1_Click(object sender, EventArgs e)
    {
        new Form2().Show();
    }

Click each button and check the properties, especially in the events part:

    
03.05.2017 / 07:08
2

Your problem lies in the assignment of the event, you can assign it via code:

 public Form1()
    {
        InitializeComponent();
        button1.Click += button1_Click;
        button2.Click += button2_Click;
    }

    void button2_Click(object sender, EventArgs e)
    {
        //Evento click do botão 2
    }

    void button1_Click(object sender, EventArgs e)
    {
        //Evento click do Botão 1
    }

or by IDE as Gypsy showed above, would look like this:

By remembering that by clicking on the button (within the visual studio), it will either be taken to the Click event, or double click on the blank space in front of the desired Event in the properties window, which will be generated new event for the control.

    
03.05.2017 / 18:07