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

Asked

Viewed 271 times

3

I’m new to c, so forgive me for making a rookie mistake. I need to put two buttons on the same form,one to call a form, and another to perform 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 in button1, it works, but when I set it to button 2, it doesn’t. Even, no function is available to button2. I would like some clarification on this.


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

But, behold, now the two buttons are calling to form2, even if the function is assigned only to the 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 = "";
        }
    }
}
  • 1

    And Voce is sure that the event is assigned to the element?

  • I’m not sure, tell me how, I repeat, I’m new.

  • Double-click the button2 and check if it is going to the button2_Click method or if it is generating a new method.

  • When I double click, it falls into the button1 method. Do I just need to make this reference to the Button2_click menu? How do I?

  • managed to solve your problem Igor ? please check the reply. Thank you

2 answers

2

Apparently there’s nothing peculiar that makes the code work on the first button and it doesn’t work on the second. Anyway, I would slightly alter the invocation:

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

ShowDialog() is not a good because the form will be invoked as modal, which does not allow to touch other Forms while it’s 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 on each button and check the properties, especially in the event part:

Button Click

  • I don’t usually program for Windows Forms, so forgive the ignorance. But because there is this custom of opening the Forms without keeping a variable as a reference for it?

  • 1

    @Arturotemplary If you won’t need to use a variable, it’s best not to create =D

  • But for example, if you have the possibility to open a form repeatedly, for example, to send a message. Then you open and close it again and again. Still it is better not to create the variable?

  • 1

    @Arturotemplário This is not the case, right? If we can make it simple, why should we complicate it? Let’s leave the complicated uses for the complicated cases.

  • 1

    Thank you so much for the explanations, it’s always nice to hear from someone more experienced :)

2

Your problem is in the event assignment, 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 the Gypsy showed above, would look like this: Evento do botão1

Evento do botão2

remembering that, when giving 2 Clicks on the button (inside the visual studio), it will already be taken to the Click event, or give 2 clicks on the white space in front of the desired Event, in the properties window, which will be generated a new event for control.

Browser other questions tagged

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