C# 如何使用 public get set 从 Form 2 启用 Form 1 中的按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16631852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:30:54  来源:igfitidea点击:

How to enable button in Form 1 from Form 2 using public get set?

c#winformsbutton

提问by jhyap

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

我在 Form 1 中有两个按钮,一个是“ShowForm2”按钮,一个是“button1”按钮。

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

默认情况下,按钮 1 处于禁用状态。当我单击“ShowForm2”按钮时,将显示 Form 2。

PrntScn of my form 1

我的表格 1 的 PrntScn

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

所以,我想要的是,当我单击表单 2 中的“button2”时,它将启用表单 1 中的“button1”。

enter image description here

在此处输入图片说明

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

所以,我尝试在我的 form2 类中这样编码:

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?

然后在我的 Form1 类中,我希望将“enable_form1_button1 = true”传递到表单 1 并启用我的表单 1 button1。但是如何做到这一点呢?

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
    }
}

采纳答案by Mukesh Sakre

Form1.cs

表格1.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

表格2.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();
        }
    }
}

回答by Dimitar Dimitrov

Well what you can do is, expose the button as a property and send a reference of your current form to the form that needs to take control:

那么您可以做的是,将按钮作为属性公开,并将当前表单的引用发送到需要控制的表单:

Form1

表格1

public partial class Form1 : Form
{        
    public Button BtnShowForm2
    {
        get { return btnShowForm2; }
        set { btnShowForm2 = value; }
    }

    private void btnShowForm2_Click(object sender, EventArgs e)
    {
        // pass the current form to form2
        Form2 form = new Form2(this);
        form.Show();
    }
}

Then in Form2:

然后在Form2 中

public partial class Form2 : Form
{
    private readonly Form1 _form1;

    public Form2(Form1 form1)
    {
        _form1 = form1;
        InitializeComponent();
    }

    private void btnEnabler_Click(object sender, EventArgs e)
    {
        // access the exposed property here <-- in this case disable it
        _form1.BtnShowForm2.Enabled = false;
    }
}

I hope this is what you're looking for.

我希望这就是你要找的。

回答by hm1984ir

you can send a refrence of your form1 to your form2 in the from2 constructor for example : and then in button clicked event handler in form2 call a public method in form1 to enable the button :

例如,您可以在 from2 构造函数中将您的 form1 的引用发送到您的 form2:然后在 form2 中的按钮单击事件处理程序中调用 form1 中的公共方法以启用按钮:

this could be your form1 code :

这可能是您的 form1 代码:

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

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2(this);
            frm.Show();
        }

        public void enableButton()
        {
            button2.Enabled = true;
        }

this could be your form2 code :

这可能是您的 form2 代码:

public partial class Form2 : Form
    {
        private Form1 frm;
        public Form2(Form1 frm)
        {
            InitializeComponent();

            this.frm = frm;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frm.enableButton();
        }

回答by dharmatech

If you'll only have one instance of Form1you can also get away with having a static Buttonmember in Programwhich references the button you want to enable from Form2.

如果您只有一个实例,Form1您也可以使用一个静态Button成员,Program其中引用您要启用的按钮Form2

Form1.cs:

Form1.cs:

using System;
using System.Windows.Forms;

namespace StackOverflow_2013_05_19_Form1_Form2_buttons
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            button2.Enabled = false;

            Program.button = button2;
        }

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

        private void button2_Click(object sender, EventArgs e) { }
    }
}

Form2.cs:

Form2.cs:

using System;
using System.Windows.Forms;

namespace StackOverflow_2013_05_19_Form1_Form2_buttons
{
    public partial class Form2 : Form
    {
        public Form2()
        { InitializeComponent(); }

        private void button1_Click(object sender, EventArgs e)
        { Program.button.Enabled = true; }
    }
}

Program.cs:

程序.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace StackOverflow_2013_05_19_Form1_Form2_buttons
{
    static class Program
    {
        public static Button button;

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

回答by Mukesh Sakre

I would recommend you to use event/delegates to perform this task. This is the standard way of doing so.

我建议您使用事件/委托来执行此任务。这是这样做的标准方法。

Again passing reference in constructor or making static func/var/properties are just workaround and Called as bad programming.

再次在构造函数中传递引用或制作静态 func/var/properties 只是解决方法,并被称为糟糕的编程。