4
Answers

Setting SolidBrush color with a ColorDialog

James

James

15y
10.3k
1
Hey

I am new to c# and need some assistance with something I am trying to do.  I want to use a Color Dialog box to let the user change the color of a brush when painting a grid.  I have used a button, so when it is clicked on the form, the Color Dialog loads up.  However when I select a new color and then paint on the grid, the color hasn't changed.  I have put the code below.  Could anyone explain where I am going wrong, or what I need to do to make sure the grid is painted witht the newly selected color please.

         private void button2_Click(object sender, EventArgs e)
{
SolidBrush myBrush = new SolidBrush(Color.Empty);
ColorDialog diag = new ColorDialog();
diag.ShowDialog();
pictureBox1.Refresh();
Refresh();

}
If anyone can give me some assistance on how I can achieve what I want to do then I would be very grateful.  I just need to understand what code I need to add or where I have gone wrong at the minute.  Really appreciate anyones times.

J

Answers (4)
0
Nilanka Dharmadasa
NA 5.2k 0 15y
Hi friend,


The reason is though you set the color to the brush, you don't have access to that brush outside button click event.

So change your code as follows.


        SolidBrush myBrush;//Define the varaible outside the methods.
       
        public Form1()
        {
            InitializeComponent();
            myBrush = new SolidBrush(Color.Black);//You set a default color as black.
           
           
pictureBox1.Paint += new PaintEventHandler(f1_paint);//Your picture box paint method

            //You can write your other code here, if you want.
        }

        private void f1_paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Graphics formGraphics = this.CreateGraphics();
            formGraphics.FillEllipse(myBrush, 20, 20, 40, 40);
            //You can write other drawing code as you like.
            //But use 'myBrush ' to draw.
            
        }


        private void button2_Click(object sender, EventArgs e)
        {          

            ColorDialog clrdlg = new ColorDialog();

            if (clrdlg.ShowDialog() == DialogResult.OK)
            {

                myBrush = new SolidBrush(clrdlg.Color);
                pictureBox1.Refresh();
            }
        }





If you find this answer helpful, please do not forget to  check 'Do you like this answer' checkbox.

0
Kirtan Patel
NA 35k 2.8m 15y
Friend what are you doin with that PictureBox because after changing... the color of Brush you need to DrawSomthing with that brush ...then you can see color changed or not .
0
James
NA 3 0 15y
Hi Kirtan

Thaks very much for replying to my question with the code.  I added this code but unfortunately it didn't work. when I click on the button, the color dialog window opens, i select a new color for the brush, paint on the grid again but the color has not changed.  I also tried this code, but it didnt work.
 private void button2_Click(object sender, EventArgs e)
{

SolidBrush myBrush;

ColorDialog clrdlg = new ColorDialog();

if (clrdlg.ShowDialog() == DialogResult.OK)
{

myBrush = new SolidBrush(clrdlg.Color);

}
}
I used myBrush as this is what I am using earlier in the code as my brush object. I also tried this code but it didn't work.
 private void button2_Click(object sender, EventArgs e)
{

SolidBrush myBrush;

ColorDialog clrdlg = new ColorDialog();

if (clrdlg.ShowDialog() == DialogResult.OK)
{

myBrush = new SolidBrush(clrdlg.Color);
pictureBox1.Refresh();

}
}
Do you know why this isn't working?  Do you have any suggestions on how I can resolve this issue?  Thanks for your time.

Kind regards
0
Kirtan Patel
NA 35k 2.8m 15y
Friend , Here is Code you need .!!

 private void button1_Click(object sender, EventArgs e)

        {

            SolidBrush brush;

            ColorDialog clrdlg = new ColorDialog();

            if (clrdlg.ShowDialog() == DialogResult.OK)

            {

                brush = new SolidBrush(clrdlg.Color);

            }

 

            //Now use the Brush where you need

 

            // Thats it

 

        }