简体   繁体   English

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

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

Hello there i have created a userControl for windows form C# and i dynamicly added some panels in the code and after running the form My control is not appering in the form and i removed the added controls from the code then The Control Appear can someone help me please ?? 您好我已经为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);
        }
    }

This is showing fine, the problem is that you set p.Size = this.Size; 这显示没问题,问题是你设置了p.Size = this.Size; inside of InitFirstPanel which means your gray panel will be over the whole user control after you've added it to Controls . InitFirstPanel内部,这意味着在将灰色面板添加到Controls后,灰色面板将覆盖整个用户Controls Try setting different size and you will be fine :) 尝试设置不同的大小,你会没事的:)

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

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

相关问题 如果动态添加到Page.Load中,则UserControl不会出现在页面上 - UserControl doesn't appear on page if dynamically added in Page.Load 控件不显示是否在后台线程上运行(c#winform) - controls doesn't show if run on a background thread ( c# winform) C#winform:从每个选项卡页访问用户控件中的控件 - C# winform: Accessing controls in a usercontrol from every tab page 在winform c#.net应用程序中更改动态添加的控件的值,如何跟踪动态添加的控件 - Change values of dynamically added controls in winform c#.net application, how to track dynamically added controls 如何将控件移动到页面中动态添加的用户控件中? - How can I move controls into a dynamically added usercontrol into page? C# 的 Winform 控件 [关闭] - Winform controls for C# [closed] C#Winform:Winform和用户控件之间的交流 - C# winform: communation between winform and usercontrol C#WinForms,在运行时和控件中动态添加usercontrol是不可见的? - C# WinForms, Adding a usercontrol Dynamically during runtime and controls are invisible? 动态添加的事件处理程序在C#中无法按预期工作 - Dynamically added event handlers doesn't work as expected in C# 从WinForm删除动态添加的控件 - Removing dynamically added controls from WinForm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM