Click here to Skip to main content
15,877,560 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a custom dialog that would modify the background and foreground of a control. This ColorPicker is set as "FixedDialog" mode. This custom dialog enables me to adjust the background with foreground then set richtextbox colors on the main app.

I may be missing something... I kept on getting R: 240, G: 240, B: 240.

What I have tried:

From the calling application:
C#
ColorPicker cp = new ColorPicker();

richtextbox2.BackColor = cp.BackColor;


in the custom dialog: the code looks like this:
C#
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 TerminalWindows
{
    public partial class ColorPicker : Form
    {
        public Color Color_Background { get; set; }
        public Color Color_Foreground { get; set; }

        int red = 0;
        int blue = 0;
        int green = 0;
        int brightness;

        public ColorPicker()
        {
            InitializeComponent();

            btn_01.Click += ColorButtonClick;
            btn_02.Click += ColorButtonClick;
            btn_04.Click += ColorButtonClick;
            btn_05.Click += ColorButtonClick;
            btn_06.Click += ColorButtonClick;
            btn_07.Click += ColorButtonClick;
            btn_08.Click += ColorButtonClick;
            btn_09.Click += ColorButtonClick;
            btn_10.Click += ColorButtonClick;
            btn_11.Click += ColorButtonClick;
            btn_12.Click += ColorButtonClick;
            btn_13.Click += ColorButtonClick;
            btn_14.Click += ColorButtonClick;
            btn_15.Click += ColorButtonClick;
            btn_16.Click += ColorButtonClick;
            trackbar_Blue.ValueChanged += TrackbarColorUpdate;
            trackbar_Red.ValueChanged += TrackbarColorUpdate;
            trackbar_Green.ValueChanged += TrackbarColorUpdate;
            trackbar_Brightness.ValueChanged += TrackbarColorUpdate;
            lbl_ColorResult.ForeColor = Color_Foreground;
            lbl_ColorResult.BackColor = Color_Background;
            ChangeTrackbarValue(BackColor);
        }

        private void Btn_01_MouseClick1(object sender, MouseEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void Btn_01_MouseClick(object sender, MouseEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void ReverseColor(object sender, EventArgs e)
        {
            lbl_ColorResult.BackColor = Color.FromArgb(lbl_ColorResult.BackColor.ToArgb() ^ 0xffffff);
            lbl_ColorResult.ForeColor = Color.FromArgb(lbl_ColorResult.ForeColor.ToArgb() ^ 0xffffff);

            if (rb_Background.Checked)
                ChangeTrackbarValue(lbl_ColorResult.BackColor);
            else
                ChangeTrackbarValue(lbl_ColorResult.ForeColor);
        }

        private void rb_Background_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_Background.Checked)
            {
                ChangeTrackbarValue(Color_Background);
            }
        }

        private void rb_Foreground_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_Foreground.Checked)
            {
                ChangeTrackbarValue(Color_Foreground);
            }
            
        }

        private void ColorButtonClick(object sender, EventArgs e)
        {
            var button = sender as Button;

            ChangeTrackbarValue(button.BackColor);
        }

        private void UpdateColors()
        {
            
            red = trackbar_Red.Value;
            green = trackbar_Green.Value;
            blue = trackbar_Blue.Value;
            brightness = trackbar_Brightness.Value;
            lbl_Blue.Text = blue.ToString();
            lbl_Red.Text = red.ToString();
            lbl_Brightness.Text = brightness.ToString();
            lbl_Green.Text = green.ToString();

            if (rb_Background.Checked)
                lbl_ColorResult.BackColor = Color.FromArgb(red, green, blue);
            else if (rb_Foreground.Checked)
                lbl_ColorResult.ForeColor = Color.FromArgb(red, green, blue);
            
            Color_Background = lbl_ColorResult.BackColor;
            Color_Foreground = lbl_ColorResult.ForeColor;
        }

        private void TrackbarColorUpdate(object sender, EventArgs e)
        {
            UpdateColors();
        }

        private void ChangeTrackbarValue(Color color)
        {
            trackbar_Green.Value = color.G;
            trackbar_Red.Value = color.R;
            trackbar_Blue.Value = color.B;
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Good bye");
        }
    }
}
Posted
Updated 12-Jun-23 12:52pm

The most obvious thing that I can see is that you are updating the property Color_Background however you are retrieving the form's own BackColor.

You need to change this:
C#
richtextbox2.BackColor = cp.BackColor;

To:
C#
richtextbox2.BackColor = cp.Color_Background ;

Also, this may also need changing:
C#
lbl_ColorResult.ForeColor = Color_Foreground;
lbl_ColorResult.BackColor = Color_Background;
ChangeTrackbarValue(BackColor);

To:
C#
lbl_ColorResult.ForeColor = Color_Foreground;
lbl_ColorResult.BackColor = Color_Background;
ChangeTrackbarValue(lbl_ColorResult.BackColor);
 
Share this answer
 
v2
Ahh... misssed that on the first section of change... I had it refactored and missed that... Thanks!
 
Share this answer
 
Comments
CHill60 13-Jun-23 3:04am    
If you want to pass a comment on a post then use the "Have a Question or Comment?" link next to it. That way the member will actually be notified of your response.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900