Winforms 为什么不显示以编程方式添加的控件?

Winforms 为什么不显示以编程方式添加的控件?,winforms,controls,Winforms,Controls,有人知道为什么表单上没有显示这些控件吗?如果我通过design添加具有相同属性的控件,那么一切都可以正常工作,但是如果我在表单构造函数中使用来自设计器的相同代码,只有其他名称,那么一切都不起作用 private void CreatePlayingTab() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceMana

有人知道为什么表单上没有显示这些控件吗?如果我通过design添加具有相同属性的控件,那么一切都可以正常工作,但是如果我在表单构造函数中使用来自设计器的相同代码,只有其他名称,那么一切都不起作用

private void CreatePlayingTab()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));

        // 
        // bunifuCustomLabel_Titel
        // 
        BunifuCustomLabel bunifuCustomLabel_Titel = new BunifuCustomLabel();
        bunifuCustomLabel_Titel.AutoSize = true;
        bunifuCustomLabel_Titel.Font = new Font("Century Gothic", 11.25F, FontStyle.Bold, GraphicsUnit.Point, 0);
        bunifuCustomLabel_Titel.ForeColor = Color.FromArgb(224, 224, 224);
        bunifuCustomLabel_Titel.Location = new Point(19, 30);
        bunifuCustomLabel_Titel.Name = "bunifuCustomLabel_Titel";
        bunifuCustomLabel_Titel.Size = new Size(153, 18);
        bunifuCustomLabel_Titel.Text = "#001 Wer ist Naruto?";

        // 
        // bunifuCustomLabel_Interpret
        // 
        BunifuCustomLabel bunifuCustomLabel_Interpret = new BunifuCustomLabel();
        bunifuCustomLabel_Interpret.AutoSize = true;
        bunifuCustomLabel_Interpret.Font = new Font("Century Gothic", 11.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
        bunifuCustomLabel_Interpret.ForeColor = Color.FromArgb(224, 224, 224);
        bunifuCustomLabel_Interpret.Location = new Point(20, 52);
        bunifuCustomLabel_Interpret.Name = "bunifuCustomLabel_Interpret";
        bunifuCustomLabel_Interpret.Size = new Size(98, 20);
        bunifuCustomLabel_Interpret.Text = "Studio Tokyo";

        // 
        // windowsMediaPlayer
        // 
        AxWindowsMediaPlayer windowsMediaPlayer = new AxWindowsMediaPlayer();
        windowsMediaPlayer.Dock = DockStyle.Bottom;
        windowsMediaPlayer.Enabled = true;
        windowsMediaPlayer.Location = new Point(0, 117);
        windowsMediaPlayer.Name = "windowsMediaPlayer";
        windowsMediaPlayer.OcxState = ((AxHost.State)(resources.GetObject("mediaPlayer.OcxState")));
        windowsMediaPlayer.Size = new Size(912, 513);
        windowsMediaPlayer.uiMode = "None";

        // 
        // panel_currentlyPlaying
        //
        Panel panel_currentlyPlaying = new Panel();
        panel_currentlyPlaying.Controls.Add(windowsMediaPlayer);
        panel_currentlyPlaying.Controls.Add(bunifuCustomLabel_Titel);
        panel_currentlyPlaying.Controls.Add(bunifuCustomLabel_Interpret);
        panel_currentlyPlaying.Dock = DockStyle.Fill;
        panel_currentlyPlaying.Location = new Point(0, 100);
        panel_currentlyPlaying.Name = "panel_Playing";
        panel_currentlyPlaying.Size = new Size(912, 630);

        panel_Media.Controls.Add(panel_currentlyPlaying);
    }

我想你可能忘了在表单中添加控件


使用此.Controls.Add(控制项)

可能
panel\u Media
未添加到窗体或不可见,或超出窗体边界,我们无法在代码中看到。您正在将“mediaPlayer”添加到panel\u currently playing。。。但是您创建的AxWindowsMediaPlayer实例名为“windowsMediaPlayer”。是的,这是一个错误。现在更改了,只有mediaPlayer可见,但标签不可见。我刷新了问题,还有其他人有想法吗?我通过设计添加了panel_Media,因此算法在InitialiseComponents方法中创建了它,因此它已添加到表单中。如果我现在向panel_Media添加控件,它也应该添加到表单中。其他人有什么想法吗?