Solved

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

Posted on 2012-03-18
13
1,648 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now