简体   繁体   English

如何在不同的类(C#)中更改标签文本

[英]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: 基本的winform脚本:

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. 我创建了下一个类,该类将更改maxSkill文本。

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. 我真的很想所有答案。

I got it very simple: Hand over the Label control in the constructor of your external class: 我得到的非常简单:在外部类的构造函数中移交Label控件:

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: 在表单类中,创建“您的类”的实例并移交给Label:

Yourclass cls = new Yourclass(Label1);

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. 您必须将BuildEditor *表单的instance传递给ChangeTextForMaxSkill.ChangeText()函数。

Next, the label object maxSkill is not a property of your BuildEditor class. 接下来,标签对象maxSkill不是BuildEditor类的属性。 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 :) 我已经测试过了,这有效:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM