简体   繁体   中英

Can't save forms with my custom userControl in it

After I created a userControl and established it in a Windows form, this form could no longer be saved. When I press Ctrl + S or the Save button nothing happens, the small * on the form file name suggests that it was not saved. Also, my form contains a property for a color. When I change this, the color doesn't change and the property window doesn't seem to respond anymore.

Code of custom userControl

namespace FlowGui
{

    public partial class FlowProgressBar: UserControl
    {

        // Public Variables with Setters

        private int maxValue;
        [Bindable(true), Category("Behavior")]
        public int Maximum
        {
            get
            {
                return maxValue;
            }
            set
            {
                maxValue = value;
                DrawBar();
            }
        }

        private int minValue;
        [Bindable(true), Category("Behavior")]
        public int Minimum
        {
            get
            {
                return minValue;
            }
            set
            {
                minValue = value;
                DrawBar();
            }
        }

        private int currentValue;
        [Bindable(true), Category("Behavior")]
        public int Value
        {
            get
            {
                return currentValue;
            }
            set
            {
                currentValue = value;
                DrawBar();
            }
        }

        private Color barColor;
        [Bindable(true), Category("Appearance")]
        public Color BarColor
        {
            get
            {
                return barColor;
            }
            set
            {
                barColor = value;
                CreateBarImage();
                DrawBar();
            }
        }

        // private Values for Drawing

        private Image canvas;
        private Graphics gfx;

        private Bitmap barImage;

        public FlowProgressBar()
        {
            InitializeComponent();
        }

        // Load
        private void FlowProgressBar_Load(object sender, EventArgs e)
        {

            maxValue = 100;
            minValue = 0;
            currentValue = 0;

            barColor = Color.LightBlue;

            // Init Graphic
            InitGraphics();
            CreateBarImage();
            DrawBar();
        }

        private void CreateBarImage()
        {
            barImage = new Bitmap(10, 50);

            Bitmap currentBarImage = Properties.Resources.BarImage_Basic;

            for(int x=0; x<10; x++)
            {
                for (int y = 0; y < 50; y++)
                {

                    Color barPreColor = currentBarImage.GetPixel(x, y);

                    int r = barPreColor.R * barColor.R / 255;
                    int g = barPreColor.G * barColor.G / 255;
                    int b = barPreColor.B * barColor.B / 255;

                    Color pixelColor = Color.FromArgb(r,g,b);
                    currentBarImage.SetPixel(x, y, pixelColor);

                }
            }

        }

        private void InitGraphics()
        {
            canvas = new Bitmap(pictureBoxBar.Width, pictureBoxBar.Height);
            gfx = Graphics.FromImage(canvas);
        }

        private void DrawBar()
        {
            int barWidth  = pictureBoxBar.Width;
            int barHeight = pictureBoxBar.Height;

            Image bgImage = Properties.Resources.BarBG_Raised_Grey;

            gfx.DrawImage(bgImage,-200,0,barWidth+400,barHeight);

            gfx.DrawImage(barImage, 0, 0, 100, barHeight);

            pictureBoxBar.Image = canvas;

        }

        // Behaviour

        private void FlowProgressBar_SizeChanged(object sender, EventArgs e)
        {
            InitGraphics();
            DrawBar();
        }

        private void pictureBoxBar_Paint(object sender, PaintEventArgs e)
        {
            InitGraphics();
            DrawBar();
        }

    }
}

My first thought was that it could be because the userControll is in another project and only linked. But even after I implemented the userControll in the main project, my described problems occurred. So I undo this.

在此处输入图像描述

I have no idea what I'm doing wrong. Thank you very much for your help.

Solved!

The problem was the pictureBoxBar_Paint() event. After I removed it from the userControl everything runs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM