【问题标题】:c# - UserControl doesn't appear on winform if i dynamically added some controls in the codec# - 如果我在代码中动态添加了一些控件,则用户控件不会出现在 winform 上
【发布时间】:2013-03-08 10:19:44
【问题描述】:

你好,我已经为 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);
        }
    }

【问题讨论】:

    标签: c# winforms visual-studio-2008 user-controls


    【解决方案1】:

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

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

    【讨论】:

    • 我没有,它仍然无法正常工作,并确保我删除了 InitFirstPanel() 函数,但我确实将面板的颜色更改为透明并且控件开始抛出异常(创建窗口句柄时出错) 将其添加到控件列表时
    • @YaserJaradeh 异常很明显,因为删除 InitFirstPanel 后,您的面板不再初始化。将颜色更改为透明不会像您期望的那样工作,因此只需删除一行 p.Size = this.Size;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 2015-03-30
    • 1970-01-01
    • 2012-02-11
    • 2012-12-10
    • 2010-12-07
    • 1970-01-01
    相关资源
    最近更新 更多