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 :)

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.