BlueMystical / Dark-Mode-Forms

Apply Dark Mode to all Controls in a Form [WinForms]
GNU General Public License v3.0
99 stars 13 forks source link

Dark-Mode-Forms

Apply Dark Mode to all Controls in a Form [WinForms]

Preview

Now with Dark Messenger

Window's default MessageBox cant not be themed, so Added a Messenger.cs class that allowes the user to popup Messages and InputBoxes:

MessageBox:

try
{
   if (Messenger.MessageBox("Hello World!", "You got a Message:",
       MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
   {
      //Do Something here.
   }
}
catch (Exception ex)
{
   Messenger.MessageBox(ex);
}

InputBox:

// Definition of a Single Field:
var BoolControl = new KeyValue("Boolean", "true", KeyValue.ValueTypes.Boolean);

// Can Validate User Inputs on the Field:
BoolControl.Validate += (object? _control, KeyValue.ValidateEventArgs _e) =>
{
   string OldValue = _e.OldValue;
   if (_e.NewValue == "False")
   {
      //_e.Cancel = true; //<- CAN CANCEL THE MODIFICATION
      _e.ErrorText = "No puede ser Falso!";
   }
};

// Custom Values for 'Dynamic' Fields:
List<KeyValue> Dtypes = new List<KeyValue>
{
   new KeyValue("RichText Format", "0"),
   new KeyValue("Plain Text",      "1"),
   new KeyValue("AccountManager",  "2")
};

// Definition of Multiple Fields:
List<KeyValue> _Fields = new List<KeyValue>
{
   new KeyValue("String",  "String",   KeyValue.ValueTypes.String),
   new KeyValue("Password","Password", KeyValue.ValueTypes.Password),
   new KeyValue("Integer", "1000",     KeyValue.ValueTypes.Integer),
   new KeyValue("Decimal", "3,141638", KeyValue.ValueTypes.Decimal),
   BoolControl,
   new KeyValue("Dynamic", "1",        KeyValue.ValueTypes.Dynamic, Dtypes),
};

// Dialog Show:
if (Messenger.InputBox("Custom InputBox", "Please Fill the Form:", ref _Fields,
    MsgIcon.Edit, MessageBoxButtons.OKCancel) == DialogResult.OK)
{
   Debug.WriteLine(string.Format("The New Password is: '{0}'", _Fields[0].Value));
}

Example of a LoginForm with Password Validation:

image

List<KeyValue> _Fields = new List<KeyValue>
{
   new KeyValue("User Name", "user", KeyValue.ValueTypes.String),
   new KeyValue("Password",  string.Empty, KeyValue.ValueTypes.Password)
};

// Can Validate All the Controls before Closing the Dialog:
Messenger.ValidateControls += (object? sender, CancelEventArgs e) =>
{
   string _userName = _Fields[0].Value;
   string _password = _Fields[1].Value;

   //TODO: Here you should send the User/Password to your BackEnd for Validation
   if (_password != "password")
   {
      _Fields[1].ErrorText = "Incorrect Password!";
      e.Cancel = true; //<- Prevents the Dialog to be closed until is valid
   }
};

// Dialog Show:
if (Messenger.InputBox("Login", "Please Input your Credentials:", ref _Fields,
 MsgIcon.Lock, MessageBoxButtons.OKCancel) == DialogResult.OK)
{
   Messenger.MessageBox(string.Format("The User '{0}' is Logged!", _Fields[0].Value), "Login Correct!",
      MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

Implementation

public Form1()
{
   InitializeComponent();
   _ = new DarkModeCS(this); //<- Line of code here
}

Dark Mode Colors

Preview

Framework Compatibility

Limitations

There are a few Winforms Controls that are, by design, extremely hard to theme:

Buy me a Coffe