无法使用我的自定义userControl保存表单

时间:2019-11-21 10:06:31

标签: c# windows forms save element

创建userControl并以Windows窗体建立它之后,该窗体无法再保存。当我按Ctrl + S或“保存”按钮没有任何反应时,表单文件名上的小*符号表示该文件未保存。此外,我的表单还包含颜色的属性。当我更改此设置时,颜色不会更改,属性窗口似乎也不再响应。

自定义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();
        }

    }
}

我首先想到的可能是因为userControll在另一个项目中并且仅被链接。但是,即使在主项目中实现了userControll之后,仍然出现了上述问题。所以我撤消了。

enter image description here

我不知道我在做什么错。非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

解决了!

问题出在pictureBoxBar_Paint()事件上。从userControl中删除它后,一切都会运行。