Manipulating event button in various formats

Asked

Viewed 653 times

2

Good guys, I’m trying to make a manipulation at the event button, in several forms different, I’m using C# WindowsFormAplication.

Well, I will try to say my logic here (I hope you understand).

I have a small system, it has T_Principal (see img below):

inserir a descrição da imagem aqui

Note that on this same screen has 2 Icon, the first blank, another a floppy disk.

I added to them the event click.

My doubt is really in this part now, see the image below:

inserir a descrição da imagem aqui

As you can see, man Form T_Agendamentos this one inside T_Principal.

I want to register events, which when clicking on the floppy disk triggers the event, check which form is triggering the event and executes the registration code. You can do this?

2 answers

1

In fact T_Agendamentos is not in, is in front of T_Principal, so much so that it has the close buttons, minimize. If you open T_Agendamentos.ShowDialog() it will not let you click on the back window which is where the floppy disk is, have to use T_Agendamentos.Show().

Much more practical is if the floppy disk can stay on T_Agendamentos. The new window collects the data and sends it to the main archive. But suppose this is not an option...

If your classes call T_Principal and T_Agendamentos.

In T_Principal

using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class T_Principal  : Form
    {
        public T_Principal()
        {
            InitializeComponent();
        }

        // Variável que guardará a referência da janela T_Agendamentos
        public T_Agendamentos JanelaAgenda;        

        // Abrir janela de cadastro
        private void NovoCadastro_Click(object sender, System.EventArgs e)
        {
            this.JanelaAgenda = new T_Agendamentos();
            JanelaAgenda.Show();
        }

        // Ícone do disquete clicado. Obter dados preenchidos
        // em T_Agendamentos e cadastrar
        private void Salvar_Click(object sender, System.EventArgs e)
        {
            string agendamento = null;
            string data = null;
            string solicitante = null;
            string local = null;

            // Esse método preenche as quatro variáveis
            this.JanelaAgenda.Cadastrar(ref agendamento, ref data,
                ref solicitante, ref local);

            // Código que faz o cadastro...
        }
    }
}

And in T_Agendamentos

using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class T_Agendamentos : Form
    {
        public T_Agendamentos()
        {
            InitializeComponent();
        }

        // Método que passará para T_Principal os dados dos campos preenchidos
        public void Cadastrar(ref string Agendamento, ref string Data,
            ref string Solicitante, ref string Local)
        {
            // No caso as variáveis estão sendo passadas por referência,
            // preencher aqui é o mesmo que preencher de onde o método está sendo chamado
            // Basta passar os dados que foram preenchidos
            Agendamento = "Campo 1";
            Data = "Campo 2";
            Solicitante = "Campo 3";
            Local = "Campo 4";

            this.Close(); // Fecha a janela de cadastro
        }
    }
}

0


I do not know if it comes to the case, but beyond what proposed by Marcelo Nascimento, you can also create a delegate that receives a parameter that is your form trigger, take this parameter and fire the method relative to the form that triggered the event. It’s just one more option.

Behold that link that I think will help you.

  • could give an example?

Browser other questions tagged

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