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
Solved

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

Posted on 2012-03-18
13
1,836 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 85

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 85

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 85

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

860 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