C# 无法以编程方式添加自定义控件
我已经创建了一个自定义控件,希望能够在运行时添加它,但它不起作用。我可以通过设计视图添加它,效果很好。下面是我添加自定义控件的代码 pluginControl按钮.csC# 无法以编程方式添加自定义控件,c#,forms,winforms,linq,custom-controls,C#,Forms,Winforms,Linq,Custom Controls,我已经创建了一个自定义控件,希望能够在运行时添加它,但它不起作用。我可以通过设计视图添加它,效果很好。下面是我添加自定义控件的代码 pluginControl按钮.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using Sys
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace customControlLib
{
public partial class PluginControlButton : Control
{
private Color color1 = Color.LightGray;
private Color color2 = Color.Black;
private Color color3 = Color.White;
private Color colorU = Color.Yellow;
private int cpSize = 70;
private Image backgroundImage;
/// <summary>
/// The image for the plugin
/// </summary>
[Category("PluginControlSettings")]
public Image PluginImage
{
get { return backgroundImage; }
set { backgroundImage = value; Invalidate(); }
}
/// <summary>
/// The size of the component
/// </summary>
[Category("PluginControlSettings")]
public int ComponentSize
{
get { return cpSize; }
set
{
if (value == cpSize)
return;
cpSize = value;
cpSize = value;
Invalidate();
}
}
/// <summary>
/// The color of the out ring
/// </summary>
[Category("PluginControlSettings")]
public Color ProcessColor
{
get { return color1; }
set { color1 = value; Invalidate(); }
}
/// <summary>
/// The color of the inside circle
/// </summary>
[Category("PluginControlSettings")]
public Color InnerColor
{
get { return color2; }
set { color2 = value; Invalidate(); }
}
/// <summary>
/// The color of the update circle
/// </summary>
[Category("PluginControlSettings")]
public Color UpdateColor
{
get { return colorU; }
set { colorU = value; Invalidate(); }
}
/// <summary>
/// The color of the text
/// </summary>
[Category("PluginControlSettings")]
public Color textColor
{
get { return color3; }
set { color3 = value; Invalidate(); }
}
/// <summary>
/// Plugin text
/// </summary>
[Category("PluginControlSettings")]
public new string Text
{
get { return base.Text; }
set
{
if (value == base.Text)
return;
base.Text = value;
Invalidate();
}
}
public PluginControlButton()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
this.Width = cpSize;
this.Height = cpSize;
Graphics gfx = pe.Graphics;
Rectangle rc = ClientRectangle;
rc.Width -= 1;
rc.Height -= 1;
Graphics gfx2 = pe.Graphics;
Rectangle rc2 = ClientRectangle;
rc2.Width -= 17;
rc2.Height -= 17;
rc2.Location = new Point(8, 8);
Graphics gfxU = pe.Graphics;
Rectangle rcU = ClientRectangle;
rcU.Width -= 9;
rcU.Height -= 9;
rcU.Location = new Point(4, 4);
gfx.FillRectangle(new SolidBrush(Parent.BackColor), ClientRectangle);
gfx.FillEllipse(new SolidBrush(color1), rc);
gfx.DrawEllipse(new Pen(Color.Transparent, 1.0f), rc);
gfxU.FillEllipse(new SolidBrush(colorU), rcU);
gfxU.DrawEllipse(new Pen(Color.Transparent, 1.0f), rcU);
gfx2.FillEllipse(new SolidBrush(color2), rc2);
gfx2.DrawEllipse(new Pen(Color.Transparent, 1.0f), rc2);
Font fnt = new Font(base.Font.FontFamily, base.Font.Size, base.Font.Style, GraphicsUnit.Pixel);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
gfx.DrawString(Text, fnt, new SolidBrush(color3), new RectangleF((float)(rc2.Left), (float)(rc2.Top), (float)(rc2.Width), (float)(rc2.Height)), sf);
if (backgroundImage != null)
{
Graphics gfx3 = pe.Graphics;
Rectangle rc3 = ClientRectangle;
rc3.Width -= 35;
rc3.Height -= 35;
rc3.Location = new Point(17, 17);
gfx3.DrawImage(backgroundImage, rc3);
base.ForeColor = color2;
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
}
}
}
好的,现在可以了,我必须给组件添加一个尺寸。谢谢你的帮助
customControlLib.PluginControlButton pbc = new customControlLib.PluginControlButton()
{
ComponentSize = 70,
InnerColor = Color.Black,
Name = "TEST",
PluginImage = null,
ProcessColor = System.Drawing.Color.LightGray,
Size = new System.Drawing.Size(70, 70),
Text = "Test",
textColor = System.Drawing.Color.White,
UpdateColor = System.Drawing.Color.Yellow
};
运行时会出现什么错误?是否为控件指定了位置和大小?不要在绘画事件中调整画布的大小-你只在那个事件中画画,没有别的。我试着给它一个位置和大小。我没有得到任何错误,没有显示问题是什么。表单加载,但控件不会加载。当我给它一个大小时,代码对我有效。加载事件是否已连接?我可能会将您的代码移到InitializeComponent行下的构造函数中。@JW12689在创建pbc.Visible=true后尝试调用它。
customControlLib.PluginControlButton pbc = new customControlLib.PluginControlButton()
{
ComponentSize = 70,
InnerColor = Color.Black,
Name = "TEST",
PluginImage = null,
ProcessColor = System.Drawing.Color.LightGray,
Size = new System.Drawing.Size(70, 70),
Text = "Test",
textColor = System.Drawing.Color.White,
UpdateColor = System.Drawing.Color.Yellow
};