繁体   English   中英

c# - 如果我在代码中动态添加了一些控件,则UserControl不会出现在winform上

[英]c# - UserControl doesn't appear on winform if i dynamically added some controls in the code

您好我已经为Windows窗体C#创建了一个userControl,我动态地在代码中添加了一些面板,并在运行表单后我的控件没有在表单中显示,我从代码中删除了添加的控件然后控件出现可以有人帮助我请 ??

public partial class Schedual : UserControl
    {
        int days;

        public int Days
        {
            get { return days; }
            set
            {
                days = value;
                change = true;
                this.Refresh();
            }
        }

        int periods;

        public int Periods
        {
            get { return periods; }
            set
            {
                periods = value;
                change = true;
                this.Refresh();
            }
        }

        Brush brush;

        bool change;

        List<Panel> panels;

        public Schedual()
        {
            InitializeComponent();
            this.days = 1;
            this.periods = 1;
            brush = Brushes.White;
            change = false;
            panels = new List<Panel>();
            InitFirstPanel();
        }

        /// <summary>
        /// Initialize the first panel on the board
        /// </summary>
        private void InitFirstPanel()
        {
            var p = new Panel();
            p.Size = this.Size;
            p.Location = this.Location;
            AddPanels(p);
        }

        /// <summary>
        /// Adds a given panel to the list of panels
        /// </summary>
        /// <param name="panel">the wanted panel</param>
        private void AddPanels(Panel panel)
        {
            panels.Add(panel);
            this.Controls.Add(panel);  //if i removed this then the control work
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawString("Yaser", new Font("Arial", 20.5f), Brushes.Violet, new PointF(200, 150));
            base.OnPaint(e);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            var h = this.Height / days;
            var w = this.Width / periods;
            g.Clear(Color.White);
            g.FillRectangle(Brushes.White, new Rectangle(0,0,this.Width,this.Height));
            if (change)
                panels.Clear();
            for (int i = 0; i <= days; i++)
            {
                for (int j = 0; j <= periods; j++)
                {
                    g.FillRectangle(brush, j * w, i * h, w, h);
                    if (change)
                    {
                        AddPanel(j * w, i * h, w, h);
                    }
                    g.DrawLine(Pens.Black, 0, i * h, this.Width, i * h);
                    g.DrawLine(Pens.Black, j * w, 0, this.Height, j * w);
                    if (brush == Brushes.White)
                        brush = Brushes.YellowGreen;
                    else
                        brush = Brushes.White;
                }
            }
            change = false;
        }

        /// <summary>
        /// Create a new Panel and adds it to the list
        /// </summary>
        /// <param name="x">the x position of the panel</param>
        /// <param name="y">the y position of the panel</param>
        /// <param name="width">the width of the panel</param>
        /// <param name="height">the height position of the panel</param>
        private void AddPanel(int x, int y, int width, int height)
        {
            var panel = new Panel();
            panel.Location = new Point(x, y);
            panel.Size = new Size(width, height);
            AddPanels(panel);
        }
    }

这显示没问题,问题是你设置了p.Size = this.Size; InitFirstPanel内部,这意味着在将灰色面板添加到Controls后,灰色面板将覆盖整个用户Controls 尝试设置不同的大小,你会没事的:)

p.Size = new Size(50,50);

暂无
暂无

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

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