如何更改不同類中的標籤文本?現在,我使用這個腳本,但標籤沒有改變。如何更改不同類中的標籤文本(C#)
Winform的腳本:
public partial class buildEditor : Form
{
ChangeTextForMaxSkill skill = new ChangeTextForMaxSkill(this);
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";
}
}
}
我創建了隔壁班的,這將是對maxSkillPoint標籤改變文本,但它不工作。
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");
}
}
調試find的控制:
buildEditor {test.buildEditor, Text: test} test.buildEditor
this {test.ChangeTextForMaxSkill} test.ChangeTextForMaxSkill
什麼不行呢?它會拋出異常嗎?文字是不是變了? –
是的,標籤上的文字未在「測試」中更改 –
您是否嘗試過調試以查看'label'在調用Find後是否爲空? –