c#-UserControl不';如果我在代码中动态添加了一些控件,则不会出现在winform上
您好,我已经为windows窗体C创建了一个userControl,我在代码中动态添加了一些面板,在运行窗体后,我的控件在窗体中没有出现,我从代码中删除了添加的控件,然后该控件出现了,有人能帮我吗c#-UserControl不';如果我在代码中动态添加了一些控件,则不会出现在winform上,c#,winforms,visual-studio-2008,user-controls,C#,Winforms,Visual Studio 2008,User Controls,您好,我已经为windows窗体C创建了一个userControl,我在代码中动态添加了一些面板,在运行窗体后,我的控件在窗体中没有出现,我从代码中删除了添加的控件,然后该控件出现了,有人能帮我吗 public partial class Schedual : UserControl { int days; public int Days { get { return days; } set
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);
}
}
公共部分类Schedual:UserControl
{
国际日;
公众整数日
{
获取{返回天数;}
设置
{
天数=价值;
改变=正确;
这个。刷新();
}
}
整数周期;
公共整数周期
{
获取{返回周期;}
设置
{
周期=价值;
改变=正确;
这个。刷新();
}
}
刷子;
布尔变换;
名单小组;
公共服务
{
初始化组件();
这1.5天=1;
这个周期=1;
刷子=刷子。白色;
改变=错误;
面板=新列表();
InitFirstPanel();
}
///
///初始化板上的第一个面板
///
private void InitFirstPanel()
{
var p=新面板();
p、 大小=这个。大小;
p、 位置=此位置;
AddPanels(p);
}
///
///将给定面板添加到面板列表中
///
///通缉小组
专用面板(面板)
{
面板。添加(面板);
this.Controls.Add(panel);//如果我删除了它,那么该控件将工作
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
图形g=e.图形;
g、 抽绳(“Yaser”,新字体(“Arial”,20.5f),画笔。紫色,新点F(200150));
基础漆(e);
}
PaintBackground上受保护的覆盖无效(PaintEventArgs e)
{
图形g=e.图形;
var h=此高度/天;
var w=此宽度/周期;
g、 清晰(颜色:白色);
g、 FillRectangle(笔刷.White,新矩形(0,0,this.Width,this.Height));
如果(更改)
面板。清除();
对于(int i=0;i这显示良好,问题是您将p.Size=This.Size;
设置在InitFirstPanel
的内部,这意味着您的灰色面板将在您将其添加到Controls
后覆盖整个用户控件。尝试设置不同的大小,您会很好:)
我没有这样做,它仍然不工作,为了确保我删除了InitFirstPanel()函数,但我确实将面板的颜色更改为透明,控件开始抛出异常(创建窗口句柄时出错)将其添加到控件时List@YaserJaradeh例外情况很明显,因为删除InitFirstPanel后,您的面板将不再初始化。将颜色更改为透明将无法按预期工作,因此只需删除一行p.Size=this.Size;
p.Size = new Size(50,50);