0

Okay, this is a simple question, but it's been racking my brain for a while now.

I have two forms: Form1and Form2.

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:

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

I'm getting an error with cbTESTING as it's not referenced on Form1.

How can I use or reference checkboxes from Form2 in Form1?

4
  • How do the forms relate? Does Form1 have a form2 variable of type Form2? Commented Jan 18, 2017 at 21:16
  • as of right now they don't relate at all, but im wanting to use the functions from the check boxes on form2 in form1. Commented Jan 18, 2017 at 21:23
  • You could simply add public methods to the form to return the variable values, no? Commented Jan 18, 2017 at 22:16
  • @AndrewTruckle I do not think that is what helps because he has a problem of access. @Dzje I edited my answer concerning the connection between these two forms. As I mentioned there, if you have them totally unrelated then you have to start communication between them, either by sockets and events or using WCF. If one of them creates the other it would be easier for us to help you either by my answer in the case of Form1 creating Form2 or I can tell you if it was the other way around. Commented Jan 19, 2017 at 20:02

5 Answers 5

1

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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:

private System.Windows.Forms.Checkbox cbTESTING;

To:

public System.Windows.Forms.Checkbox cbTESTING;

Assuming in Form1 you created Form2 like this:

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

Then you use this to check 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. 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.

Anyway, I would assume some other Form or Thread would launch these Forms?

3 Comments

I saw your comment, and immediately made these exact revisions a couple of minutes ago. I made an error distinguishing between protected and public, but have fixed that in my answer. Thanks for catching my mistake!
Alright, I am not sure the first method works, I will remove it anyway. Thanks for the reply
Residual typo; I had them flipped from my original post. Please make sure it's correct now.
1

Here's a pretty clean way to fix this.. just change the access modifier to public for cbTESTING in the designer for Form2.

That is, in Form2.Designer.cs, change

private System.Windows.Forms.Checkbox cbTESTING;

to

public System.Windows.Forms.Checkbox cbTESTING;

Then, Form1 can look like this:

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.

4 Comments

protected works for inherited classes, Form1 should be inheriting Form2 not otherwise because Form1 is using Form2.
I was confused by the use of protected which lead me wrongly. protected would not work anyway. However, in your solution using public it kind of looks like my solution, but now Form1 : Form and Form2 : Form because it is accessing its instance public variable.
@AmmarSalman I just spent some time fiddling around in Visual Studio, and it turns out I desperately needed a lesson on access modifiers before trying to suggest how to use them to other people. I've completely removed the protected part from my answer because I agree that it's not the best option for this case.
Yeah, I do not think it could work in the first place because it will try accessing a field/property from the base class which does not really help because the base class is a source not instance and we need to access the instance that already exists. Anyway, in my answer I made an edit on the issue.
0

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. 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.

Comments

0

Might not be the most elegant solution but will definitely work. Declare a public static bool variable in Form2. 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

Edit

In Form2 declare public static bool CheckBoxStatus = false; false by default, you can change that.

Also in Form2 add following event checkBox1.CheckedChanged += new EventHandler(CheckedChanged) and add appropriate function such as

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

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

Hope that helps. *PS sorry my formatting, new to posting answers here.

2 Comments

Hey, would appreciate very much if you could give an example explanation on this.
Edited my answer for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.