Reputation: 105
I have a simple windows form and I have added a picturebox that I want to act as a dark mode on/off switch for my application. I have created an event for mouse click, but it only works one way:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WeatherApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// The below two lines are used to move my WindowsForm window
private bool mouseDown;
private Point lastlocation;
public void picDarkMode_MouseClick(object sender, MouseEventArgs e)
{
//var frmForm1 = new Form1();
//frmForm1.BackColor = Color.Black;
//var darkTheme = this.BackColor = System.Drawing.Color.DarkGray;
this.BackColor = System.Drawing.Color.DarkGray;
if (System.Drawing.Color.DarkGray == System.Drawing.Color.DarkGray)
{
this.BackColor = System.Drawing.Color.LightYellow;
}
else
{
this.BackColor = System.Drawing.Color.DarkGray;
}
//this.BackColor = System.Drawing.Color.LightYellow;
}
I tried to play with if statements, but cannot understand why it is not working...
Upvotes: 0
Views: 172
Reputation: 595
You are making two mistakes. You are setting the backcolor to DarkGray before the if statement, so it will always give the same result, and you are comparing DarkGray to DarkGray instead of the forms backcolor to DarkGray. So... Get rid of this line:
this.BackColor = System.Drawing.Color.DarkGray;
And change this:
if (System.Drawing.Color.DarkGray == System.Drawing.Color.DarkGray)
To this:
if (this.BackColor == System.Drawing.Color.DarkGray)
This is the whole click event:
public void picDarkMode_MouseClick(object sender, MouseEventArgs e)
{
if (this.BackColor == System.Drawing.Color.DarkGray)
{
this.BackColor = System.Drawing.Color.LightYellow;
}
else
{
this.BackColor = System.Drawing.Color.DarkGray;
}
}
Upvotes: 3