Handling event button in various forms

2

I'm trying to do a manipulation on the event button , on several forms different, I'm using C # WindowsFormAplication .

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

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

Notethatthesamescreenhas2icons,thefirstoneisblank,theotherisafloppydisk.

Iaddedtheclickeventtothem.

Myquestionisreallythispartnow,seetheimagebelow:

As you can see, my Form T_Agendamentos is within T_Principal .

I want to make an event log, which when clicking on the diskette it triggers the event, verifies which form is firing the event and executes the code of the register. Can you do that?

    
asked by anonymous 12.07.2015 / 05:53

2 answers

0

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

See this link that I think will help you.

    
12.07.2015 / 15:14
1

Actually T_Agendamentos is not inside, it is ahead of T_Principal , so much so that it has the close, minimize buttons. If you open T_Agendamentos.ShowDialog() it will not let you click on the back window that is where the floppy is, you have to use T_Agendamentos.Show() .

It is much more practical if the floppy disk can be in T_Agendamentos . The new window collects the data and sends it to the main file. But supposing that this is not an option ...

If your classes are called 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
        }
    }
}
    
12.07.2015 / 07:21