Home > OS >  I can't use a Winforms Control created with a Class
I can't use a Winforms Control created with a Class

Time:12-10

I was looking for a Circular Picture Box for my app and I stumbled across this code (IT IS NOT MINE) and I've tried as many times as I could but I can't find any mistake. I have followed every step that was made in the tutorial for this Rounded Picture Box so it can't be a miscopy because it was working perfectly in the tutorial.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace New_Radio_Barcelona.Controls
{
 
        class RashiCircularPictureBox : PictureBox
        {
            private int border = 2;
            private Color colorBorder = Color.RoyalBlue;
            private Color colorBorder2 = Color.HotPink;
            private DashStyle borderstyle = DashStyle.Solid;
            private DashCap borderCap = DashCap.Flat;
            private float gradiant = 50f;

        public RashiCircularPictureBox()
        {
            this.Size = new Size(95, 95);
            this.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        public int Border
            {
                get
                {
                    return border;
                }

                set
                {
                    border = value;
                    this.Invalidate();
                }
            }

            public Color ColorBorder
            {
                get
                {
                    return colorBorder;
                }

                set
                {
                    colorBorder = value;
                    this.Invalidate();

                }
            }

            public Color ColorBorder2
            {
                get
                {
                    return colorBorder2;
                }

                set
                {
                    colorBorder2 = value;
                    this.Invalidate();

                }
            }

            public DashStyle Borderstyle
            {
                get
                {
                    return borderstyle;
                }

                set
                {
                    borderstyle = value;
                    this.Invalidate();

                }
            }

            public DashCap BorderCap
            {
                get
                {
                    return borderCap;
                }

                set
                {
                    borderCap = value;
                    this.Invalidate();

                }
            }

            public float Gradiant
            {
                get
                {
                    return gradiant;
                }

                set
                {
                    gradiant = value;
                    this.Invalidate();

                }
            }

            protected override void OnResize(EventArgs e)
            {
                base.OnResize(e);
                this.Size = new Size(this.Width, this.Width);
            }

            protected override void OnPaint(PaintEventArgs pe)
            {
                base.OnPaint(pe);

                var graphic = pe.Graphics;
                var rect = Rectangle.Inflate(this.ClientRectangle, -1, -1);
                var rectborder = Rectangle.Inflate(rect, -border, -border);
                var size = border > 0 ? border * 3 : 1;

                using (var bordercolorG = new LinearGradientBrush(rectborder, colorBorder, colorBorder2, gradiant))
                using (var path = new GraphicsPath())
                using (var pen = new Pen(this.Parent.BackColor, border))
                using (var penborder = new Pen(bordercolorG, size))
                {

                    graphic.SmoothingMode = SmoothingMode.AntiAlias;
                    penborder.DashStyle = borderstyle;
                    penborder.DashCap = borderCap;

                    path.AddEllipse(rect);

                    this.Region = new Region(path);

                    graphic.DrawEllipse(pen, rect);
                    if (border > 0)
                    {
                        graphic.DrawEllipse(penborder, rectborder);
                    }

                }

            }



        }
    }

I compile the project and then try to add it to the Design tab as shown in the tutorial. It says it could not be loaded. I was trying to understand what is not working properly but I still do not find the mistake. Some help plis? Another aspect to take into consideration is the fact that in class RashiCircularPictureBox : PictureBox puts 1 reference above the code and in public RashiCircularPictureBox() it says 0 references. It may be for this but I'm no expert on Classes and I'm stuck in this stupidity. if anyone could clear my mind about this issue I would be so grateful about it

CodePudding user response:

The designer in most versions of Visual Studio up until recently has been a 32-bit process. So if the control was built as 64-bit, it wouldn’t be able to load it at design-time, but VS would still be able to create 64-bit applications that can use the 64-bit control at runtime.

This means if you build your control as 32-bit or AnyCPU, it should solve the design-time loading problem.

The release notes of Visual Studio 2022 version 17.0.0 state that “devenv.exe is now 64-bit only”. I haven’t tried this myself, but it probably means you can now use 64-bit controls at design time with the newer versions of VS.

In all cases, AnyCPU should work.

  • Related