开发者

transfer value between checkboxes in another forms in C#

i need your help. I have two forms, Form1 and Form2. In Form1 i ha开发者_开发知识库ve a checkBox1 and in Form2 I have a checkBox2. All i want is the value of checkBox1 tranfering automatically in checkBox2.

Thanks in advance


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜