简体   繁体   English

无法在文件代码中声明 UserControl

[英]Cannot declare a UserControl in file code

I have had a problem.我遇到了问题。 I create an UserControl and I save it, it appears in ToolBox.我创建了一个 UserControl 并保存它,它出现在工具箱中。 I can drop it from ToolBox normally.我可以正常地将它从 ToolBox 中删除。 However, I can not declare it as a variable in the file code.但是,我不能在文件代码中将其声明为变量。 Can you help me, please?你能帮我吗? And it is file code of UserControl并且是UserControl的文件代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Music_Player_Project_IT008N13.Controls;

namespace Music_Player_Project_IT008N13.Music_design_User_Control
{
    public partial class LocationPanel : UserControl
    {
        public LocationPanel()
        {
            InitializeComponent();
        }
        public delegate void DoEvent(string maSo, string tenSV, string khoa, string diemTB);
        public event DoEvent RefeshDgv;
        private void btnDelete_Click(object sender, EventArgs e)
        {
            
        }
    }
}

When you "drop it from the ToolBox normally", the Designer generates a member declaration for a new user control and also adds it to the forms Control collection.当您“正常地将其从工具箱中删除”时,设计器会为新的用户控件生成一个成员声明,并将其添加到 forms Control集合中。 As I understand it, your question states that you would like to declare your user control variable in code .据我了解,您的问题表明您想在代码中声明您的用户控制变量。 If this is the goal, we have to do the same thing that the Designer would do and add it to the Controls collection of the container you want to put it in.如果这是目标,我们必须做与设计器相同的事情,并将其添加到您要放入的容器的Controls集合中。


Example例子

Add a FlowLayoutPanel using code, then add 3 CustomUserControls to it also in code.使用代码添加 FlowLayoutPanel,然后在代码中添加 3 个 CustomUserControls。

截屏

public partial class MainForm : Form
{
    public MainForm() => InitializeComponent();
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        // Declare a flow layout panel in code.
        // Add it to the main form control collection
        var flowLayoutPanel = new FlowLayoutPanel
        {
            Name = "flowLayoutPanel",
            Dock = DockStyle.Fill,
        };
        this.Controls.Add(flowLayoutPanel);

        for (char c = 'A'; c < 'D'; c++)
        {
            var userControl = new CustomUserControl
            {
                Name = $"userControl{c}",  // No space, start with lowercase
                Text = $"UserControl {c}", // Visible name
                Margin = new Padding(10, 2, 10, 2),
                Width = flowLayoutPanel.Width - 20,
            };
            flowLayoutPanel.Controls.Add(userControl);
        }

        // T E S T
        CustomUserControl? loopback = GetUserControl("userControlA");
        Debug.Assert(loopback != null, "Expecting to retrieve user control by name");
    }

Then, to use the control, retrieve it from the Controls collection by name.然后,要使用该控件,请按名称从 Controls 集合中检索它。

    CustomUserControl? GetUserControl(string name)
    {
        var layoutPanel = Controls["flowLayoutPanel"] as FlowLayoutPanel;
        return layoutPanel.Controls[name] as CustomUserControl;
    }
}

Where:在哪里:

public partial class CustomUserControl : UserControl
{
    public CustomUserControl() => InitializeComponent();
    public new string Text
    {
        get => label1.Text; 
        set => label1.Text = value;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM