简体   繁体   English

无法以编程方式添加自定义控件

[英]Custom control not able to be added programmatically

I have created a custom control and want to be able to add it at run time but it is not working. 我已经创建了一个自定义控件,并希望能够在运行时添加它,但是它不起作用。 I can add it through design view and it works fine. 我可以通过设计视图添加它,并且效果很好。 My code is below for the custom control and where I am adding it. 我的代码在以下自定义控件及其添加位置。

PluginControlButton.cs PluginControlButton.cs

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)
        {
        }
    }
}

Form1.cs Form1.cs的

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;
using customControlLib;

namespace GlassSquid2
{
    public partial class GlassSquidControl : Form
    {
        public GlassSquidControl()
        {
            InitializeComponent();

        }

        private void GlassSquidControl_Load(object sender, EventArgs e)
        {
            PluginControlButton pbc = new PluginControlButton();

            this.Controls.Add(pbc);
        }
    }
}

Ok, it is working now, I had to add a size to the component. 好的,现在可以正常工作了,我必须为组件添加一个大小。 Thanks for the help. 谢谢您的帮助。

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
        };

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

相关问题 是否可以通过编程方式添加到面板的自定义控件中访问和更改值? - Is there a way to access and change a value in a custom control that was added, programmatically, to a panel? 如何使用UpdatePanel以编程方式添加到页面的方式创建和使用自定义控件 - How to create and use custom control with UpdatePanel added to the page programmatically 尝试从设计类中添加以编程方式在设计时添加到窗体中的winforms控件 - Trying to add a winforms control added to form in design time programmatically from a custom class 以编程方式添加了用于自定义列表框的自动换行 - Wordwrap for Custom ListBoxes Added Programmatically 添加到“自定义控件”时触发UpdatePanel消失 - Trigger for UpdatePanel disappearing when added to a “custom control” 在自定义文本框控件中添加了两次属性 - Attributes added twice in custom textbox control 从动态添加的自定义控件访问数据 - accessing data from a dynamically added custom control 检查是否已将自定义控件添加到表单中 - Checking if custom control is already added to form 无法访问以编程方式添加的控件(C#) - Unable to access a programmatically-added control (C#) 从C#中的网格中删除以编程方式添加的控件? - Deleting a programmatically added control from a Grid in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM