From 0555d16597fabbc618cfef1d90efaf902b6c33f4 Mon Sep 17 00:00:00 2001 From: NARWIN Date: Thu, 17 Oct 2019 21:26:55 -0800 Subject: [PATCH] Added mouse hold feature to increase and decrease MetroSetNumeric value, Fix #20, Fix #22 --- MetroSet UI Example/Form1.Designer.cs | 9 +- MetroSet UI/Controls/MetroSetNumeric.cs | 65 +++++++++--- MetroSet UI/Controls/MetroSetRichTextBox.cs | 32 ++---- MetroSet UI/Controls/MetroSetTextBox.cs | 109 +++++++------------- 4 files changed, 97 insertions(+), 118 deletions(-) diff --git a/MetroSet UI Example/Form1.Designer.cs b/MetroSet UI Example/Form1.Designer.cs index ff7ce68..5a6a3eb 100644 --- a/MetroSet UI Example/Form1.Designer.cs +++ b/MetroSet UI Example/Form1.Designer.cs @@ -135,13 +135,12 @@ private void InitializeComponent() this.metroSetTabControl1.Controls.Add(this.metroSetTabPage5); this.metroSetTabControl1.Controls.Add(this.metroSetTabPage6); this.metroSetTabControl1.Controls.Add(this.metroSetTabPage7); - this.metroSetTabControl1.Cursor = System.Windows.Forms.Cursors.Hand; + this.metroSetTabControl1.Cursor = System.Windows.Forms.Cursors.Default; this.metroSetTabControl1.Dock = System.Windows.Forms.DockStyle.Fill; - this.metroSetTabControl1.Font = new System.Drawing.Font("Segoe UI", 8F); this.metroSetTabControl1.ItemSize = new System.Drawing.Size(100, 38); this.metroSetTabControl1.Location = new System.Drawing.Point(12, 70); this.metroSetTabControl1.Name = "metroSetTabControl1"; - this.metroSetTabControl1.SelectedIndex = 2; + this.metroSetTabControl1.SelectedIndex = 0; this.metroSetTabControl1.Size = new System.Drawing.Size(1114, 616); this.metroSetTabControl1.SizeMode = System.Windows.Forms.TabSizeMode.Fixed; this.metroSetTabControl1.Speed = 100; @@ -1257,7 +1256,7 @@ private void InitializeComponent() this.metroSetContextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.textBoxToolStripMenuItem}); this.metroSetContextMenuStrip1.Name = "metroSetContextMenuStrip1"; - this.metroSetContextMenuStrip1.Size = new System.Drawing.Size(115, 26); + this.metroSetContextMenuStrip1.Size = new System.Drawing.Size(116, 26); this.metroSetContextMenuStrip1.Style = MetroSet_UI.Design.Style.Light; this.metroSetContextMenuStrip1.StyleManager = null; this.metroSetContextMenuStrip1.ThemeAuthor = "Narwin"; @@ -1266,7 +1265,7 @@ private void InitializeComponent() // textBoxToolStripMenuItem // this.textBoxToolStripMenuItem.Name = "textBoxToolStripMenuItem"; - this.textBoxToolStripMenuItem.Size = new System.Drawing.Size(114, 22); + this.textBoxToolStripMenuItem.Size = new System.Drawing.Size(115, 22); this.textBoxToolStripMenuItem.Text = "TextBox"; // // metroSetLabel8 diff --git a/MetroSet UI/Controls/MetroSetNumeric.cs b/MetroSet UI/Controls/MetroSetNumeric.cs index 169d570..dda5c35 100644 --- a/MetroSet UI/Controls/MetroSetNumeric.cs +++ b/MetroSet UI/Controls/MetroSetNumeric.cs @@ -31,6 +31,7 @@ using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; +using Timer = System.Timers.Timer; namespace MetroSet_UI.Controls { @@ -114,6 +115,7 @@ public StyleManager StyleManager private StyleManager _styleManager; private Point _point; private int _value; + private readonly Timer _holdTimer; #endregion Internal Vars @@ -132,6 +134,13 @@ public MetroSetNumeric() _utl = new Utilites(); ApplyTheme(); _point = new Point(0, 0); + _holdTimer = new Timer() + { + Interval = 10, + AutoReset = true, + Enabled = false + }; + _holdTimer.Elapsed += HoldTimer_Tick; } #endregion Constructors @@ -294,7 +303,7 @@ public int Value } [Browsable(false)] - public override Color BackColor => Color.Transparent; + public sealed override Color BackColor => Color.Transparent; /// /// Gets or sets the control backcolor. @@ -351,11 +360,10 @@ protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); _point = e.Location; - if (_point.X > Width - 50) - { - Cursor = Cursors.Hand; - } Invalidate(); + Cursor = _point.X > Width - 50 ? Cursors.Hand : Cursors.IBeam; + + } /// @@ -365,17 +373,7 @@ protected override void OnMouseMove(MouseEventArgs e) protected override void OnClick(EventArgs e) { base.OnClick(e); - if (_point.X <= Width - 45 || _point.X >= Width - 3) return; - if (_point.X > Width - 45 && _point.X < Width - 25) - { - if (Value + 1 <= Maximum) - Value += 1; - } - else - { - if (Value - 1 >= Minimum) - Value -= 1; - } + Revaluate(); } /// @@ -404,7 +402,42 @@ protected override void OnResize(EventArgs e) Height = 26; } + protected override void OnMouseDown(MouseEventArgs e) + { + base.OnMouseDown(e); + if (_point.X <= Width - 45 || _point.X >= Width - 3) return; + if (e.Button == MouseButtons.Left) + { + _holdTimer.Enabled = true; + } + + Invalidate(); + } + + protected override void OnMouseUp(MouseEventArgs e) + { + _holdTimer.Enabled = false; + } + private void HoldTimer_Tick(object sender, EventArgs args) + { + Revaluate(); + } + + private void Revaluate() + { + if (_point.X <= Width - 45 || _point.X >= Width - 3) return; + if (_point.X > Width - 45 && _point.X < Width - 25) + { + if (Value + 1 <= Maximum) + Value += 1; + } + else + { + if (Value - 1 >= Minimum) + Value -= 1; + } + } #endregion diff --git a/MetroSet UI/Controls/MetroSetRichTextBox.cs b/MetroSet UI/Controls/MetroSetRichTextBox.cs index 378c6ce..c6e8474 100644 --- a/MetroSet UI/Controls/MetroSetRichTextBox.cs +++ b/MetroSet UI/Controls/MetroSetRichTextBox.cs @@ -126,31 +126,6 @@ public StyleManager StyleManager private RichTextBox T = new RichTextBox(); - private RichTextBox _T - { - get => _T; - set - { - if (_T != null) - { - _T.MouseHover -= T_MouseHover; - _T.MouseUp -= T_MouseUp; - _T.MouseLeave -= T_Leave; - _T.MouseEnter -= T_Enter; - _T.KeyDown -= T_KeyDown; - _T.TextChanged -= T_TextChanged; - } - _T = value; - if (_T == null) return; - _T.MouseHover += T_MouseHover; - _T.MouseUp += T_MouseUp; - _T.Leave += T_Leave; - _T.Enter += T_Enter; - _T.KeyDown += T_KeyDown; - _T.TextChanged += T_TextChanged; - } - } - #endregion Base RichTextBox #endregion Internal Vars @@ -196,6 +171,13 @@ private void T_Defaults() T.Font = Font; T.Size = new Size(Width, Height); + T.MouseHover += T_MouseHover; + T.MouseUp += T_MouseUp; + T.Leave += T_Leave; + T.Enter += T_Enter; + T.KeyDown += T_KeyDown; + T.TextChanged += T_TextChanged; + } #endregion Constructors diff --git a/MetroSet UI/Controls/MetroSetTextBox.cs b/MetroSet UI/Controls/MetroSetTextBox.cs index fceea04..08f0355 100644 --- a/MetroSet UI/Controls/MetroSetTextBox.cs +++ b/MetroSet UI/Controls/MetroSetTextBox.cs @@ -133,28 +133,6 @@ public StyleManager StyleManager #region Base TextBox private TextBox T = new TextBox(); - private TextBox _T - { - get => _T; - set - { - if (_T != null) - { - _T.MouseHover -= T_MouseHover; - _T.MouseLeave -= T_Leave; - _T.MouseEnter -= T_Enter; - _T.KeyDown -= T_KeyDown; - _T.TextChanged -= T_TextChanged; - } - _T = value; - if (_T == null) return; - _T.MouseHover += T_MouseHover; - _T.Leave += T_Leave; - _T.Enter += T_Enter; - _T.KeyDown += T_KeyDown; - _T.TextChanged += T_TextChanged; - } - } #endregion @@ -212,6 +190,14 @@ private void T_Defaults() { Height = T.Height + 11; } + + T.MouseHover += T_MouseHover; + T.Leave += T_Leave; + T.Enter += T_Enter; + T.KeyDown += T_KeyDown; + T.TextChanged += T_TextChanged; + T.KeyPress += T_KeyPress; + } #endregion Constructors @@ -369,6 +355,8 @@ public void UpdateProperties() public new event TextChangedEventHandler TextChanged; public delegate void TextChangedEventHandler(object sender); + public virtual event KeyPressEventHandler KeyPressed; + public delegate void KeyPressEventHandler(object sender); /// /// Handling textbox leave event and raising the same event here. @@ -381,6 +369,12 @@ public void T_Leave(object sender, EventArgs e) Invalidate(); } + public void T_KeyPress(object sender, KeyPressEventArgs e) + { + KeyPressed?.Invoke(this); + Invalidate(); + } + /// /// Handling mouse leave event of the cotnrol. /// @@ -463,7 +457,7 @@ public void T_Enter(object sender, EventArgs e) /// - /// Handling Keydown event of thextbox cotnrol. + /// Handling Keydown event of textbox cotnrol. /// /// object /// KeyEventArgs @@ -471,11 +465,9 @@ private void T_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.A) e.SuppressKeyPress = true; - if (e.Control && e.KeyCode == Keys.C) - { - T.Copy(); - e.SuppressKeyPress = true; - } + if (!e.Control || e.KeyCode != Keys.C) return; + T.Copy(); + e.SuppressKeyPress = true; } @@ -508,10 +500,7 @@ protected override void OnCreateControl() /// public void AppendText(string text) { - if (T != null) - { - T.AppendText(text); - } + T?.AppendText(text); } @@ -520,12 +509,10 @@ public void AppendText(string text) /// public void Undo() { - if (T != null) + if (T == null) return; + if (T.CanUndo) { - if (T.CanUndo) - { - T.Undo(); - } + T.Undo(); } } @@ -537,14 +524,7 @@ public void Undo() /// public int GetLineFromCharIndex(int index) { - if (T != null) - { - return T.GetLineFromCharIndex(index); - } - else - { - return 0; - } + return T?.GetLineFromCharIndex(index) ?? 0; } @@ -566,9 +546,7 @@ public Point GetPositionFromCharIndex(int index) /// public int GetCharIndexFromPosition(Point pt) { - if (T == null) - return 0; - return T.GetCharIndexFromPosition(pt); + return T?.GetCharIndexFromPosition(pt) ?? 0; } @@ -577,9 +555,7 @@ public int GetCharIndexFromPosition(Point pt) /// public void ClearUndo() { - if (T == null) - return; - T.ClearUndo(); + T?.ClearUndo(); } @@ -588,9 +564,7 @@ public void ClearUndo() /// public void Copy() { - if (T == null) - return; - T.Copy(); + T?.Copy(); } @@ -599,9 +573,7 @@ public void Copy() /// public void Cut() { - if (T == null) - return; - T.Cut(); + T?.Cut(); } @@ -610,9 +582,7 @@ public void Cut() /// public void SelectAll() { - if (T == null) - return; - T.SelectAll(); + T?.SelectAll(); } @@ -621,9 +591,7 @@ public void SelectAll() /// public void DeselectAll() { - if (T == null) - return; - T.DeselectAll(); + T?.DeselectAll(); } @@ -633,9 +601,7 @@ public void DeselectAll() /// public void Paste(string clipFormat) { - if (T == null) - return; - T.Paste(clipFormat); + T?.Paste(clipFormat); } @@ -646,9 +612,7 @@ public void Paste(string clipFormat) /// public void Select(int start, int length) { - if (T == null) - return; - T.Select(start, length); + T?.Select(start, length); } #endregion @@ -859,8 +823,7 @@ public override string Text [Category("MetroSet Framework"), Description("Gets or sets the text in the TextBox while being empty.")] public string WatermarkText { - get - {return _watermarkText;} + get => _watermarkText; set { _watermarkText = value; @@ -945,7 +908,7 @@ public AutoCompleteMode AutoCompleteMode /// Gets or sets the font of the text displayed by the control. /// [Category("MetroSet Framework"), Description("Gets or sets the font of the text displayed by the control.")] - public override Font Font + public sealed override Font Font { get => base.Font; set @@ -1015,5 +978,7 @@ public override ContextMenuStrip ContextMenuStrip #endregion + + } } \ No newline at end of file