【问题标题】:Why are programmatically added controls not shown?为什么没有显示以编程方式添加的控件?
【发布时间】:2018-05-29 01:48:33
【问题描述】:

有谁知道为什么这些控件没有显示在表单上?如果我通过设计添加具有相同属性的控件,一切正常,但如果我在表单构造函数中使用来自设计器的相同代码,只有其他名称,则没有任何作用。

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);
    }

【问题讨论】:

  • 可能panel_Media没有添加到表单或不可见或超出表单边界,我们在代码中看不到。
  • 您正在将“mediaPlayer”添加到 panel_currentlyPlaying ...但您创建的 AxWindowsMediaPlayer 实例名为“windowsMediaPlayer”
  • 是的,这是一个错误。现在更改它只有 mediaPlayer 可见,但标签不可见。
  • 我刷新了问题
  • 还有其他人的想法吗?

标签: winforms controls


【解决方案1】:

我认为您可能忘记将控件添加到表单中。

使用 this.Controls.Add(Control Item);

【讨论】:

  • 我通过设计添加了 panel_Media,因此算法在 InitialiseComponents 方法中创建它,因此它已经添加到表单中。如果我现在向 panel_Media 添加一个控件,它也应该添加到表单中。
  • 还有其他人有什么想法吗?
猜你喜欢
  • 2013-12-02
  • 1970-01-01
  • 2017-01-09
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2015-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多