本文介绍了如何利用公共获取设置,以便从表2的表格1按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表1两个按钮,一个是ShowForm2按钮,一个是BUTTON1按钮。

按钮1默认情况下禁用。当我点击ShowForm2按钮,表2显示。

所以,我想的是,当我点击表格2的按钮2,它会在形式上1启用BUTTON1。

所以,我尝试code像这样在我的窗口2类:

 公共部分类窗体2:表
{
    布尔enable_form1_button1;
    公共BOOL Enable_form1_button1
    {
        {返回this.enable_form1_button1; }
        集合{this.enable_form1_button1 =价值; }
    }
    公共窗体2()
    {
        的InitializeComponent();
    }    私人无效button2_Click(对象发件人,EventArgs的发送)
    {
        enable_form1_button1 = TRUE;
    }
}

然后在我的Form1类中,我期待得到enable_form1_button1 = true将传递到形式1,使我的表格1 BUTTON1。但如何做到这一点?

 公共部分Form1类:表格
{
    公共Form1中()
    {
        的InitializeComponent();
    }    私人无效btb_Showfrm2_Click(对象发件人,EventArgs的发送)
    {
        窗体2 FRM2 =新Form2的();
        frm2.Show();
        button1.Enabled = frm2.Enable_form1_button1; //我把它放在这里,它只是似乎并不正确
    }
}


解决方案

Form1.cs的

 公共部分Form1类:表格
    {
    公共Form1中()
    {
        的InitializeComponent();
        button1.Enabled = FALSE;
    }    私人无效button2_Click(对象发件人,EventArgs的发送)
    {
        窗体2 oFrm2 =新Form2的();
        oFrm2.evtFrm + =新ShowFrm(oFrm2_evtFrm);
        oFrm2.Show();
    }    无效oFrm2_evtFrm()
    {
        button1.Enabled = TRUE;
    }
}

Form2.cs

 公共委托无效ShowFrm();
    公共部分类窗体2:表
    {
    公共事件ShowFrm evtFrm;
    公共窗体2()
    {
        的InitializeComponent();
    }    私人无效的button1_Click(对象发件人,EventArgs的发送)
    {
        如果(evtFrm!= NULL)
        {
            evtFrm();
        }
    }
}

I have two buttons in Form 1, one is "ShowForm2" button and one is "button1" button.

Button 1 is disable by default. And when I click "ShowForm2" button, Form 2 will show.

So, what I want is, when I click the "button2" in form 2, it will enable the "button1" in form 1.

So, I try to code like this in my form2 class:

public partial class Form2 : Form
{
    bool enable_form1_button1;
    public bool Enable_form1_button1
    {
        get { return this.enable_form1_button1; }
        set { this.enable_form1_button1 = value; }
    }
    public Form2()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        enable_form1_button1 = true;
    }
}

Then in my Form1 class, I am expecting to get the "enable_form1_button1 = true" to pass into form 1 and enable my form 1 button1. But how to do this?

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

    private void btb_Showfrm2_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.Show();
        button1.Enabled = frm2.Enable_form1_button1; // I put it here, and it just does not seems right
    }
}
解决方案

Form1.cs

    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
        button1.Enabled = false;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 oFrm2 = new Form2();
        oFrm2.evtFrm += new ShowFrm(oFrm2_evtFrm);
        oFrm2.Show();
    }

    void oFrm2_evtFrm()
    {
        button1.Enabled = true;
    }
}

Form2.cs

    public delegate void ShowFrm();
    public partial class Form2 : Form
    {
    public event ShowFrm evtFrm;
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (evtFrm != null)
        {
            evtFrm();
        }
    }
}

这篇关于如何利用公共获取设置,以便从表2的表格1按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 16:45