How would I be able to check if a checkbox is checked from a childform and use that information on the parent?

Dani AI

Generated

The root issues in this thread are twofold: a simple syntax/compile mistake, and an architectural choice about where app-wide settings live.

The compile error saw comes from using the method name instead of calling it (the compiler treats that as a method group). Invoke the method (or expose the value as a property) so the parent receives a bool instead of a delegate reference. was right to suggest exposing the state — the pattern below is a cleaner, safer way to do it and avoids relying on reading controls on an unseen form.

Example patterns (different from the code already posted):

/* child form: expose setting as a property */
public bool PlayOnStart => chkPlayOnStart.Checked;

/* parent: show settings dialog and read result */
using (var dlg = new SettingsForm())
{
  if (dlg.ShowDialog() == DialogResult.OK)
  {
    bool play = dlg.PlayOnStart;
    // apply setting
  }
}

For looser coupling use events so the child raises "SettingsSaved" and the parent subscribes. For startup behavior (show windows, play sounds, etc.) store the choice in persistent settings (Properties.Settings, JSON, registry, DB) and read that from the parent on FormLoad instead of instantiating or querying a child form that hasn't run yet. Troubleshooting tips: set breakpoints to ensure you are checking the same instance, avoid creating multiple instances of the child, and prefer ShowDialog() for modal settings dialogs or events/DI for runtime updates.

Recommended Answers

All 9 Replies

Add a public function in Child form

bool public GetStatus()
{
  return checkbox1.Checked;
}

And call this function where you have called for Child.Show();

I need to check on formload, how would I be able to do that?

If you are fetching it in form load did you even check the child form yet wouldn't you get always false? (Unless you set it to be true by default).

Try this in form load.

ChildForm CF =  new ChilForm()
bool StatusCheck;
if (CF.ShowDialog() == DialogResult.OK)
{
   StatusCheck = CF.GetStatus;
}

In my program, the user has the ability to use extra things like play sounds when done and show certain windows on startup.

the error I get is

Error	1	Cannot convert method group 'GetStatus' to non-delegate type 'bool'. Did you intend to invoke the method?	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\iUltimate\iUltimate\Form1.cs	201	28	iUltimate

Hmm show me the code.

Ok, know Im getting data from parent to a child...


form1 is the parent, form8 is the child, also I need other childs to be able to the same thing a form8....

Form1 frm;
        
        public Form8()
        {
            InitializeComponent();
            frm = new Form1(this);   
        }
public Form8 frm;
        public Form1(Form8 F)
        {
            
            frm = F;

Ummm. From Form8 you can simply do Form1.VariableorFunction();

I don't know what you are trying to do up there.

Ok, let me say this, I need to transfer data to another form. I cant get my way to work, can you explain your way more?

in Parent.

int x = 5;
ChildForm CF =  new ChilForm(x)
bool StatusCheck;
if (CF.ShowDialog() == DialogResult.OK)
{
   StatusCheck = CF.GetStatus;
}

In child

int k;
public ChildForm(int y)
{
   InitializeComponent();
   k = y;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.