布尔作为数据源的复选框(布尔更新在比它的影响的其他线程) [英] bool as datasource for a CheckBox (Bool update in an other thread than the one it is impacting)

查看:219
本文介绍了布尔作为数据源的复选框(布尔更新在比它的影响的其他线程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到如何将布尔分配给一个复选框。我想,我的checkbox.checked值会自动刷新,当我的布尔数据的变化。我知道,组合框有数据源的属性,使其与清单,但我不能找到等价使用复选框。

I try to find how to assign a bool to a checkbox. I want that my checkbox.checked value refresh automatically when my bool data change. I know that for ComboBox there are the DataSource attribute that make it with a list but I am not able to find the equivalent with checkbox.

我尝试与checkBox.DataBindings,但它似乎并没有work.On另一方面,我真的不知道是什么意思的第三个属性。

I try with the checkBox.DataBindings but it does not seem to work.On the other hand I don't really know what mean the third attribute.

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

我需要的,因为我有刷新相同的复选框值两个独立的窗口!

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

编辑:

我尝试用事件来更新我的主界面,但它说:跨线程操作无效:控制复选框从一个线程比它创建的线程其他访问

I try to use the Event to update my main GUI but it say : Cross-thread operation not valid: Control 'checkBox' accessed from a thread other than the thread it was created on.

问题是链接到一个事实,即布尔值从比所述一个一个其他线程刷新它的影响。

The problem is link to the fact that the bool value is refresh from an other thread than the one that it impact.

推荐答案

我找到一个更简单的方式与朋友解决我的问题。我使用的DialogResult从我form.When我从形式回到它给我的按钮,点击状态,它给我的文本框的值。同样的code可以与我的复选框的问题。

I find an easier way with a friend to solve my problem. I use the DialogResult from my form.When I come back from the form it give me the state of the button click and it give me the value of the textbox. The same code works with my checkbox problem.

这里是code时为例:

Here is an exemple of the 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;
    }
}

这篇关于布尔作为数据源的复选框(布尔更新在比它的影响的其他线程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆