/// <summary> /// Provides a way to edit a property and returns the new value. /// </summary> /// <param name="context"></param> /// <param name="provider"></param> /// <param name="value"></param> /// <returns></returns> public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { var color = ((Color)value); var form = new PsColorPicker(color); if (form.ShowDialog() != DialogResult.OK) { return(color); } var newColor = form.Color; var result = Color.FromArgb(color.A, newColor.R, newColor.G, newColor.B); return(result); }
/// <summary> /// Opens color selector and sets the appropriate text box. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnPropColorSelect_Click(object sender, EventArgs e) { var button = (Button)sender; var textBoxName = button.Name.Replace("Btn", "Txt"); var textBoxField = this.GetType().GetField(textBoxName, BindingFlags.NonPublic | BindingFlags.Instance); if (textBoxField == null) { MessageBox.Show("Text box not found.", Title, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var textBox = textBoxField.GetValue(this); var textProperty = textBoxField.FieldType.GetProperty("Text", BindingFlags.Public | BindingFlags.Instance); if (textProperty == null) { MessageBox.Show("Text property not found.", Title, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var prevColorText = ((string)textProperty.GetValue(textBox)).Replace("0x", ""); if (!int.TryParse(prevColorText, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var prevColorArgb)) { prevColorArgb = 0xFFFFFF; } var prevColor = Color.FromArgb(prevColorArgb); var form = new PsColorPicker(prevColor); if (form.ShowDialog() != DialogResult.OK) { return; } var newColor = form.Color; var newColorText = string.Format("0x{0:X8}", newColor.ToArgb()); button.BackColor = newColor; textProperty.SetValue(textBox, newColorText); }
/// <summary> /// Opens a color picker on left click to select a new color, /// or resets the color to the default on right-click. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void LblColor_MouseClick(object sender, MouseEventArgs e) { var control = (sender as Control); if (e.Button == MouseButtons.Left) { var form = new PsColorPicker(control.BackColor); form.ShowDialog(); if (form.DialogResult == DialogResult.OK) { control.BackColor = form.Color; } } else if (e.Button == MouseButtons.Right) { this.ResetOption(control.Tag as string); } }