Hi!
I'm going crazy about this problem.
Here's the setup:
On my mainform I have a menustrip from where I can open a new form (form2) with different controls like checkboxes etc. After I've modified these controls I close or hide that form and continue using the mainform.
Now here's my problem, how do I check from the mainform if ex. a checkbox is checked in form2? Also when I reopen form2 the controls should remain as I modified them last before closing form2 (It would look like to the user that he saves thouse settings).

I've tried:

Form2 form2 = new Form2(this);
form2.Show();

And then use a button to hide it.
But this obviolusly just opens a new "fresh" form every time.

Need some help in the right direction.
Any help is appreciated!

Your main form:

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 daniweb.checkboxes
{
  public partial class frmMain : Form
  {
    private frmOther frm;

    public frmMain()
    {
      InitializeComponent();
    }

    private void frmMain_Load(object sender, EventArgs e)
    {
      frm = new frmOther();
    }

    private void buttonShowOtherForm_Click(object sender, EventArgs e)
    {
      frm.Show();
    }

    private void buttonHideOtherForm_Click(object sender, EventArgs e)
    {
      frm.Hide();
    }

    private void buttonCheckAll_Click(object sender, EventArgs e)
    {
      frm.SetCheckValue(true);
    }

    private void buttonUncheckAll_Click(object sender, EventArgs e)
    {
      frm.SetCheckValue(false);
    }
  }
}

Your other form:

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 daniweb.checkboxes
{
  public partial class frmOther : Form
  {
    public frmOther()
    {
      InitializeComponent();
    }

    private void frmOther_FormClosing(object sender, FormClosingEventArgs e)
    {
      e.Cancel = true;
      this.Hide();
    }

    public void SetCheckValue(bool Checked)
    {
      foreach (Control c in this.Controls)
      {
        SetCheckValue(Checked, c);
      }
    }

    private static void SetCheckValue(bool Checked, Control c)
    {
      CheckBox cb = (c as CheckBox);
      if (cb != null)
        cb.Checked = Checked;
      foreach (Control childCtrl in c.Controls)
      {
        SetCheckValue(Checked, childCtrl);
      }
    }

  }
}

Wow, thank you very much. Works like a charm!
You almost made it too easy for me ;)

Wow, thank you very much. Works like a charm!
You almost made it too easy for me ;)

I thought you were asking how you could determine from main form whether the checkboxes on form2 are checked or unchecked, but it looks like you have your answer.

Please mark this thread as solved. EDIT: Looks like you already did... I'm losing my mind me thinks...

Edited 7 Years Ago by DdoubleD: n/a

This question has already been answered. Start a new discussion instead.