Bool as a data source for CheckBox (updating Bool in another thread, except that it affects)

I am trying to find how to assign bool to a checkbox. I want my checkbox.checked value to be updated automatically when the bool data changes. I know that for ComboBox there is a DataSource attribute that makes it with a list, but I can not find the equivalent with a checkbox.

I am trying to use checkBox.DataBindings, but it does not work. On the other hand, I do not know what the third attribute means.

checkBox.DataBindings.Add("Checked", DisableBugWatcher, "check"); 

I need this because I have two independent windows that update the same checkbox value!

EDIT:

I try to use Event to update my main GUI, but it says: Invalid cross-stream operation: Control 'checkBox' is drawn from a stream other than the stream in which it was created.

The problem is due to the fact that the bool value is updated from a different thread than the one it affects.

+6
source share
2 answers

I found an easier way with a friend to solve my problem. I am using DialogResult from my form. When I get back from the form, it will give me the state of the button click and give me the value of the text field. The same code works with my checkbox problem.

Here is a sample code:

 public partial class MainWindow : Form { private OtherWindow m_otherWindow; public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // Lazy create other window if it doesn't exist. m_otherWindow = m_otherWindow ?? new OtherWindow(); // Passe textbox value to other window. m_otherWindow.PassedValue=textBox1.Text; if (m_otherWindow.ShowDialog()==DialogResult.OK) { // Clicked ok : update textbox value with textbox value of other window. textBox1.Text=m_otherWindow.PassedValue; } } private void button2_Click(object sender, EventArgs e) { Close(); } } public partial class OtherWindow : Form { /// <summary> /// Value to be passed to the window. /// </summary> public string PassedValue { get { return textBox1.Text; } set { textBox1.Text = value; } } public OtherWindow() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { DialogResult = DialogResult.OK; } } 
+1
source

To make DataBindings work, you must implement INotifyPropertyChange in a class that contains your bool value. If you hit myValue from a thread other than the user interface thread, you will have to use the SynchronizationContext and initialize myObject in the user interface thread.

 public class myObject : INotifyPropertyChanged { // The class has to be initialized from the UI thread public SynchronizationContext context = SynchronizationContext.Current; bool _myValue; public bool myValue { get { return _myValue; } set { _myValue = value; // if (PropertyChanged != null) // PropertyChanged(this, new PropertyChangedEventArgs("myValue")); if (PropertyChanged != null) { context.Send( new SendOrPostCallback(o => PropertyChanged(this, new PropertyChangedEventArgs("myValue")) ), null); } } } public event PropertyChangedEventHandler PropertyChanged; } 

Then set your DataBinding as follows:

 checkBox1.DataBindings.Add("Checked", myObject.GlobalObject, "myValue"); 

The first parameter is the property of the UI object with which you want to bind. The second attribute is an instance of your target, the third is the name of the property of the target, which must be bound to the first.

I tried my best to flip your script using a timer that switches myValue every second (to check the checkbox accordingly). Here is the code of the form used:

 System.Timers.Timer x = new System.Timers.Timer(); myObject target; public Form1() { InitializeComponent(); target = new myObject(); x.Elapsed += (s, e) => { target.myValue = !target.myValue; }; x.Interval = 1000; checkBox1.DataBindings.Add("Checked", target, "myValue"); x.Start(); } 
+5
source

All Articles