winforms 对象不包含“文本”的定义

8ehkhllq  于 2023-02-19  发布在  其他
关注(0)|答案(2)|浏览(107)

所以我刚学了Microsoft Visual。我试着用label和用事件处理器设置一个文本,但是我在this.lblText.Text行得到了一个错误,更确切地说是在"文本"中,有人能告诉我如何解决它吗?我对C#不是很了解
这是密码

namespace Praktikum1
{
    public partial class Form1 : Form
    {
        private object lblText;

        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.lblText.Text = "Belajar Pemrograman Visual C#";

        }
    }
}

误差是in = this.lblTextText

u7up0aaq

u7up0aaq1#

您可以看到在此处手动添加了lblText

private object lblText;

如果由窗体设计器生成,则将其声明为

private Label lblText;

你说是“模块”做的,我不知道那是什么意思。也不可能知道lblText是否真的是一个标签。
如果是的话,这就行了

((Label)this.lblText).Text = "Belajar Pemrograman Visual C#";

即将object转换为Label类型

lymnna71

lymnna712#

您应该先create the label,然后change its text

Label testLBL = new Label();
this.Controls.Add(testLBL);
testLBL.Text = "testLBL";

或者添加label to the form through the toolbox(来自设计器)并设置文本。

相关问题