简体   繁体   English

在C#中,如何使用numericupdown控件动态地更改Solidbrush颜色?

[英]In C#, how can I change a solidbrush color dynamically using numericupdown control?

Heres the code I'm using: 这是我正在使用的代码:

    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 Colors_Form
    {
        public partial class Form1 : Form
        {
            public int RedValue;
            public int GreenValue;
            public int BlueValue;
            public SolidBrush sb = new SolidBrush(Color.FromArgb(255, 0, 0, 0));
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                RedValue = (int)RedNumBox.Value;
                GreenValue = (int)GreenNumBox.Value;
                BlueValue = (int)BlueNumBox.Value;

                //SolidBrush sb = new SolidBrush(Color.FromArgb(255, RedValue, GreenValue, BlueValue ));
                g.FillRectangle(sb, 10, 150, 300, 200);
            }

            private void RedNumBox_ValueChanged(object sender, EventArgs e)
            {
                sb = new SolidBrush(Color.FromArgb(255, RedValue, GreenValue, BlueValue));


            }

            private void GreenNumBox_ValueChanged(object sender, EventArgs e)
            {
                GreenValue = (int)GreenNumBox.Value;
                sb = new SolidBrush(Color.FromArgb(255, RedValue, GreenValue, BlueValue));
            }

            private void BlueNumBox_ValueChanged(object sender, EventArgs e)
            {
                BlueValue = (int)GreenNumBox.Value;
                sb = new SolidBrush(Color.FromArgb(255, RedValue, GreenValue, BlueValue));
            }
        }
    }

What I'm trying to do is get the solid brush to change color when the 'valuechanged' event is triggered. 我正在尝试做的事情是,当'valuechanged'事件触发时,使固体画笔改变颜色。 Unfortunately, I have been unsuccessful. 不幸的是,我一直没有成功。 I have tried variations on: 我尝试过以下变化:

    sb = sb.Color(Color.FromArgb(255,RedValue, GreenValue, BlueValue));

but that code fails as well. 但是该代码也会失败。 I placed the solid brush globally, so there could be access to it from any method created. 我将实心画笔全局放置,因此可以通过任何创建的方法对其进行访问。 I have looked everywhere and cannot find any color update instructions and all I see is 'new brush' and now, I'm unsure how to proceed. 我到处都看过,找不到任何颜色更新说明,我所看到的只是“新笔刷”,现在,我不确定如何继续。 I appreciate any assistance. 感谢您的协助。

You should move the brush into the paint event and remove the global variable. 您应该将画笔移到paint事件中并删除全局变量。 SolidBrush should be disposed of when its done. 完成后应将SolidBrush丢弃。 Better to do that in one place than all the change events. 比所有变更事件都集中在一个地方更好。

using (SolidBrush sb = new SolidBrush(Color.FromArgb(255, RedValue, GreenValue, BlueValue )))
        {
            g.FillRectangle(sb, 10, 150, 300, 200);
        }

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

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