Radek Tarant
Radek Tarant

Reputation: 227

How can I change label text in different class (C#)

Please, you can you help me, how can i change label text in diferent class?

Basic winform script:

public partial class buildEditor : Form
{
    public buildEditor()
    {
        InitializeComponent();
        Label maxSkillPoint = new Label();
        maxSkillPoint.AutoSize = true;
        maxSkillPoint.BackColor = System.Drawing.Color.Transparent;
        maxSkillPoint.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
        maxSkillPoint.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(193)))), ((int)(((byte)(196)))), ((int)(((byte)(181)))));
        maxSkillPoint.Location = new System.Drawing.Point(528, 687);
        maxSkillPoint.Name = "maxSkillPoint";
        maxSkillPoint.Text = UniqueValue.spentSkillPoints.ToString();
        maxSkillPoint.Size = new System.Drawing.Size(0, 20);
        this.Controls.Add(maxSkillPoint);
    }

    public void maxSkillPoint_TextChanged(Form formInstance, string labelName)
    {
        // Get reference to the label
        var label = formInstance.Controls.Find(labelName, true).FirstOrDefault();
        if (null != label && label is Label)
        {
            (label as Label).Text = "test";
        }
    }
}

I created next class which will be change text for maxSkill text.

public class ChangeTextForMaxSkill()
{
    Button button = new Button();

    public ChangeTextForMaxSkill()
    {
        button.Click += new EventHandler(changeText);
    }

    private void changeText(object sender, EventArgs e)
    {
        // Get reference to the label
        var buildEditor = new buildEditor();
        buildEditor.maxSkillPoint_TextChanged(buildEditor, "maxSkillPoint");
    }
}

I really thx for all answers.

Upvotes: 1

Views: 5417

Answers (2)

user6663244
user6663244

Reputation: 41

I got it very simple: Hand over the Label control in the constructor of your external class:

using System.Windows.Forms;

public class Yourclass{

    private Label UpdateLabel;
    public Yourclass (Label yourLabel)
    {
        this.UpdateLabel = yourlabel;
    }

    private void action()
    {
        //here is your update of the label
        UpdateLabel.Text = "Your text";
    }

}

In the form class, create an instance of "yourclass" and hand over the Label:

Yourclass cls = new Yourclass(Label1);

Upvotes: 4

Dmitriy Khaykin
Dmitriy Khaykin

Reputation: 5258

First of all your naming conventions do not follow standard practices. Both class and method names should use uppercase first letters of words, not camel case as you have done. I have used proper naming conventions in my answer.

You have to pass an instance of your BuildEditor* form to your ChangeTextForMaxSkill.ChangeText() function.

Next, the label object maxSkill is not a property of your BuildEditor class. Therefore, you'd need to actually find a reference to that control within the form since you're dynamically adding it.

public partial class BuildEditor : Form
{
    public BuildEditor()
    {
        InitializeComponent();

        Label maxSkill = new Label();
        maxSkill.Name = "MaxSkil"; // need the ID to find it later (makes it easier)
        maxSkill.Location = new Point(1, 1);
        this.Controls.Add(maxSkill);
    }

    // This is just for testing; assumes you dragged a button from toolbox onto your
    // BuildEditor form in the Form Designer
    private void button1_Click(object sender, EventArgs e)
    {
        var changeTextForMaxSkill = new ChangeTextForMaxSkill();
        changeTextForMaxSkill.ChangeText(this, "MaxSkil");
    }

}

public class ChangeTextForMaxSkill
{
    public void ChangeText(Form formInstance, string labelName)
    {
        // Get reference to the label
        var label = formInstance.Controls.Find(labelName, true).FirstOrDefault();
        if (null != label && label is Label)
        {             
            (label as Label) .Text = "test";
        }
    }
}

If you want to test it, just add a button on your form and make the test in the button click handler:

private void button1_Click(object sender, EventArgs e)
{
    var changeTextForMaxSkill = new ChangeTextForMaxSkill();
    changeTextForMaxSkill.ChangeText(this, "MaxSkil");
}

I've tested and this works :)

Upvotes: 1

Related Questions