Skip to content

Instantly share code, notes, and snippets.

@amitkhare
Last active March 24, 2020 09:50
Show Gist options
  • Save amitkhare/1aaf0af6fa83c850900a7774e9f4ee6a to your computer and use it in GitHub Desktop.
Save amitkhare/1aaf0af6fa83c850900a7774e9f4ee6a to your computer and use it in GitHub Desktop.
UserControl for Counting text of given textbox
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.IO;
namespace TextCounter
{
public enum DisplayFormat { Remaining = 0, Typed = 1, Both = 2, MaxLength = 3 }
public partial class TextCounter: UserControl
{
[Browsable(true), Description("Output Image Format"), Category("Misc")]
public DisplayFormat ShowCountAs { get; set; }
[Description("TextBox Name"), Category("Misc")]
public string TextBox_Name { get; set; }
private TextBox sourceTextBox;
public TextCounter()
{
InitializeComponent();
_ = TaskUpdateFromParent();
}
private async Task TaskUpdateFromParent()
{
await Task.Delay(600);
if (TextBox_Name == null) TextBox_Name = "tboxSource";
if (this.Parent == null || this.Parent.Controls.Count < 1) return;
// define TextBox_Slug
if (this.Parent.Controls[TextBox_Name] != null)
{
sourceTextBox = (TextBox)this.Parent.Controls[TextBox_Name];
sourceTextBox.KeyUp -= OnTextBox_KeyUp;
sourceTextBox.KeyUp += OnTextBox_KeyUp;
sourceTextBox.TextChanged -= OnTextBox_TextChanged;
sourceTextBox.TextChanged += OnTextBox_TextChanged;
upateLabel();
this.BackColor = Control.DefaultBackColor;
this.ForeColor = Control.DefaultForeColor;
//lblCount.ForeColor = this.ForeColor;
}
}
private void OnTextBox_TextChanged(object sender, EventArgs e)
{
upateLabel();
}
private void OnTextBox_KeyUp(object sender, KeyEventArgs e)
{
upateLabel();
}
private void upateLabel() {
{
if (ShowCountAs == DisplayFormat.Remaining)
lblCount.Text = (sourceTextBox.MaxLength - sourceTextBox.Text.Length).ToString();
else if (ShowCountAs == DisplayFormat.Typed)
lblCount.Text = (sourceTextBox.Text.Length).ToString();
else if (ShowCountAs == DisplayFormat.MaxLength)
lblCount.Text = sourceTextBox.MaxLength.ToString();
else
lblCount.Text = (sourceTextBox.MaxLength - sourceTextBox.Text.Length).ToString() + "/" + sourceTextBox.MaxLength.ToString();
}
lblCount.BackColor = getColor();
}
private Color getColor() {
int max = sourceTextBox.MaxLength;
int current = sourceTextBox.Text.Length;
int filledPercentage = (current * 100 ) / max;
/*
Console.WriteLine(" current : " + current);
Console.WriteLine(" max : " + max);
Console.WriteLine(" filledPercentage : " + filledPercentage);
*/
if (filledPercentage <= 50)
return Color.FromArgb(255, 0, 240, 0);
if (filledPercentage > 50 && filledPercentage <= 85)
return Color.FromArgb(255, 150, 250, 0);
if (filledPercentage > 85 && filledPercentage < 100)
return Color.FromArgb(255, 255, 210, 0);
else
return Color.FromArgb(255, 255, 100, 100);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment