C# 创建自定义对象(两个对象的组合)

C# 创建自定义对象(两个对象的组合),c#,listbox,runtime,custom-controls,label,C#,Listbox,Runtime,Custom Controls,Label,hello创建自定义对象可能是一个广泛发表的主题,但我缺乏编码技能,这在实际实现我要做的事情时证明是有问题的 简而言之,我在运行时在flowpanelLayout中添加控件。现在只是列表框,代码运行良好。我想要一种方法来标记正在添加的列表框,我想没有比使用文本标签更好的方法了。我在想,创建某种自定义控件(如果可能的话)会很容易,它是一个列表框和一个文本标签,就像一个在另一个上面或其他什么的。通过这种方式,我可以在当前代码中添加新的自定义控件,并在一个动作中指定listbox属性和标签文本等 这就

hello创建自定义对象可能是一个广泛发表的主题,但我缺乏编码技能,这在实际实现我要做的事情时证明是有问题的

简而言之,我在运行时在flowpanelLayout中添加控件。现在只是列表框,代码运行良好。我想要一种方法来标记正在添加的列表框,我想没有比使用文本标签更好的方法了。我在想,创建某种自定义控件(如果可能的话)会很容易,它是一个列表框和一个文本标签,就像一个在另一个上面或其他什么的。通过这种方式,我可以在当前代码中添加新的自定义控件,并在一个动作中指定listbox属性和标签文本等

这就是我所想的,也许有更好的方法

我当前的listview创建代码:

public void addListView()
        {

            ListView newListView = new ListView();
            newListView.AllowDrop = true;
            newListView.DragDrop += listView_DragDrop;
            newListView.DragEnter += listView_DragEnter;
            newListView.MouseDoubleClick += listView_MouseDoubleClick;
            newListView.MouseDown += listView_MouseDown;
            newListView.DragOver += listView_DragOver;
            newListView.Width = 200;
            newListView.Height = 200;
            newListView.View = View.Tile;
            newListView.MultiSelect = false;

            flowPanel.Controls.Add(newListView);
            numWO++;

            numberofWOLabel.Text = numWO.ToString();
        }
也许实际最好的答案就是在这里添加一个文本标签,并定义一些设置坐标来放置它。让我知道你的想法


如果要使用自定义控件,请为我提供一些资源或示例-我将不胜感激。

以下是一个自定义用户控件: 只需设置TitleLabelText即可设置标题

[Category("Custom User Controls")]
public class ListBoxWithTitle : ListBox
{
    private Label titleLabel;
    public ListBoxWithTitle()
    {
        this.SizeChanged +=new EventHandler(SizeSet);
        this.LocationChanged +=new EventHandler(LocationSet);
        this.ParentChanged += new EventHandler(ParentSet);

    }
    public string TitleLabelText
    {
        get;
        set;
    }
    //Ensures the Size, Location and Parent have been set before adding text
    bool isSizeSet = false;
    bool isLocationSet = false;
    bool isParentSet = false;
    private void SizeSet(object sender, EventArgs e)
    {
        isSizeSet = true;
        if (isSizeSet && isLocationSet && isParentSet)
        {
            PositionLabel();
        }
    }
    private void LocationSet(object sender, EventArgs e)
    {
        isLocationSet = true;
        if (isSizeSet && isLocationSet && isParentSet)
        {
            PositionLabel();
        }
    }
    private void ParentSet(object sender, EventArgs e)
    {
        isParentSet = true;
        if (isSizeSet && isLocationSet && isParentSet)
        {
            PositionLabel();
        }
    }
    private void PositionLabel()
    {
        //Initializes text label
        titleLabel = new Label();
        //Positions the text 10 pixels below the Listbox.
        titleLabel.Location = new Point(this.Location.X, this.Location.Y + this.Size.Height + 10);
        titleLabel.AutoSize = true;
        titleLabel.Text = TitleLabelText;
        this.Parent.Controls.Add(titleLabel);
    }

}
示例用法:

    public Form1()
    {
        InitializeComponent();

        ListBoxWithTitle newitem = new ListBoxWithTitle();
        newitem.Size = new Size(200, 200);
        newitem.Location = new Point(20, 20);
        newitem.TitleLabelText = "Test";
        this.Controls.Add(newitem);
    }

以下是一个自定义用户控件,可以执行此操作: 只需设置TitleLabelText即可设置标题

[Category("Custom User Controls")]
public class ListBoxWithTitle : ListBox
{
    private Label titleLabel;
    public ListBoxWithTitle()
    {
        this.SizeChanged +=new EventHandler(SizeSet);
        this.LocationChanged +=new EventHandler(LocationSet);
        this.ParentChanged += new EventHandler(ParentSet);

    }
    public string TitleLabelText
    {
        get;
        set;
    }
    //Ensures the Size, Location and Parent have been set before adding text
    bool isSizeSet = false;
    bool isLocationSet = false;
    bool isParentSet = false;
    private void SizeSet(object sender, EventArgs e)
    {
        isSizeSet = true;
        if (isSizeSet && isLocationSet && isParentSet)
        {
            PositionLabel();
        }
    }
    private void LocationSet(object sender, EventArgs e)
    {
        isLocationSet = true;
        if (isSizeSet && isLocationSet && isParentSet)
        {
            PositionLabel();
        }
    }
    private void ParentSet(object sender, EventArgs e)
    {
        isParentSet = true;
        if (isSizeSet && isLocationSet && isParentSet)
        {
            PositionLabel();
        }
    }
    private void PositionLabel()
    {
        //Initializes text label
        titleLabel = new Label();
        //Positions the text 10 pixels below the Listbox.
        titleLabel.Location = new Point(this.Location.X, this.Location.Y + this.Size.Height + 10);
        titleLabel.AutoSize = true;
        titleLabel.Text = TitleLabelText;
        this.Parent.Controls.Add(titleLabel);
    }

}
示例用法:

    public Form1()
    {
        InitializeComponent();

        ListBoxWithTitle newitem = new ListBoxWithTitle();
        newitem.Size = new Size(200, 200);
        newitem.Location = new Point(20, 20);
        newitem.TitleLabelText = "Test";
        this.Controls.Add(newitem);
    }