Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

How to access the label in the child form from the parent form.?
Posted

There is no "Child form" and "Parent form". This is "Owned form" and its Owner.
Try to use this relationship in all cases.

This relationship does not matter at all when it comes to their collaboration.
Please see my Answer here:
How to copy all the items between listboxes in two forms[^]. See the whole discussion — can be very useful.

—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 19-Apr-11 16:09pm    
Good link, my 5
Sergey Alexandrovich Kryukov 19-Apr-11 16:09pm    
Thank you, Espen.
--SA
charles henington 19-Apr-11 18:56pm    
my 5
Sergey Alexandrovich Kryukov 19-Apr-11 19:24pm    
Thank you, Charles,
--SA
charles henington 19-Apr-11 19:42pm    
No prob SA I have followed your stuff for some time and have not yet seen you post anything under 5+
To add to the other answers, do not directly access a control in a different form. Instead expose a public property in the target form, or alternatively expose a public event that callers can hook on to, to listen for changes.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 19-Apr-11 18:47pm    
Absolutely yes! 5+
You should have tried by your own or atleast Googled[^] it.

One of the links, look this as a sample:
Tutorial: Visual Studio Windows Application Passing Data Between Parent And Child Forms[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Apr-11 15:18pm    
I voted 5 for a "useful" reference, but later noticed it's wrong. There are no "parent" and "child" forms, and also, this is only one method, not sure if it is the best one. Also the article goes well beyond the topic with no apparent reason. So, this is not a very good tutorial I think.
It would be much better to reference past CodeProject discussion.

Please see my answer and my past solution referenced.
--SA
Sandeep Mewara 20-Apr-11 0:45am    
:) Done and 5ed!
Sandeep Mewara 20-Apr-11 1:03am    
BTW, if you have not noticed congrats for 100K :thumsup:
Sergey Alexandrovich Kryukov 20-Apr-11 1:16am    
Thank you very much, but...

...100K is 102400, according to my calculator :-) Do I miss anything? What so special about this figure? Perhaps you're referring to some "round" number, but which one? The nearest round number was 0x10000 = 1 << 16, next one will be less rounded: 1 << 17 = 0x20000 = 131072, not so soon... :-)

Thank you!
--SA
Sandeep Mewara 20-Apr-11 1:21am    
Haha!

Your reputation points total is now 100,000+ (100K == 100 kilo == 100 thousand)

Here: http://www.codeproject.com/Messages/3862520/Congrats-SAKryukov.aspx
here we have the parent form

C#
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class ParentForm : Form
    {
        private Button button1;
        private ChildForm childForm = new ChildForm();
        public ParentForm()
        {
            InitializeComponent();
        }
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(0, 0);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // ParentForm
            // 
            this.ClientSize = new System.Drawing.Size(284, 264);
            this.Controls.Add(this.button1);
            this.Name = "ParentForm";
            this.Load += new System.EventHandler(this.ParentForm_Load);
            this.ResumeLayout(false);
        }
        private void ParentForm_Load(object sender, EventArgs e)
        {
            childForm.Show();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (childForm.lblText() == "true")
            {
                childForm.lblText("false");
            }
            else
            {
                childForm.lblText("true");
            }
        }
    }
}


and here we have the child form. Click the button in the parent form and watch the label in the child form

C#
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class ChildForm : Form
    {
        private Label label1;
    
        public ChildForm()
        {
            InitializeComponent();
            label1.Text = "false";
        }
        internal string lblText()
        {
            return label1.Text;
        }
        internal string lblText(string text)
        {
            return label1.Text = text;
        }
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(35, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            // 
            // ChildForm
            // 
            this.ClientSize = new System.Drawing.Size(284, 264);
            this.Controls.Add(this.label1);
            this.Name = "ChildForm";
            this.ResumeLayout(false);
            this.PerformLayout();
        }
    }
}


Now you can also change

C#
internal string lblText()
        {
            return label1.Text;
        }
        internal string lblText(string text)
        {
            return label1.Text = text;
        }


to this

C#
internal string lblText
        {
            get { return label1.Text; }
            set { label1.Text = value; }
        }


If you change to the second method you also have to change how you call the change text method to

C#
if (childForm.lblText == "true")
            {
                childForm.lblText = "false";
            }
            else
            {
                childForm.lblText = "true";
            }
 
Share this answer
 
v3
Comments
charles henington 19-Apr-11 18:59pm    
Who would vote that a 1 does it not answer his question and without exposing the label to the parent form
Nish Nishant 19-Apr-11 19:25pm    
I gave you a 5.

I reckon whoever voted 1 voted so because you added an answer to a thread where the question had already been answered.
charles henington 19-Apr-11 19:43pm    
thank you Nishant

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900