944,087 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1911
  • C# RSS
Oct 1st, 2009
-1

Checking controlproperties of child from parent

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  1. Form2 form2 = new Form2(this);
  2. 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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MillStrike is offline Offline
13 posts
since Sep 2005
Oct 1st, 2009
0

Re: Checking controlproperties of child from parent

Your main form:
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace daniweb.checkboxes
  11. {
  12. public partial class frmMain : Form
  13. {
  14. private frmOther frm;
  15.  
  16. public frmMain()
  17. {
  18. InitializeComponent();
  19. }
  20.  
  21. private void frmMain_Load(object sender, EventArgs e)
  22. {
  23. frm = new frmOther();
  24. }
  25.  
  26. private void buttonShowOtherForm_Click(object sender, EventArgs e)
  27. {
  28. frm.Show();
  29. }
  30.  
  31. private void buttonHideOtherForm_Click(object sender, EventArgs e)
  32. {
  33. frm.Hide();
  34. }
  35.  
  36. private void buttonCheckAll_Click(object sender, EventArgs e)
  37. {
  38. frm.SetCheckValue(true);
  39. }
  40.  
  41. private void buttonUncheckAll_Click(object sender, EventArgs e)
  42. {
  43. frm.SetCheckValue(false);
  44. }
  45. }
  46. }

Your other form:
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace daniweb.checkboxes
  11. {
  12. public partial class frmOther : Form
  13. {
  14. public frmOther()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. private void frmOther_FormClosing(object sender, FormClosingEventArgs e)
  20. {
  21. e.Cancel = true;
  22. this.Hide();
  23. }
  24.  
  25. public void SetCheckValue(bool Checked)
  26. {
  27. foreach (Control c in this.Controls)
  28. {
  29. SetCheckValue(Checked, c);
  30. }
  31. }
  32.  
  33. private static void SetCheckValue(bool Checked, Control c)
  34. {
  35. CheckBox cb = (c as CheckBox);
  36. if (cb != null)
  37. cb.Checked = Checked;
  38. foreach (Control childCtrl in c.Controls)
  39. {
  40. SetCheckValue(Checked, childCtrl);
  41. }
  42. }
  43.  
  44. }
  45. }
Attached Files
File Type: zip daniweb.checkboxes.zip (16.1 KB, 43 views)
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 2nd, 2009
0

Re: Checking controlproperties of child from parent

Wow, thank you very much. Works like a charm!
You almost made it too easy for me
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MillStrike is offline Offline
13 posts
since Sep 2005
Oct 3rd, 2009
0

Re: Checking controlproperties of child from parent

Click to Expand / Collapse  Quote originally posted by MillStrike ...
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...
Last edited by DdoubleD; Oct 3rd, 2009 at 10:10 am.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Changing specific picture box background
Next Thread in C# Forum Timeline: Reading Listbox values of other window





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC