Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I am creating an employee viewer application, i have multiples forms in my applications. I want users to have the option of changing the back color of the Main form to (Red, Green, Magenta and Blue) for example, and then back to its default color. The main form contains a combo box with the values in it like:

Default
Red
Green
Magenta
Blue

I want the form to change the back color when a user clicks each of these values. What is the best approach to do it? Thank you in advance.
Posted
Comments
[no name] 23-Jun-15 7:41am    
The best approach? Get the selected color from the combobox and change the form background color. So far, you have not told us what the problem could possibly be with the code you have written....
FranzBe 23-Jun-15 7:46am    
I would start this way:
1) Define a class "MyApplicationSettings"
2) put a property "FormBackgroundColor" there
3) define an event OnBackgroundColorChanged
4) raise this event in the setter part of the FormBackgroundColor Property
5) subscribe to this event in the OnLoad routine of each form
6) change the background color of the form in the corresponding event handler
7) use the SelectedIndexChanged event from your combobox to write the selected value to the FormBackgroundColor property

you should provide the code you have written so far and point out where the problem is you are struggeling with
Ralf Meier 23-Jun-15 8:25am    
@Franz:
You should deploy that as Solution ...
leandro2 23-Jun-15 8:44am    
Sorry guy I only started coding the solution now, as soon as I am done i will deploy it here..
leandro2 23-Jun-15 9:19am    
Hi guys, this is the solution i came up with at the moment...

private int _selectedOption;

public int SelectOption
{
get
{
return _selectedOption;
}
set
{
_selectedOption = value;
}
}
private void toolStripComboBox1_Click(object sender, EventArgs e)
{
if (toolStripComboBox1.Text == "Red")
{
SelectOption = toolStripComboBox1.SelectedIndex;
this.BackColor = System.Drawing.Color.Red;
}
else if (toolStripComboBox1.Text == "Blue")
{
this.BackColor = System.Drawing.Color.Blue;
}
else if (toolStripComboBox1.Text == "Green")
{
this.BackColor = System.Drawing.Color.Green;
}
else if (toolStripComboBox1.Text == "Magenta")
{
this.BackColor = System.Drawing.Color.Magenta;
}
else
{
toolStripComboBox1.Text = "Default";
this.BackColor = System.Drawing.Color.WhiteSmoke;
}
}

But when I try to change the color of the form by using the value "Red" for example from the comboBox, none of the selected values in the comboBox is working, the form is not changing to any of the colors... Any solutions please.

Hi Leandro, below you find what I wrote in the comment as code, it works for one form.
The MyApplicationSettings class is at the bottom, otherwise the forms designer will complain. If you want the multi-form solution, you have to give the MyApplicationSettings instance some global place to live. Form2 to Formn will then subscribe to the BackColorChanged event and provide their OnBackColorChanged event handler.


public partial class Form1 : Form
{
  public MyApplicationSettings ApplicationSettings = new MyApplicationSettings();

  public Form1()
  {
    InitializeComponent();
  }

  protected override void OnLoad(EventArgs e)
  {
    base.OnLoad(e);

    ApplicationSettings.BackColorChanged += OnBackColorChanged;
    comboBoxFormBackColor.SelectedIndexChanged += OnComboBoxFormBackColorChanged;
    comboBoxFormBackColor.Items.Add(System.Drawing.Color.Red);
    comboBoxFormBackColor.Items.Add(System.Drawing.Color.Blue);
    comboBoxFormBackColor.Items.Add(System.Drawing.Color.Green);
    comboBoxFormBackColor.Items.Add(System.Drawing.Color.Magenta);
    comboBoxFormBackColor.SelectedIndex = 0;
    ApplicationSettings.FormBackColor = (System.Drawing.Color)comboBoxFormBackColor.SelectedItem;
  }

  private void OnComboBoxFormBackColorChanged(object sender, EventArgs e)
  {
    ApplicationSettings.FormBackColor = (System.Drawing.Color)comboBoxFormBackColor.SelectedItem;
  }

  private void OnBackColorChanged(object sender, EventArgs e)
  {
    this.BackColor = ApplicationSettings.FormBackColor;
  }

}


public class MyApplicationSettings
{
  private Color _formBackColor;
  public System.Drawing.Color FormBackColor
  {
    get
    {
      return _formBackColor;
    }
    set
    {
      _formBackColor = value;
      OnBackColorChanged();
    }
  }

  public event EventHandler BackColorChanged;
  protected void OnBackColorChanged()
  {
    EventHandler handler = BackColorChanged;
    if (handler != null) handler(this, EventArgs.Empty);
  }

}
 
Share this answer
 
Comments
Ralf Meier 24-Jun-15 8:43am    
my vote 5 for your Solution !
I refer to your posted solution :
Don't use the ComboBox.Click-Event for your method.
Better use the ComboBox.SelectedIndexChanged.

But for me, the solution from FranzBe, posted as comment, should be the best one ...
 
Share this answer
 
v2
i think you can maintain one table for user color like( UID and ClorName) for default you can set any color one a user change then you can save new color into table for future request.


please mark as ans if it helps you.
 
Share this answer
 

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