Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-18
13
Medium Priority
?
2,302 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 80 total points
ID: 37734557
Declare label1 as global
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 776 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 860 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 860 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 776 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 860 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 776 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 284 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

670 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