簡體   English   中英

C# UserControl 在按鈕單擊時生成 label

[英]C# UserControl generate label on button click

我知道如何使用設計文件上的工具箱創建標簽,但我希望每次在我的 UserControl 上單擊按鈕時生成 label 並且不完全確定如何執行此操作 - 以下是單擊按鈕時調用的方法:

        private void button1_Click(object sender, EventArgs e)
        {

        }

我將感謝該方法中的代碼片段,該代碼片段將生成 label,其中包含我選擇的文本、位置、工具提示。

您可以在 windows forms 應用程序中輕松完成。 以下代碼片段僅在特定位置生成label ,其中包含硬編碼文本和一些基本屬性設置。 您可以根據您的要求對其進行修改,例如您可以相應地更改label name, location, text 希望這對您有所幫助。

private void button1_Click(object sender, EventArgs e)
        {
            Label lblnew = new System.Windows.Forms.Label();
            lblnew.Location = new Point(50, 50);
            lblnew.Text = "My Label"; //Text can be dynamically assigned e.g From some text box
            lblnew.AutoSize = true;
            lblnew.BackColor = System.Drawing.Color.LightGray;
            lblnew.Font = new System.Drawing.Font("Microsoft JhengHei UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            //this refers to current form you can use your container according to requirement
            this.Controls.Add(lblnew);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM