Solved

C# - How to change the text of the label1 by another class?

Posted on 2012-03-18
13
2,108 Views
Last Modified: 2012-06-27
How to change the text of label1 on Form1 in another class, like in the picture?

Change the properties
0
Comment
Question by:ocaccy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +2
13 Comments
 
LVL 2

Assisted Solution

by:mtayyab1984
mtayyab1984 earned 20 total points
ID: 37734557
Declare label1 as global
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 194 total points
ID: 37734585
private void metodo(Label label)
{
//put your code here
}

Open in new window

0
 

Author Comment

by:ocaccy
ID: 37734800
Declare label1 as global

HOW?

BuggyCoder

Error does not appear, but also not working.

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

namespace testeXX
{
    public partial class Form1 : Form
    {
        Class1 c = new Class1();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            c.metodo();
        }
    }
}

Open in new window


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

namespace testeXX
{
    public class Class1
    {
        private void metodo(Label label1 )
        {
            //label1.ForeColor = Color.Red;
            label1.Text = "Hello World !! ";
        }

        internal void metodo()
        {
            //throw new NotImplementedException();
        }
    }
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 215 total points
ID: 37734801
Both solutions above would be "tightly coupled", because the class must have knowledge of the Label.  In general, this makes the class less re-usable since it must be modified to work with different forms/controls.

A "loosely coupled" solution would make the class raise an event that the form subscribes to.  In this manner, the class just passes out its data and doesn't need to know who is using it.  If you decided to use a control other than a Label, then no changes would need to be made inside the class at all (the change would be at the form where the control is; and the change was made).

Here's a simple example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        private Class1 c;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            c = new Class1();
            c.NewValue += new Class1.Data(c_NewValue);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            c.metodo();
        }

        void c_NewValue(string value)
        {
            label1.Text = value;
        }

    }

    public class Class1
    {
        public delegate void Data(string value);
        public event Data NewValue;

        private Random r = new Random();

        public void metodo()
        {
            if (NewValue != null)
            {
                NewValue("RandomValue" + r.Next(1, 11).ToString());
            }
        }

    }

}

Open in new window

1
 

Author Comment

by:ocaccy
ID: 37734814
Idle_Mind, I use this example in the future.

In this time I need an example as above in my last answer.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 215 total points
ID: 37734824
Ok...not really recommended, but here is one way to make the label "global":
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static Label GlobalLabel1 = null;

        private void Form1_Load(object sender, EventArgs e)
        {
            Form1.GlobalLabel1 = this.label1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Class1 c = new Class1();
            c.metodo();
        }
    }

    public class Class1
    {

        public void metodo()
        {
            if (Form1.GlobalLabel1.Text != null)
            {
                Form1.GlobalLabel1.Text = "Hello World!";
            }
        }

    }
}

Open in new window

0
 

Author Comment

by:ocaccy
ID: 37734886
I think you have a good and rational reason to discourage me to use a global label!
Why?
I do not want to put the hand in the house of the snake in the dark.
But my reason for wanting to do this?

My form1.cs with this 12000 lines.
It`s getting difficult to maintain.
So I want to divide it into 4 pieces or:
form1.cs, part2.cs, part3.cs, part4.cs.

Thanks for always helping me.
ocaccy
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 194 total points
ID: 37735064
Here use this:-

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

namespace testeXX
{
    public partial class Form1 : Form
    {
        Class1 c = new Class1();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
//assuming that your label name is lblText
            c.metodo(lblText);
        }
    }
}

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

namespace testeXX
{
    public class Class1
    {
        private void metodo(Label label1 )
        {
            //label1.ForeColor = Color.Red;
            label1.Text = "Hello World !! ";
        }

        internal void metodo()
        {
            //throw new NotImplementedException();
        }
    }
}

Open in new window

0
 

Author Comment

by:ocaccy
ID: 37735140
I try, but.
capture of error
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 215 total points
ID: 37735177
Just change "private" to "public" at line #42 of BuggyCoder's last post.

This:

        private void metodo(Label label1 )

Becomes:

        public void metodo(Label label1 )
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 194 total points
ID: 37735187
i am sorry, the method was private. Here is the modified class1.cs:-

namespace testeXX
{
    internal class Class1
    {
        internal void metodo(Label label1 )
        {
            //label1.ForeColor = Color.Red;
            label1.Text = "Hello World !! ";
        }

        internal void metodo()
        {
            //throw new NotImplementedException();
        }
    }
}

Open in new window

0
 
LVL 13

Assisted Solution

by:Naman Goel
Naman Goel earned 71 total points
ID: 37735259
There are lots of ways to achieve this:
1. Create static object of Label (as told by Idle_Mind not advisable)
2. Pass Label as parameter to method of class1
3. Create Event Handler to handle the same case(as told by Idle_Mind)
4. Create a property for Form1in Class1 class that can be used to pass the reference of Form1 object to Class1 and use that to access it's child control (more generic solution)

Here is code for 4th Method

Form1 Class:


using System;
using System.Windows.Forms;

namespace testeXX
{
    public partial class Form1 : Form
    {
        private Label label1;
        Class1 c = new Class1();
    
        public Form1()
        {
            c.Form1 = this;
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(65, 37);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(35, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            c.methodo();
        }
    }
}

Open in new window



Class1 Class:


using System.Windows.Forms;

namespace testeXX
{
    class Class1
    {
        private Form1 form1 = null;

        internal Form1 Form1
        {
            set { form1 = value; }
        }

        internal void methodo()
        {
            Label label= form1.Controls.Find("Label1", true)[0] as Label;
            label.Text = "Hello World !! ";

        }
    }
}

Open in new window


Also if you want to distribute your large code into multiple files then use partial class facility of .net(http://msdn.microsoft.com/en-us/library/wa80x488%28v=vs.80%29.aspx)
0
 

Author Closing Comment

by:ocaccy
ID: 37735674
It worked.
In the next question will extend this question,
Thanks for the great help and advice too.
ocaccy
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question