using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NCPS.Forms
{
    public partial class MDIParent1 : Form   
    {
        Form_Main mForm = new Form_Main();

        public MDIParent1()
        {
            InitializeComponent();
        }

        private void MDIParent1_Load(object sender, EventArgs e)
        {           
            mForm.MdiParent = this;
            mForm.Show();
        }

        private void S_SMGP_Click(object sender, EventArgs e)
        {
            //mForm.T_Rows.Text = "4";
            //mForm.T_Columns.Text = "3";
            //mForm.T_Prefix.Text = "SM0";
            //mForm.T_SeriesLength.Text = "8";
            //mForm.T_PageLength.Text = "6";
            //mForm.T_Date.Text = "Jul. 01, 2012";
            //mForm.T_Pack.Text = "500";
            //mForm.CB_Terminator.Checked = true;
            //mForm.C_Delimiter.Text = "space";
        }


    }
}

Good day guys, I have here a code, in a class, where it should access some controls from a form (form1) when the user clicks a button from (parent form). But, It doesn't work, I'm new to C# so don't really know if I'm doing it right :)

Dani AI

Generated

The observed problem is almost always the same: the module creates its own Form_Main instance, updates that invisible instance, and never touches the Form_Main the UI is showing. created new Form_Main() inside Smgp_Process, while the parent form also creates/shows a different Form_Main. That explains why changes “don’t work.” ’s MDI comment is relevant too — make sure the same instance gets MdiParent and Show() called on it.

Prefer passing the real form into the module (constructor or method) and keep UI access minimal. Do not make controls public unless necessary. A clean pattern is to expose a small interface or method on the form that maps to controls, then inject that interface into the processing class:

public interface ISmgpTarget
{
    string Rows { get; set; }
    string Prefix { get; set; }
    void ApplyChanges();   // optional batch apply
}

// SmgpProcess receives the existing form (or its interface)
public class SmgpProcess
{
    readonly ISmgpTarget _target;
    public SmgpProcess(ISmgpTarget target) { _target = target; }
    public void InitDefaults()
    {
        _target.Rows = "4";
        _target.Prefix = "SM0";
        _target.ApplyChanges();
    }
}

Implement ISmgpTarget in Form_Main with properties that internally set the control Text properties (keeps controls private). If updates may come from background threads, marshal to the UI thread with Invoke/BeginInvoke.

Quick troubleshooting checklist:

  • Put a breakpoint and confirm you have only one Form_Main instance (compare object identity).
  • If controls are null, ensure InitializeComponent() ran before setting them.
  • If direct access is used, set the Designer “Modifiers” only as a last resort — prefer wrapper properties or methods.
  • Prefer the event/DTO pattern if the module should not know about UI at all: module emits a settings object and the form subscribes and applies it.

This keeps coupling low, avoids the “updates on the wrong instance” bug, and follows the safer suggestions from while improving encapsulation.

Recommended Answers

All 5 Replies

My bad, it's the code from the parent form. Here's the code in the class.

using System;
using System.Collections.Generic;
using System.Text;

namespace NCPS.Modules
{
    class Smgp_Process
    {
        Form_Main mForm = new Form_Main();

        public void smgp_initial_value()
        {
            mForm.T_Rows.Text = "4";
            mForm.T_Columns.Text = "3";
            mForm.T_Prefix.Text = "SM0";
            mForm.T_SeriesLength.Text = "8";
            mForm.T_PageLength.Text = "6";
            mForm.T_Date.Text = "Jul. 01, 2012";
            mForm.T_Pack.Text = "500";
            mForm.CB_Terminator.Checked = true;
            mForm.C_Delimiter.Text = "space";
        }

    }
}

Don't know exactly what your intentions are. Are you trying to set up a multiple document interface ?

The form mForm is created from a Form_Main class. What is Form_Main?

If you set up a Form not through the designer but programmatically, you also have to set up all the controls it contains via code.

You basically have the right idea in the first snippet, assuming of course that all the controls were set up in the designer. The code in the child form isn't necessary you can do what you want from the parent form.

With this code, when the child form is shown it will have initial values in the controls. When a button on the main form is clicked those values will change. This should give you enough to proceed having 2 examples of how to access the controls.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    namespace NCPS.Forms
    {
        public partial class MDIParent1 : Form
        {
            Form_Main mForm = new Form_Main();
            public MDIParent1()
            {
                InitializeComponent();
            }
            private void MDIParent1_Load(object sender, EventArgs e)
            {
                mForm.MdiParent = this;
                smgp_initial_value();
                mForm.Show();
            }
            private void S_SMGP_Click(object sender, EventArgs e)
            {
                mForm.T_Rows.Text = "5";
                mForm.T_Columns.Text = "6";
                mForm.T_Prefix.Text = "SM1";
                mForm.T_SeriesLength.Text = "5";
                mForm.T_PageLength.Text = "5";
                mForm.T_Date.Text = "Jul. 02, 2012";
                mForm.T_Pack.Text = "5001";
                mForm.CB_Terminator.Checked = false;
                mForm.C_Delimiter.Text = "comma";
            }
            public void smgp_initial_value()
            {
                mForm.T_Rows.Text = "4";
                mForm.T_Columns.Text = "3";
                mForm.T_Prefix.Text = "SM0";
                mForm.T_SeriesLength.Text = "8";
                mForm.T_PageLength.Text = "6";
                mForm.T_Date.Text = "Jul. 01, 2012";
                mForm.T_Pack.Text = "500";
                mForm.CB_Terminator.Checked = true;
                mForm.C_Delimiter.Text = "space";
            }                
        }
    }

Thanks for your response guys, What I want to do is to access all the control from a certain form (Form_Main), using a code in my class (NCPS.MODULES).

With your class written like this:

class Smgp_Process
{
    public Form_Main mForm = new Form_Main();

    public Smgp_Process()
    {
        smgp_initial_value();
    }

    public void smgp_initial_value()
    {
        mForm.T_Rows.Text = "4";
        mForm.T_Columns.Text = "3";
        mForm.T_Prefix.Text = "SM0";
        mForm.T_SeriesLength.Text = "8";
        mForm.T_PageLength.Text = "6";
        mForm.T_Date.Text = "Jul. 01, 2012";
        mForm.T_Pack.Text = "500";
        mForm.CB_Terminator.Checked = true;
        mForm.C_Delimiter.Text = "space";
    }
}

You would use it in your parent form like this:

    Smgp_Process NewProcess = new Smgp_Process();
    private void button1_Click(object sender, EventArgs e)
    {            
        NewProcess.mForm.MdiParent = this;
        NewProcess.mForm.Show();
    }

This assumes that the parent form has MDIContainer set to true, and that every control you want to access from outside the child form has its Modifier property set to public.

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.