簡體   English   中英

C#在用戶控件上繪制鏈接標簽

[英]c# draw linklabel on usercontrol

我正在設計一個UserControl ,它將保存一個linklabel的列表。 像這樣:

public class ItemControl : LinkLabel {}

public class ItemsControl : UserControl
{
    private readonly List<ItemControl> items;

    public TaskBox()
    {
        this.items = new List<ItemControl>();
    }

    public List<ItemControl> Items
    {
        get { return this.items; }
    }
}

但是,一旦將它們添加到列表中,如何在UserControl上繪制它們? 另外,如何在代碼中向他們添加clicked事件?

您可以將FlowLayoutPanel控件放在ItemsControl並使用以下代碼向其繪制ItemControl

foreach (var item in this.Items)
{
      flowLayoutPanel1.Controls.Add(item);
      item.LinkClicked += item_LinkClicked;
}

void item_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
      MessageBox.Show(((item)sender).Name);
}

使用FlowLayaoutPanel是任意的,但是如果不使用FlowLayoutPanel ,則可以將每個LinkItem直接放在UserControl ,並且必須自己管理每個LinkItem位置。

暫無
暫無

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

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