将元素(Label)添加到表单中的面板 [英] Adding a element(Label) to a panel in a form

查看:111
本文介绍了将元素(Label)添加到表单中的面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 PanelName添加文本视图。 Controls.Add( LabelName)它在一个案例中有效,但在其他情况下无效。


案例1:我创建了一个TabControl和一个标签页,然后动态添加了另一个 带有面板且包含标签的标签页。



案例2 :当我使用给定代码动态添加一个面板(已经创建)主tabcontrol时,它无法正常工作。




public Form0()

        {

           的InitializeComponent();                  //////// 案例2

            //顶部面板

            Panel panelTop = new Panel();           

            panelTop.Dock = DockStyle.Top;

            panelTop.BackColor = Color.Yellow;

            tabPage1.Controls.Add(panelTop);

            //标题

           标签头=新标签();

            Head.Name =" LAMDA";

            Head.AutoSize = true;

            //Head.Anchor = AnchorStyles.Top& AnchorStyles.Right& AnchorStyles.Left;

            // Head.ForeColor = Color.Black;

            // Head.Size = new Size(20,10);

            // Head.Location = new Point(20,20);

            panelTop.Controls.Add(Head);

            Head.Parent = panelTop;

           

            // bottom

            // left

            //右边
            //填写¥b $ b            InitializeProfile();

        }¥b $ b        void InitializeProfile()

        {

$
            createTabProfile();
$


        }¥b $ b        void createTabProfile()                                                  ///////// CASE
1


        {    //个人简介attribs                  

            TabPage tab1 = new TabPage(" Profile");

            tab1.BackgroundImage = Image.FromFile(" .\\BackTab.png");

            tab1.BackgroundImageLayout = ImageLayout.Center;

            tab1.BackColor = Color.White;

            tabControl1.TabPages.Add(tab1);
$


$
            //面板创建主体$
            Panel mainPanel = new Panel();

            mainPanel.Dock = DockStyle.Fill;

            mainPanel.BackColor = DefaultBackColor;

            tab1.Controls.Add(mainPanel);

            

            //名称标签问候语部分¥b $ b           标签NameLabel =新标签();

            NameLabel.Text =" NAME"; $
            NameLabel.Location = new Point(210,0);

            mainPanel.Controls.Add(NameLabel);

            // DP

            PictureBox DP =新PictureBox();

            DP.Image = Image.FromFile(@" .\\NarutoDP.png");

            DP.Size =新尺寸(150,150);

            DP.Name =" DP";

            DP.BorderStyle = BorderStyle.FixedSingle;

            DP.Location =新点(0,0);

            DP.Visible = true;

            mainPanel.Controls.Add(DP);

            DP.Parent = mainPanel;

            // tab1.Controls.Add(DP);



$
        }

解决方案

如果将代码从表单构造函数移动到Load或Shown事件,它是否有效?

I was trying to add a text view using PanelName.Controls.Add(LabelName) it worked in one case but doesnt working other.

Case 1: I created a TabControl and a tabpage then dynamically added another  tabpage having a panel and containing label worked.

Case 2: When I added a panel dynamically to (already created) main tabcontrol using given code it doen not worked.

public Form0()
        {
            InitializeComponent();                  ////////CASE 2
            //Top panel
            Panel panelTop = new Panel();           
            panelTop.Dock = DockStyle.Top;
           panelTop.BackColor = Color.Yellow;
            tabPage1.Controls.Add(panelTop);
            //HEading
            Label Head = new Label();
            Head.Name = "LAMDA";
            Head.AutoSize = true;
            //Head.Anchor = AnchorStyles.Top & AnchorStyles.Right & AnchorStyles.Left;
           // Head.ForeColor = Color.Black;
           // Head.Size = new Size(20, 10);
           // Head.Location = new Point(20,20);
            panelTop.Controls.Add(Head);
            Head.Parent = panelTop;
           
            //bottom
            //left
            // right
            //fill
            InitializeProfile();
        }
        void InitializeProfile()
        {

            createTabProfile();

        }
        void createTabProfile()                                                 /////////CASE 1
        {   //Profile attribs                  
            TabPage tab1 = new TabPage("Profile");
            tab1.BackgroundImage = Image.FromFile(".\\BackTab.png");
            tab1.BackgroundImageLayout = ImageLayout.Center;
            tab1.BackColor = Color.White;
            tabControl1.TabPages.Add(tab1);


            //Panel creating main
            Panel mainPanel = new Panel();
            mainPanel.Dock = DockStyle.Fill;
            mainPanel.BackColor = DefaultBackColor;
            tab1.Controls.Add(mainPanel);
            
            //Name Label Greeting part
            Label NameLabel = new Label();
            NameLabel.Text = "NAME";
           NameLabel.Location = new Point(210,0);
           mainPanel.Controls.Add(NameLabel);
            //DP
            PictureBox DP = new PictureBox();
            DP.Image = Image.FromFile(@".\\NarutoDP.png");
            DP.Size = new Size(150, 150);
            DP.Name = "DP";
            DP.BorderStyle = BorderStyle.FixedSingle;
            DP.Location = new Point(0, 0);
            DP.Visible = true;
            mainPanel.Controls.Add(DP);
            DP.Parent = mainPanel;
           // tab1.Controls.Add(DP);


        }

解决方案

If you move the code from the form constructor to either Load or Shown event does it work?


这篇关于将元素(Label)添加到表单中的面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆