transfer value between checkboxes in another forms in C#

为君一笑 提交于 2019-12-13 18:12:03

问题


i need your help. I have two forms, Form1 and Form2. In Form1 i have a checkBox1 and in Form2 I have a checkBox2. All i want is the value of checkBox1 tranfering automatically in checkBox2.

Thanks in advance


回答1:


On Form1:

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

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        Form2 a = new Form2();
        a.c = checkBox1.Checked;
        a.ShowDialog();
    }
}

On Form2:

    public partial class Form2 : Form
{
    public bool c;
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        checkBox1.Checked = c;
    }
}



回答2:


First of all, in a multi-form application, form-form direct contact should not be permitted. However, I can think of a way which I present here considering yours is an exceptional scenario. So the method might violate usual best practices.

Here is the method

  1. Make your checkboxes in your forms as Public. You can do that by clicking on the CheckBox in the design mode and then selecting Public under Modifiers in Properties window. This step makes your checkbox accessible from outside your form's instance.

  2. Write the following code in CheckedChanged event of CheckBox in Form1.

    ((Form2)(Application.openForms["Form2"])).checkBox1.Checked = this.checkBox1.Checked;
    

However, I strongly recommend revisiting your strategy based on your application need.




回答3:


All instances of Form2 within an MDI parent will reflect any change to the CustomCheckBox control Checked property placed on the Form. Of course, this would be true of when placing the CustomCheckBox on any MDI child form, just set up the proper events like Form2.

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 WindowsFormsApplication1 {
    public partial class Form2 : Form {
        public Form2() {
            InitializeComponent();

            CustomCheckBox.CheckChanged += (object sender, EventArgs e) => {
                if (sender != m_customCheckBox) { m_customCheckBox.Checked = CustomCheckBox.GetCheck(); }
            };

            FormClosed += (object _sender, FormClosedEventArgs _e) => {
                CustomCheckBox.CheckChanged -= (object __sender, EventArgs __e) => { };
            };

        }
    };
};



////////////////////////////////////////////////

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

namespace WindowsFormsApplication1 {

    public class CustomCheckBox : CheckBox {
        static private bool? m_checked = null;
        static private object m_synchRoot = new object();
        static public event EventHandler CheckChanged;

        public CustomCheckBox() {
            if (HasCheckValue) {
                // Do this BEFORE we set up the CheckChanged event so that
                // we do not needlessly kick off the CustomCheckBox.CheckChanged
                // event for any other existing CustomCheckBoxes (as they already
                // have their Checked property properly set)...
                Checked = CustomCheckBox.GetCheck();
            }
            this.CheckedChanged += new EventHandler(OnCheckedChanged);
        }       

        protected void OnCheckedChanged(object sender, EventArgs e) {           
            if (CustomCheckBox.HasCheckValue && this.Checked == CustomCheckBox.GetCheck()) {
                return;
            } else {
                CustomCheckBox.SetCheck(this.Checked);
                if (CustomCheckBox.CheckChanged != null) {
                    CustomCheckBox.CheckChanged(sender, e);
                }
            }
        }

        static public bool HasCheckValue {
            get {
                lock (m_synchRoot) {
                    return m_checked.HasValue;
                }
            }
        }

        static public bool GetCheck() {
            lock (m_synchRoot) {
                return m_checked.Value;
            }
        }

        static private void SetCheck(bool _check) {
            lock (m_synchRoot) {
                m_checked = _check;
            }
        }
    };
};


来源:https://stackoverflow.com/questions/6684645/transfer-value-between-checkboxes-in-another-forms-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!