从主要表格中调用方法 [英] CALLING A METHOD FROM THE MAIN FORM

查看:73
本文介绍了从主要表格中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有三种形式。其中一个是MainForm(Principal.cs),第二个是Client Form(frmCliente.cs)和子表单(frmCadastrarCliente.cs)。我有以下情况:

当客户端表单中的按钮调用子表单时,它会禁用客户端表单中的所有按钮,并打开子表单。当我点击子表单上的取消时,它应该启用客户表单中的所有按钮并关闭子表单。

问题是我无法执行此操作。首先,我认为使用取消点击事件中的frmClient对象调用方法来启用所有按钮(在客户端表单上),但子表单中的Click事件方法,不识别de frmCliente.cs。有人能帮助我吗?如何直接引用程序知道我想从该表单启用那些按钮而不是实际表单?



frmClientes.cs



Hi people,
I have three forms. One of them is the MainForm(Principal.cs), the next is the Client Form (frmCliente.cs) and a child form (frmCadastrarCliente.cs). I have the follow situation:
When a button from the Client Form calls the child form, it disables all the buttons from the Client Form, and opens the child form. When I click on "cancel" on the child form, it should enable all buttons from the Client Form and close the child form.
The problem is that I couldn''t do it. First I thought that calling the method to enable all buttons (that is on the Client Form) using the frmClient object inside the Cancel Click Event, but the Click Event Method from the child form, doesn''t recognize de frmCliente.cs. Could someone help me? And how do I make reference directly to the program knows I want to enable that buttons from that form and not from the actual form ?

frmClientes.cs

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

namespace Biblioteca
{

    public partial class frmClientes : Form
    {
        public frmClientes()
        {
            InitializeComponent();
        }

        private void DesabilitaBotoes(object sender, EventArgs e)
        {
            tsbtnAlterarExcluir.Enabled = false;
            tsbtnCadastrar.Enabled = false;
            tsbtnConsultar.Enabled = false;
            tsbtnVoltar.Enabled = false;
            tsbtnSair.Enabled = false;

        }

        public void HabilitaBotoes(object sender, EventArgs e)
        {
            tsbtnAlterarExcluir.Enabled = true;
            tsbtnCadastrar.Enabled = true;
            tsbtnConsultar.Enabled = true;
            tsbtnVoltar.Enabled = true;
            tsbtnSair.Enabled = true;
        } 





儿童表格:





The Child Form:

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

namespace Biblioteca
{
    public partial class frmCadastrarClientes : Form
    {
        public frmCadastrarClientes()
        {
            InitializeComponent();
        }
       
        private void tsbtnGravar_Click(object sender, EventArgs e)
        {
        
        }

        private void frmCadastrarClientes_Load(object sender, EventArgs e)
        {

        }

        public void tsbtnCancelar_Click(object sender, EventArgs e)
        {
           objFrmClientes.Habilita ...... <------ Why I Can''t call the method here ???
        }
        
       
    }
   
     
}

推荐答案





看看这里:

在两种形式之间传递信息,第2部分:儿童到父母 [ ^ ]


如果我已经理解了你的问题,那么你正在尝试实现阻塞行为(阻止父表单直到你完成了对子表单的处理),这不是不可能的,但是会让你无法实现它。 br />


无论如何,如果你想按自己的方式去做,你可以在<$ c $中分配你的 HabilitaBotoes 事件处理程序c> frmClientes for FormClosed frmCadastrarClientes 表单就在 frmCadastrarClientes 表单的显示方法之前。就是这样。

If I have understood your problem right you are trying to implement ''blocking'' behaviour (block parent form until you have finished working with child form) yourself, which is not impossible but puts you into trouble of implementing it.

Anyway, if you want to do it your way you can assign your HabilitaBotoes event handler in frmClientes for FormClosed event of frmCadastrarClientes form just before calling Show method of your frmCadastrarClientes form. And that''s it.
frmCadastrarClientes fcc = new frmCadastrarClientes();
fcc.FormClosed += new EventHandler(this.HabilitaBotoes);
fcc.Show();





或者你可以省去实现已实现功能的艰苦工作,只需删除方法 DesabilitaBotoes HabilitaBotoes 并删除附加的一行 HabilitaBotoes FormClosed事件的方法,而不是调用



Or you can spare yourself the hard work of implementing already implemented functionality and just delete methods DesabilitaBotoes and HabilitaBotoes and delete the one line that attaches HabilitaBotoes method to FormClosed event and instead of calling

fcc.Show();

只需调用

fcc.ShowDialog(this);

就是这样。



希望这很有帮助。



快乐编码。

And that''s it.

Hope this was helpful.

Happy coding.


这篇关于从主要表格中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆