381 lines
11 KiB
C#
381 lines
11 KiB
C#
using AgroBase.Forms.IHM.Controls;
|
||
using System;
|
||
using System.ComponentModel;
|
||
using System.Drawing;
|
||
using System.Globalization;
|
||
using System.Windows.Forms;
|
||
|
||
namespace AgroBase.Forms.IHM.Operacao.Parametros.Controls
|
||
{
|
||
[DefaultEvent("ValueChanged")]
|
||
public class HmiCompactNumericField : UserControl
|
||
{
|
||
private readonly TableLayoutPanel _root;
|
||
private readonly Label _lblTitulo;
|
||
private readonly TableLayoutPanel _valueLayout;
|
||
private readonly Button _btnDiminuir;
|
||
private readonly TextBox _txtValor;
|
||
private readonly Button _btnAumentar;
|
||
private readonly Label _lblUnidade;
|
||
|
||
private decimal _value;
|
||
private decimal _minimum;
|
||
private decimal _maximum = 100M;
|
||
private decimal _increment = 1M;
|
||
private int _decimalPlaces;
|
||
|
||
public event EventHandler ValueChanged;
|
||
|
||
public HmiCompactNumericField()
|
||
{
|
||
SetStyle(
|
||
ControlStyles.AllPaintingInWmPaint |
|
||
ControlStyles.OptimizedDoubleBuffer |
|
||
ControlStyles.ResizeRedraw |
|
||
ControlStyles.UserPaint,
|
||
true);
|
||
|
||
DoubleBuffered = true;
|
||
BackColor = HmiTheme.SurfaceRaised;
|
||
Margin = new Padding(3);
|
||
Padding = new Padding(7, 5, 7, 5);
|
||
MinimumSize = new Size(105, 62);
|
||
|
||
_root = new TableLayoutPanel
|
||
{
|
||
Dock = DockStyle.Fill,
|
||
BackColor = Color.Transparent,
|
||
ColumnCount = 1,
|
||
RowCount = 2,
|
||
Margin = Padding.Empty,
|
||
Padding = Padding.Empty
|
||
};
|
||
|
||
_root.ColumnStyles.Add(
|
||
new ColumnStyle(SizeType.Percent, 100F));
|
||
|
||
_root.RowStyles.Add(
|
||
new RowStyle(SizeType.Absolute, 22F));
|
||
|
||
_root.RowStyles.Add(
|
||
new RowStyle(SizeType.Percent, 100F));
|
||
|
||
_lblTitulo = new Label
|
||
{
|
||
Dock = DockStyle.Fill,
|
||
AutoSize = false,
|
||
AutoEllipsis = true,
|
||
BackColor = Color.Transparent,
|
||
ForeColor = HmiTheme.TextMuted,
|
||
Font = new Font("Segoe UI", 6.8F, FontStyle.Bold),
|
||
Text = "Parâmetro",
|
||
TextAlign = ContentAlignment.MiddleLeft,
|
||
Margin = Padding.Empty,
|
||
Padding = Padding.Empty
|
||
};
|
||
|
||
_valueLayout = new TableLayoutPanel
|
||
{
|
||
Dock = DockStyle.Fill,
|
||
BackColor = Color.Transparent,
|
||
ColumnCount = 4,
|
||
RowCount = 1,
|
||
Margin = Padding.Empty,
|
||
Padding = Padding.Empty
|
||
};
|
||
|
||
_valueLayout.ColumnStyles.Add(
|
||
new ColumnStyle(SizeType.Absolute, 34F));
|
||
|
||
_valueLayout.ColumnStyles.Add(
|
||
new ColumnStyle(SizeType.Percent, 100F));
|
||
|
||
_valueLayout.ColumnStyles.Add(
|
||
new ColumnStyle(SizeType.Absolute, 34F));
|
||
|
||
_valueLayout.ColumnStyles.Add(
|
||
new ColumnStyle(SizeType.Absolute, 34F));
|
||
|
||
_valueLayout.RowStyles.Add(
|
||
new RowStyle(SizeType.Percent, 100F));
|
||
|
||
_btnDiminuir = CriarBotaoValor("−");
|
||
_btnAumentar = CriarBotaoValor("+");
|
||
|
||
_txtValor = new TextBox
|
||
{
|
||
Dock = DockStyle.Fill,
|
||
BackColor = HmiTheme.Surface,
|
||
ForeColor = HmiTheme.Text,
|
||
BorderStyle = BorderStyle.FixedSingle,
|
||
Font = new Font("Segoe UI", 9F, FontStyle.Bold),
|
||
TextAlign = HorizontalAlignment.Center,
|
||
ReadOnly = true,
|
||
ShortcutsEnabled = false,
|
||
TabStop = false,
|
||
Margin = new Padding(3, 1, 3, 1)
|
||
};
|
||
|
||
_lblUnidade = new Label
|
||
{
|
||
Dock = DockStyle.Fill,
|
||
AutoSize = false,
|
||
BackColor = Color.Transparent,
|
||
ForeColor = HmiTheme.TextMuted,
|
||
Font = new Font("Segoe UI", 7F, FontStyle.Bold),
|
||
Text = "%",
|
||
TextAlign = ContentAlignment.MiddleCenter,
|
||
Margin = Padding.Empty,
|
||
Padding = Padding.Empty
|
||
};
|
||
|
||
_valueLayout.Controls.Add(_btnDiminuir, 0, 0);
|
||
_valueLayout.Controls.Add(_txtValor, 1, 0);
|
||
_valueLayout.Controls.Add(_btnAumentar, 2, 0);
|
||
_valueLayout.Controls.Add(_lblUnidade, 3, 0);
|
||
|
||
_root.Controls.Add(_lblTitulo, 0, 0);
|
||
_root.Controls.Add(_valueLayout, 0, 1);
|
||
|
||
Controls.Add(_root);
|
||
|
||
_btnDiminuir.Click += delegate
|
||
{
|
||
Value -= Increment;
|
||
};
|
||
|
||
_btnAumentar.Click += delegate
|
||
{
|
||
Value += Increment;
|
||
};
|
||
|
||
AtualizarTexto();
|
||
AtualizarBotoes();
|
||
}
|
||
|
||
private static Button CriarBotaoValor(string texto)
|
||
{
|
||
Button button = new Button
|
||
{
|
||
Dock = DockStyle.Fill,
|
||
BackColor = HmiTheme.Surface,
|
||
ForeColor = HmiTheme.Info,
|
||
FlatStyle = FlatStyle.Flat,
|
||
Font = new Font("Segoe UI", 12F, FontStyle.Bold),
|
||
Text = texto,
|
||
TextAlign = ContentAlignment.MiddleCenter,
|
||
Cursor = Cursors.Hand,
|
||
Margin = new Padding(1),
|
||
Padding = Padding.Empty,
|
||
TabStop = false,
|
||
UseVisualStyleBackColor = false
|
||
};
|
||
|
||
button.FlatAppearance.BorderSize = 1;
|
||
button.FlatAppearance.BorderColor = HmiTheme.Border;
|
||
button.FlatAppearance.MouseOverBackColor =
|
||
Color.FromArgb(35, 52, 61);
|
||
|
||
button.FlatAppearance.MouseDownBackColor =
|
||
Color.FromArgb(42, 67, 78);
|
||
|
||
return button;
|
||
}
|
||
|
||
private void DefinirValor(decimal value)
|
||
{
|
||
decimal novo = Math.Max(
|
||
Minimum,
|
||
Math.Min(Maximum, value));
|
||
|
||
novo = decimal.Round(
|
||
novo,
|
||
DecimalPlaces,
|
||
MidpointRounding.AwayFromZero);
|
||
|
||
if (_value == novo)
|
||
{
|
||
AtualizarTexto();
|
||
AtualizarBotoes();
|
||
return;
|
||
}
|
||
|
||
_value = novo;
|
||
|
||
AtualizarTexto();
|
||
AtualizarBotoes();
|
||
|
||
if (ValueChanged != null)
|
||
ValueChanged(this, EventArgs.Empty);
|
||
}
|
||
|
||
private void AtualizarTexto()
|
||
{
|
||
string formato = DecimalPlaces > 0
|
||
? "F" + DecimalPlaces
|
||
: "F0";
|
||
|
||
_txtValor.Text = _value.ToString(
|
||
formato,
|
||
CultureInfo.CurrentCulture);
|
||
}
|
||
|
||
private void AtualizarBotoes()
|
||
{
|
||
bool podeDiminuir = _value > Minimum;
|
||
bool podeAumentar = _value < Maximum;
|
||
|
||
AplicarEstadoBotao(
|
||
_btnDiminuir,
|
||
podeDiminuir);
|
||
|
||
AplicarEstadoBotao(
|
||
_btnAumentar,
|
||
podeAumentar);
|
||
}
|
||
|
||
private static void AplicarEstadoBotao(
|
||
Button button,
|
||
bool habilitado)
|
||
{
|
||
button.Enabled = habilitado;
|
||
|
||
if (habilitado)
|
||
{
|
||
button.ForeColor = HmiTheme.Info;
|
||
button.BackColor = HmiTheme.Surface;
|
||
button.FlatAppearance.BorderColor =
|
||
HmiTheme.Border;
|
||
}
|
||
else
|
||
{
|
||
button.ForeColor = HmiTheme.TextMuted;
|
||
button.BackColor = HmiTheme.SurfaceRaised;
|
||
button.FlatAppearance.BorderColor =
|
||
HmiTheme.SurfaceRaised;
|
||
}
|
||
}
|
||
|
||
[Category("Conteúdo")]
|
||
public string Titulo
|
||
{
|
||
get { return _lblTitulo.Text; }
|
||
set { _lblTitulo.Text = value ?? string.Empty; }
|
||
}
|
||
|
||
[Category("Conteúdo")]
|
||
public string Unidade
|
||
{
|
||
get { return _lblUnidade.Text; }
|
||
set
|
||
{
|
||
_lblUnidade.Text = value ?? string.Empty;
|
||
AtualizarLarguraUnidade();
|
||
}
|
||
}
|
||
|
||
private void AtualizarLarguraUnidade()
|
||
{
|
||
int largura;
|
||
|
||
if (string.IsNullOrWhiteSpace(_lblUnidade.Text))
|
||
largura = 4;
|
||
else if (_lblUnidade.Text.Length <= 1)
|
||
largura = 25;
|
||
else
|
||
largura = 35;
|
||
|
||
_valueLayout.ColumnStyles[3].SizeType =
|
||
SizeType.Absolute;
|
||
|
||
_valueLayout.ColumnStyles[3].Width =
|
||
largura;
|
||
}
|
||
|
||
[Category("Dados")]
|
||
[DefaultValue(typeof(decimal), "0")]
|
||
public decimal Value
|
||
{
|
||
get { return _value; }
|
||
set { DefinirValor(value); }
|
||
}
|
||
|
||
[Category("Dados")]
|
||
[DefaultValue(typeof(decimal), "0")]
|
||
public decimal Minimum
|
||
{
|
||
get { return _minimum; }
|
||
set
|
||
{
|
||
_minimum = value;
|
||
|
||
if (_maximum < _minimum)
|
||
_maximum = _minimum;
|
||
|
||
DefinirValor(_value);
|
||
}
|
||
}
|
||
|
||
[Category("Dados")]
|
||
[DefaultValue(typeof(decimal), "100")]
|
||
public decimal Maximum
|
||
{
|
||
get { return _maximum; }
|
||
set
|
||
{
|
||
_maximum = value;
|
||
|
||
if (_maximum < _minimum)
|
||
_minimum = _maximum;
|
||
|
||
DefinirValor(_value);
|
||
}
|
||
}
|
||
|
||
[Category("Dados")]
|
||
[DefaultValue(typeof(decimal), "1")]
|
||
public decimal Increment
|
||
{
|
||
get { return _increment; }
|
||
set
|
||
{
|
||
_increment = value > 0M
|
||
? value
|
||
: 1M;
|
||
}
|
||
}
|
||
|
||
[Category("Dados")]
|
||
[DefaultValue(0)]
|
||
public int DecimalPlaces
|
||
{
|
||
get { return _decimalPlaces; }
|
||
set
|
||
{
|
||
_decimalPlaces = Math.Max(
|
||
0,
|
||
Math.Min(4, value));
|
||
|
||
DefinirValor(_value);
|
||
}
|
||
}
|
||
|
||
[Browsable(false)]
|
||
public Button BotaoDiminuir
|
||
{
|
||
get { return _btnDiminuir; }
|
||
}
|
||
|
||
[Browsable(false)]
|
||
public Button BotaoAumentar
|
||
{
|
||
get { return _btnAumentar; }
|
||
}
|
||
|
||
[Browsable(false)]
|
||
public TextBox CampoValor
|
||
{
|
||
get { return _txtValor; }
|
||
}
|
||
}
|
||
} |