Solved

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

Posted on 2012-03-18
13
1,962 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

0
 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS2010 Build fails to install 14 102
C# Formatting DateTime String Value 5 68
What are MicroServices? 4 67
Using the .NET JSON Serializer to create a single string 6 58
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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