LevelOS/Components/Textbox/LV-TEXTBOX1.cs

211 lines
5.3 KiB
C#

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
using Image = System.Drawing.Image;
namespace CPM
{
public partial class LV_TEXTBOX1 : UserControl
{
private Color borderColor = Color.Gray;
private Color borderFocusColor = Color.DodgerBlue;
private int borderSize = 2;
private bool isFocused = false;
private bool underlineStyle = false;
public event EventHandler IconClick;
public LV_TEXTBOX1()
{
InitializeComponent();
Redraw();
}
// Propriedades
public override string Text
{
get { return txt.Text; }
set { txt.Text = value; }
}
public bool ReadOnly
{
get { return txt.ReadOnly; }
set { txt.ReadOnly = value; }
}
public Color BorderColor
{
get { return borderColor; }
set { borderColor = value; Invalidate(); }
}
public Color BorderFocusColor
{
get { return borderFocusColor; }
set { borderFocusColor = value; Invalidate(); }
}
public int BorderSize
{
get { return borderSize; }
set { borderSize = value; Invalidate(); }
}
// No seu LvlTextbox
[Browsable(true)]
[Category("Behavior")]
public int SelectionStart
{
get { return txt.SelectionStart; }
set { txt.SelectionStart = value; }
}
[Browsable(true)]
[Category("Behavior")]
public int MaxLength
{
get { return txt.MaxLength; }
set { txt.MaxLength = value; }
}
[Browsable(true)]
[Category("Behavior")]
public bool Multiline
{
get { return txt.Multiline; }
set { txt.Multiline = value; }
}
public bool UnderlineStyle
{
get { return underlineStyle; }
set { underlineStyle = value; Invalidate(); }
}
public Image Icon
{
get { return picIcon.Image; }
set
{
picIcon.Image = value;
picIcon.Visible = (value != null);
Redraw();
}
}
public enum IconAlignEnum { Left, Right }
private IconAlignEnum iconAlign = IconAlignEnum.Left;
public IconAlignEnum IconAlign
{
get { return iconAlign; }
set { iconAlign = value; Redraw(); }
}
// Renderização da borda
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Color color = isFocused ? borderFocusColor : borderColor;
using (Pen pen = new Pen(color, borderSize))
{
pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
if (underlineStyle)
{
e.Graphics.DrawLine(pen, 0, Height - 1, Width, Height - 1);
}
else
{
e.Graphics.DrawRectangle(pen, 0, 0, Width - 1, Height - 1);
}
}
}
// Atualizar layout
public void Redraw()
{
int offset = 0;
if (picIcon.Visible)
{
offset = picIcon.Width + 8;
if (iconAlign == IconAlignEnum.Left)
{
picIcon.Left = 5;
txt.Left = offset;
txt.Width = Width - offset - 5;
}
else
{
picIcon.Left = Width - picIcon.Width - 5;
txt.Left = 5;
txt.Width = Width - offset - 5;
}
}
else
{
txt.Left = 5;
txt.Width = Width - 10;
}
Invalidate();
}
// Eventos internos
private void txt_Enter(object sender, EventArgs e)
{
isFocused = true;
Invalidate();
}
private void txt_Leave(object sender, EventArgs e)
{
isFocused = false;
Invalidate();
}
private char _passwordChar = '\0';
public char PasswordChar
{
get { return _passwordChar; }
set
{
_passwordChar = value;
txt.PasswordChar = value;
txt.UseSystemPasswordChar = (value != '\0');
}
}
public bool UseSystemPasswordChar
{
get { return txt.UseSystemPasswordChar; }
set { txt.UseSystemPasswordChar = value; }
}
private void txt_KeyPress(object sender, KeyPressEventArgs e)
{
OnKeyPress(e);
}
private void txt_TextChanged(object sender, EventArgs e)
{
OnTextChanged(e);
}
private void picIcon_Click(object sender, EventArgs e)
{
if (IconClick != null)
IconClick(this, e);
}
private void LvlTextbox_Resize(object sender, EventArgs e)
{
Redraw();
}
}
}