using System; using System.Collections.Generic; using System.ComponentModel; using…
- User
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing.Drawing2D;
namespace WindowsFormsAsas.CustomControls.TextBox { [DefaultEvent("_TextChanged")] public partial class AsasTextbox : UserControl { #region -> Fields //Fields private Color borderColor = Color.MediumSlateBlue; private Color borderFocusColor = Color.HotPink; private int borderSize = 2; private bool underlinedStyle = false; private bool isFocused = false;
private int borderRadius = 0; private Color placeholderColor = Color.DarkGray; private string placeholderText = ""; private bool isPlaceholder = false; private bool isPasswordChar = false; private string _text = ""; //Events public event EventHandler _TextChanged; #endregion public AsasTextbox() { InitializeComponent(); } #region -> Properties [Category("Asas Properties")] public Color BorderColor { get { return borderColor; } set { borderColor = value; this.Invalidate(); } } [Category("Asas Properties")] public Color BorderFocusColor { get { return borderFocusColor; } set { borderFocusColor = value; } } [Category("Asas Properties")] public int BorderSize { get { return borderSize; } set { if (value >= 1) { borderSize = value; this.Invalidate(); } } } [Category("Asas Properties")] public bool UnderlinedStyle { get { return underlinedStyle; } set { underlinedStyle = value; this.Invalidate(); } } [Category("Asas Properties")] public bool PasswordChar { get { return isPasswordChar; } set { isPasswordChar = value; if (!isPlaceholder) textBox1.UseSystemPasswordChar = value; } } [Category("Asas Properties")] public bool Multiline { get { return textBox1.Multiline; } set { textBox1.Multiline = value; } } [Category("Asas Properties")] public override Color BackColor { get { return base.BackColor; } set { base.BackColor = value; textBox1.BackColor = value; } } [Category("Asas Properties")] public override Color ForeColor { get { return base.ForeColor; } set { base.ForeColor = value; textBox1.ForeColor = value; } } [Category("Asas Properties")] public override Font Font { get { return base.Font; } set { base.Font = value; textBox1.Font = value; if (this.DesignMode) UpdateControlHeight(); } } [Category("Asas Properties")] public string Texts { get { if (isPlaceholder) return ""; else return textBox1.Text; } set { textBox1.Text = value; SetPlaceholder(); } } [Category("Asas Properties")] public int BorderRadius { get { return borderRadius; } set { if (value >= 0) { borderRadius = value; this.Invalidate();//Redraw control } } } [Category("Asas Properties")] public Color PlaceholderColor { get { return placeholderColor; } set { placeholderColor = value; if (isPlaceholder) textBox1.ForeColor = value; } } [Category("Asas Properties")] public string PlaceholderText { get { return placeholderText; } set { placeholderText = value; textBox1.Text = ""; SetPlaceholder(); } } //[Category("Asas Properties")] //public string _Text //{ // get { return _text; } // set // { // _Text = _text; // } //} #endregion #region -> Overridden methods protected override void OnResize(EventArgs e) { base.OnResize(e); if (this.DesignMode) UpdateControlHeight(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); UpdateControlHeight(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics graph = e.Graphics; if (borderRadius > 1)//Rounded TextBox { //-Fields var rectBorderSmooth = this.ClientRectangle; var rectBorder = Rectangle.Inflate(rectBorderSmooth, -borderSize, -borderSize); int smoothSize = borderSize > 0 ? borderSize : 1; using (GraphicsPath pathBorderSmooth = GetFigurePath(rectBorderSmooth, borderRadius)) using (GraphicsPath pathBorder = GetFigurePath(rectBorder, borderRadius - borderSize)) using (Pen penBorderSmooth = new Pen(this.Parent.BackColor, smoothSize)) using (Pen penBorder = new Pen(borderColor, borderSize)) { //-Drawing this.Region = new Region(pathBorderSmooth);//Set the rounded region of UserControl if (borderRadius > 15) SetTextBoxRoundedRegion();//Set the rounded region of TextBox component graph.SmoothingMode = SmoothingMode.AntiAlias; penBorder.Alignment = System.Drawing.Drawing2D.PenAlignment.Center; if (isFocused) penBorder.Color = borderFocusColor; if (underlinedStyle) //Line Style { //Draw border smoothing graph.DrawPath(penBorderSmooth, pathBorderSmooth); //Draw border graph.SmoothingMode = SmoothingMode.None; graph.DrawLine(penBorder, 0, this.Height - 1, this.Width, this.Height - 1); } else //Normal Style { //Draw border smoothing graph.DrawPath(penBorderSmooth, pathBorderSmooth); //Draw border graph.DrawPath(penBorder, pathBorder); } } } else //Square/Normal TextBox { //Draw border using (Pen penBorder = new Pen(borderColor, borderSize)) { this.Region = new Region(this.ClientRectangle); penBorder.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset; if (isFocused) penBorder.Color = borderFocusColor; if (underlinedStyle) //Line Style graph.DrawLine(penBorder, 0, this.Height - 1, this.Width, this.Height - 1); else //Normal Style graph.DrawRectangle(penBorder, 0, 0, this.Width - 0.5F, this.Height - 0.5F); } } } #endregion #region -> Private methods private void SetPlaceholder() { if (string.IsNullOrWhiteSpace(textBox1.Text) && placeholderText != "") { isPlaceholder = true; textBox1.Text = placeholderText; textBox1.ForeColor = placeholderColor; if (isPasswordChar) textBox1.UseSystemPasswordChar = false; } } private void RemovePlaceholder() { if (isPlaceholder && placeholderText != "") { isPlaceholder = false; textBox1.Text = ""; textBox1.ForeColor = this.ForeColor; if (isPasswordChar) textBox1.UseSystemPasswordChar = true; } } private GraphicsPath GetFigurePath(Rectangle rect, int radius) { GraphicsPath path = new GraphicsPath(); float curveSize = radius * 2F; path.StartFigure(); path.AddArc(rect.X, rect.Y, curveSize, curveSize, 180, 90); path.AddArc(rect.Right - curveSize, rect.Y, curveSize, curveSize, 270, 90); path.AddArc(rect.Right - curveSize, rect.Bottom - curveSize, curveSize, curveSize, 0, 90); path.AddArc(rect.X, rect.Bottom - curveSize, curveSize, curveSize, 90, 90); path.CloseFigure(); return path; } private void SetTextBoxRoundedRegion() { GraphicsPath pathTxt; if (Multiline) { pathTxt = GetFigurePath(textBox1.ClientRectangle, borderRadius - borderSize); textBox1.Region = new Region(pathTxt); } else { pathTxt = GetFigurePath(textBox1.ClientRectangle, borderSize * 2); textBox1.Region = new Region(pathTxt); } pathTxt.Dispose(); } private void UpdateControlHeight() { if (textBox1.Multiline == false) { int txtHeight = TextRenderer.MeasureText("Text", this.Font).Height + 1; textBox1.Multiline = true; textBox1.MinimumSize = new Size(0, txtHeight); textBox1.Multiline = false; this.Height = textBox1.Height + this.Padding.Top + this.Padding.Bottom; } } #endregion #region -> TextBox events private void textBox1_TextChanged(object sender, EventArgs e) { if (_TextChanged != null) _TextChanged.Invoke(sender, e); if (sender is System.Windows.Forms.TextBox textBox) { _text = textBox.Text; } } private void textBox1_Enter(object sender, EventArgs e) { isFocused = true; this.Invalidate(); RemovePlaceholder(); } private void textBox1_Leave(object sender, EventArgs e) { isFocused = false; this.Invalidate(); SetPlaceholder(); } private void textBox1_MouseEnter(object sender, EventArgs e) { this.OnMouseEnter(e); } private void textBox1_MouseLeave(object sender, EventArgs e) { this.OnMouseLeave(e); } private void textBox1_Click(object sender, EventArgs e) { this.OnClick(e); } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { this.OnKeyPress(e); } ///::::+ #endregion } - Assistant
- User
when i disable this custom textBox only the text box disabled and the userControl around it doesn't disable. my purpose is when I disable the custom text box all of it get the disabled color
- Assistant
- User
are you meant this in the public override bool Enabled? /// Gets the default Input Method Editor (IME) mode supported by the control. /// One of the values. protected virtual ImeMode DefaultImeMode => ImeMode.Inherit;
internal int DisableImeModeChangedCount { get => this.Properties.GetInteger(Control.PropDisableImeModeChangedCount, out bool _); set => this.Properties.SetInteger(Control.PropDisableImeModeChangedCount, value); }
private static bool IgnoreWmImeNotify { get => Control.ignoreWmImeNotify; set => Control.ignoreWmImeNotify = value; }
- Assistant