简体   繁体   English

c#使用form1中form2的复选框

[英]c# using checkbox from form2 in form1

Okay, this is a simple question, but it's been racking my brain for a while now. 好的,这是一个简单的问题,但现在已经搁浅了一段时间。

I have two forms: Form1 and Form2 . 我有两种形式: Form1Form2

I have some checkboxes on Form2 , and I want to use data from checked check boxes on Form2 on Form1 but when I add the following code on Form1 it's giving me errors: 我有一些复选框Form2 ,我想从检查复选框上使用数据Form2Form1 ,但是当我上添加以下代码Form1这是给我的错误:

if (cbTESTING.Checked) 
{
    uri_testings += string.Format("{0}.TESTINGS,", word);
}

I'm getting an error with cbTESTING as it's not referenced on Form1 . 我在cbTESTING中遇到错误,因为它没有在Form1引用。

How can I use or reference checkboxes from Form2 in Form1 ? 如何在Form1使用或引用Form2中的复选框?

I would do something like this: 我会做这样的事情:

Since Form1 creates Form2 , and Form1 needs to manipulate Form2 then you can change this from Form2.Designer.cs: 由于Form1创建Form2Form1需要操作Form2那么您可以从Form2.Designer.cs更改它:

private System.Windows.Forms.Checkbox cbTESTING;

To: 至:

public System.Windows.Forms.Checkbox cbTESTING;

Assuming in Form1 you created Form2 like this: 假设在Form1你创建了Form2如下所示:

Form2 f2 = Form2();
f2.Show();

Then you use this to check cbTESTING : 然后你用它来检查cbTESTING

if(f2.cbTESTING.Checked) // do stuff  ;

EDIT: I have seen your comment which says they do not relate to each other at all which makes it impossible to achieve in any easy method. 编辑:我已经看到你的评论说它们完全没有关系,这使得用任何简单的方法都无法实现。 What you said implies communication between these two THREADS since each Form runs in a Thread and these threads are unrelated. 你所说的意味着这两个THREADS之间的通信,因为每个Form在一个Thread运行,这些线程是不相关的。 Communication is NOT an easy thing to do, you can try it using UDP and Events but trust me, having a direct relation between them would make things MUCH easier for you. 沟通并不是一件容易的事情,你可以尝试使用UDPEvents但相信我,它们之间有直接的关系会让事情变得更容易。

Anyway, I would assume some other Form or Thread would launch these Forms ? 无论如何,我会假设其他一些FormThread会发布这些Forms

Here's a pretty clean way to fix this.. just change the access modifier to public for cbTESTING in the designer for Form2 . 这是解决此问题的一种非常简洁的方法..只需在Form2的设计器中将访问修饰符更改为public for cbTESTING

That is, in Form2.Designer.cs, change 也就是说,在Form2.Designer.cs中,进行更改

private System.Windows.Forms.Checkbox cbTESTING;

to

public System.Windows.Forms.Checkbox cbTESTING;

Then, Form1 can look like this: 然后, Form1可以如下所示:

public partial class Form1 : Form
{
    public Form1() {
        InitalizeComponent();

        Form2 secondForm = new Form2();
        bool isChecked = secondForm.cbTESTING.checked;
    }
}

Edits: removed protected solution, which isn't a great option in this case. 编辑:删除protected解决方案,在这种情况下这不是一个很好的选择。

Might not be the most elegant solution but will definitely work. 可能不是最优雅的解决方案,但肯定会有效。 Declare a public static bool variable in Form2. 在Form2中声明一个公共静态bool变量。 Add Checkbox changed event to your checkboxes and change those static variables accordingly. 将复选框更改的事件添加到您的复选框并相应地更改这些静态变量。 Then just check those variable in Form1 such as Form2.VariableName let me know if you need further explanation 然后只需检查Form1中的那些变量,如Form2.VariableName如果您需要进一步说明, Form2.VariableName告诉我

Edit 编辑

In Form2 declare public static bool CheckBoxStatus = false; 在Form2中声明public static bool CheckBoxStatus = false; false by default, you can change that. 默认情况下为false,您可以更改它。

Also in Form2 add following event checkBox1.CheckedChanged += new EventHandler(CheckedChanged) and add appropriate function such as 另外在Form2中添加以下事件checkBox1.CheckedChanged += new EventHandler(CheckedChanged)并添加适当的函数,如

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

And Finally in Form1 one you can simply check if that checkbox is checked like this 最后在Form1中你可以简单地检查是否选中了这个复选框

if(Form2.CheckBoxStatus == true) ;
else ;

Hope that helps. 希望有所帮助。 *PS sorry my formatting, new to posting answers here. * PS对不起我的格式,新到发布答案在这里。

Here you have a ugly way to do what you want: 在这里你有一个丑陋的方式来做你想要的:

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

    private void button1_Click(object sender, EventArgs e)
    {
        var frm = new Form2();
        frm.ShowDialog();

        label1.Text = frm.TextBoxChecked;
    }
}

//Just declare a prop into Form2 a set it with the value you need
public partial class Form2 : Form
{
    public string TextBoxChecked { get; set; }
    public Form2()
    {
        InitializeComponent();
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
            TextBoxChecked = "Checkbox_1_checked";
        else
            TextBoxChecked = "Checkbox_1_unchecked";
    }
}

Lets do things in a cool way. 让我们以一种很酷的方式做事。 Maybe a good aproache is to say to Form2: "Hey you, when your checkbox change let me know", It sounds like a callback. 也许对Form2说一个好的方法:“嘿,当你的复选框改变时让我知道”,这听起来像是一个回调。 So let do it: 所以,让我们这样做:

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

    private void doWhenCheckBoxChange(string text)
    {
        //I'm receiving the notification indicating that the checkbox has changed
        label1.Text = text;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var frm = new Form2();
        //I'm passing a callback to Form2, Here is where I say 
        //"Hey you, let me know where your checkbox change"
        frm.DoWhenCheckboxChange = doWhenCheckBoxChange;
        frm.ShowDialog();

        //label1.Text = frm.TextBoxChecked;
    }
}

public partial class Form2 : Form
{
    //public string TextBoxChecked { get; set; }
    public Action<string> DoWhenCheckboxChange;
    public Form2()
    {
        InitializeComponent();
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        //TextBoxChecked = "Checkbox_1";
        //Notify to Form1 that checkbox has changed.
        if (checkBox1.Checked)
            DoWhenCheckboxChange("Checkbox_1_checked");
        else
            DoWhenCheckboxChange("Checkbox_1_unchecked");
    }
}

If you test the second aproache, you will discover that you dont need to close your Form2 to see the changes. 如果您测试第二个aproache,您会发现您不需要关闭Form2来查看更改。

I managed to do this by making the checkboxes save values in the properties settings default and then calling them that way as every time i wanted to open the program instead of having to click them again it's auto saved my current values. 我设法做到这一点,通过使复选框保存属性设置默认值,然后调用它们,因为每次我想打开程序而不必再次单击它时,它会自动保存我当前的值。

Thank you all for your help though. 谢谢大家的帮助。

Here is some code for future reference if anyone else wanted to do it the way I did. 以下是一些代码供将来参考,如果其他人想按照我的方式做到这一点。

Here is the code that saves it to the settings. 以下是将其保存到设置的代码。

            if (cbTESTING.Checked)
            Properties.Settings.Default.cbTESTING = true;
        else
            Properties.Settings.Default.cbTESTing = false;

and here was the code to call that in a different form. 以下是以不同形式调用它的代码。

                if (Properties.Settings.Default.cbTESTING == true)
            {
                uri_domains += string.Format("{0}.testing,", word);
            }

Hope this will help someone in the future! 希望这将有助于未来的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM