如何重新启用从Mainform到另一个表单的按钮?

时间:2017-08-03 11:39:56

标签: c# forms button

如何重新启用从Form1Form2的按钮。

该按钮目前位于Form1,但我需要在Form2重新启用该按钮。

请参阅下面的代码:

public partial class Form2 : Form
{

    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HTCAPTION = 0x2;


    public object Form1 { get; private set; }

    [DllImport("User32.dll")]
    public static extern bool ReleaseCapture();

    [DllImport("User32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

    public Form2()
    {
        InitializeComponent();
    }

    private void Label_XButton2_Click(object sender, EventArgs e)
    {
        Form1.button1.Enabled = true;
    }

我确实添加了"公共表单1"但它仍然不允许我做我想做的事。

3 个答案:

答案 0 :(得分:1)

您的Form1没有"知道"对于Form1的实例。换句话说,它并不知道它存在。通过属性或构造函数将Form2的实例传递给Form2。这是第二种方法:

确保您拥有public partial class Form2 : Form { public const int WM_NCLBUTTONDOWN = 0xA1; public const int HTCAPTION = 0x2; //you already have property, that's good! :) public Form1 Form1 { get; private set; } //rest of Form2 code...

中的财产
Form2

像这样更改Form2构造函数,以启用将实例传递给public Form2(Form1 frm) { InitializeComponent(); //"save" instance in property this.Form1 = frm; }

Form2

在Form1上,在设置Form1时,将其引用传递给Form2 frmTwo = new Form2(this); //"this" is Form1, passed as constructor's param. frmTwo.Show(); ,如下所示:

private void Label_XButton2_Click(object sender, EventArgs e)
{
   this.Form1.button1.Enabled = true;
}

稍后,当您需要访问某些内容时,例如该按钮,您可以这样:

build.gradle

答案 1 :(得分:1)

这是一个食谱:

1)改变这一行

public object Form1 { get; private set; }

属于Form1

类型
public Form1 internalForm1 { get; private set; }

2)制作第二个构造函数,其中Form1为参数:

public Form2(Form1 f1) : this()
{
    internalForm1 = f1;
}

3)在Form1

中创建Form2时,将Form1的当前实例传递给Form2 f2 = new Form2(this) 的构造函数
Form1

4)在public void ChangeButtonStatus() { this.button1.Enabled = !this.button1.Enabled; } 中创建一个执行更改的方法

Form2

4)并在private void Label_XButton2_Click(object sender, EventArgs e) { internalForm1.ChangeButtonStatus(); }

中调用此方法
return promise;

这将翻转按钮

答案 2 :(得分:0)

@ Mong这是我的代码基于你的

表格1

namespace Offloading_Calculator
{
    public partial class Main_Form : Form
    {



        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HTCAPTION = 0x2;

        [DllImport("User32.dll")]
        public static extern bool ReleaseCapture();

        [DllImport("User32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);


        public Main_Form()
        {
            InitializeComponent();
        }



        private void Label_Xbutton_Click(object sender, EventArgs e)
        {
            //Application.Exit();
            if (MessageBox.Show("Do you want to exit?", "Saying Goodbye already?",
                  MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                  == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void Label1_Xbutton_MouseEnter(object sender, EventArgs e)
        {
            Label_Xbutton.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(216)))), ((int)(((byte)(44)))), ((int)(((byte)(7)))));
        }

        private void Label1_Xbutton_MouseLeave(object sender, EventArgs e)
        {
            Label_Xbutton.BackColor = System.Drawing.Color.Transparent;
        }


        private void Label_MinimizeBUtton_MouseEnter(object sender, EventArgs e)
        {
            Label_MinimizeBUtton.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(216)))), ((int)(((byte)(44)))), ((int)(((byte)(7)))));
        }

        private void Label_MinimizeBUtton_MouseLeave(object sender, EventArgs e)
        {
            Label_MinimizeBUtton.BackColor = System.Drawing.Color.Transparent;
        }

        private void Label_MinimizeBUtton_Click(object sender, EventArgs e)
        {

            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;
            }

        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {

                if (e.Button == MouseButtons.Left)
                {
                    ReleaseCapture();
                    SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
                }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (panel2.Height == 680)
            {
                panel2.Height = 45;

            }
            else
            {
                panel2.Height = 680;

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            //var Form2 = new Form2();
            //Form2.Show();
            //button1.Enabled = false;

            Form2 f2 = new Form2(this)

        }

        public void ChangeButtonStatus()
        {
            this.button1.Enabled = !this.button1.Enabled;
        }
    }
}

表格2

namespace Offloading_Calculator
{
    public partial class Form2 : Form
    {

        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HTCAPTION = 0x2;


        public object Form1 { get; private set; }

        public Form1 internalForm1 { get; private set; }


        public Form2(Form1 f1) : this()
        {
            internalForm1 = f1;
        }

        [DllImport("User32.dll")]
        public static extern bool ReleaseCapture();

        [DllImport("User32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        public Form2()
        {
            InitializeComponent();

        }

        private void Label_XButton2_Click(object sender, EventArgs e)
        {
            internalForm1.ChangeButtonStatus();
        }

        private void panel3_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
            }
        }
    }
}

先谢谢你们,

的问候,

Tramyer