diff --git a/AutoTypeSearch/AutoTypeSearch.csproj b/AutoTypeSearch/AutoTypeSearch.csproj index c55c5b8..8572a12 100755 --- a/AutoTypeSearch/AutoTypeSearch.csproj +++ b/AutoTypeSearch/AutoTypeSearch.csproj @@ -54,6 +54,7 @@ + @@ -82,8 +83,14 @@ SearchWindow.cs + + Component + + + EditableStringList.cs + Options.cs diff --git a/AutoTypeSearch/AutoTypeSearch.csproj b/AutoTypeSearch/AutoTypeSearch.csproj index c55c5b8..8572a12 100755 --- a/AutoTypeSearch/AutoTypeSearch.csproj +++ b/AutoTypeSearch/AutoTypeSearch.csproj @@ -54,6 +54,7 @@ + @@ -82,8 +83,14 @@ SearchWindow.cs + + Component + + + EditableStringList.cs + Options.cs diff --git a/AutoTypeSearch/EditableStringList.cs b/AutoTypeSearch/EditableStringList.cs new file mode 100755 index 0000000..64b064d --- /dev/null +++ b/AutoTypeSearch/EditableStringList.cs @@ -0,0 +1,281 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace AutoTypeSearch +{ + class FinishEditEventArgs : EventArgs + { + public bool Accepted { get; private set; } + public FinishEditEventArgs(bool accepted) + { + Accepted = accepted; + } + } + delegate void FinishEditEventHandler(object sender, FinishEditEventArgs e); + + class EditableStringList : ListBox + { + private Button mDelete; + private TextBox mEdit; + + public EditableStringList() : base() + { + this.mDelete = new System.Windows.Forms.Button(); + this.mEdit = new System.Windows.Forms.TextBox(); + this.SuspendLayout(); + // + // mDelete + // + this.mDelete.Image = global::AutoTypeSearch.Properties.Resources.Delete; + this.mDelete.Location = new System.Drawing.Point(667, 46); + this.mDelete.Margin = new System.Windows.Forms.Padding(2); + this.mDelete.Name = "mDelete"; + this.mDelete.Size = new System.Drawing.Size(22, 24); + this.mDelete.TabIndex = 11; + this.mDelete.UseVisualStyleBackColor = true; + // + // mEdit + // + this.mEdit.Location = new System.Drawing.Point(566, 54); + this.mEdit.Margin = new System.Windows.Forms.Padding(2); + this.mEdit.Name = "mEdit"; + this.mEdit.Size = new System.Drawing.Size(76, 26); + this.mEdit.TabIndex = 10; + this.mEdit.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.mEdit_KeyPress); + this.mEdit.Leave += new EventHandler(this.mEdit_Leave); + this.ResumeLayout(false); + + this.DrawMode = DrawMode.OwnerDrawFixed; + + SetNewEntryItem(); + } + + public new void EndUpdate() + { + base.EndUpdate(); + + SetNewEntryItem(); + } + + private const int LB_ADDSTRING = 0x180; + private const int LB_INSERTSTRING = 0x181; + private const int LB_DELETESTRING = 0x182; + private const int LB_RESETCONTENT = 0x184; + + private object mNewEntry = null; + public int NewEntryIndex { get { return Items.IndexOf(mNewEntry); } } + private void SetNewEntryItem() + { + if (mNewEntry != null) + { + Items.Remove(mNewEntry); + } + + mNewEntry = Items[Items.Add("+ new sequence")]; + Control control = mNewEntry as Control; + if (control != null) + { + control.ForeColor = SystemColors.GrayText; + } + } + + private int mEditIndex; + + private bool mEditing = false; + public bool IsEditing { get { return mEditing; } } + + public event EventHandler StartEdit; + + private void CreateEditBox() + { + if (mEditing) return; + + if (SelectedIndex < 0 || SelectedIndex >= Items.Count) return; + + mEditing = true; + + int delta = 0; + + mEditIndex = SelectedIndex; + + Rectangle r = GetItemRectangle(mEditIndex); + + string itemText = (string)Items[mEditIndex]; + + bool editingNewEntry = Items[mEditIndex] == mNewEntry; + + if (editingNewEntry) + { + itemText = ""; + } + + mEdit.Location = new System.Drawing.Point(r.X + delta, r.Y + delta); + mEdit.Size = new System.Drawing.Size(r.Width - (editingNewEntry ? 0 : r.Height), r.Height - delta); + mEdit.Show(); + Controls.AddRange(new System.Windows.Forms.Control[] { this.mEdit }); + mEdit.Text = itemText; + mEdit.Focus(); + mEdit.SelectAll(); + + SetDeleteEntryButton(); + + StartEdit?.Invoke(this, EventArgs.Empty); + } + + public event FinishEditEventHandler FinishEdit; + + private bool mLeavingEdit = false; + private void FinishEditing(bool accepted = true) + { + if (!mEditing || mLeavingEdit) return; + mLeavingEdit = true; + + + BeginUpdate(); + if (mEdit.Text.Trim().Length == 0) + { + Items.RemoveAt(mEditIndex); + } + else if (accepted) + { + Items[mEditIndex] = mEdit.Text; + } + + SetNewEntryItem(); + + EndUpdate(); + + Focus(); + + mEdit.Hide(); + + SetDeleteEntryButton(); + + FinishEdit?.Invoke(this, new FinishEditEventArgs(accepted)); + + mEditing = false; + mLeavingEdit = false; + } + + private void SetDeleteEntryButton() + { + if (SelectedIndex < 0 || SelectedIndex >= Items.Count || SelectedItem == mNewEntry) + { + mDelete.Hide(); + } + else + { + Controls.AddRange(new System.Windows.Forms.Control[] { this.mDelete }); + mDelete.Show(); + + Rectangle r = GetItemRectangle(SelectedIndex); + + mDelete.Location = new Point(r.Right - r.Height, r.Y); + mDelete.Size = new Size(r.Height, r.Height); + } + } + + protected override void OnKeyDown(KeyEventArgs e) + { + if (e.KeyCode == Keys.Enter) + { + CreateEditBox(); + } + else if (e.KeyCode == Keys.Delete) + { + RemoveEntry(); + } + else + { + base.OnKeyDown(e); + } + } + + private void RemoveEntry() + { + if (SelectedIndex >= 0 && SelectedIndex < Items.Count && SelectedItem != mNewEntry) + { + Items.RemoveAt(SelectedIndex); + } + } + + private void mEdit_KeyPress(object sender, KeyPressEventArgs e) + { + if (e.KeyChar == 13) + { + FinishEditing(); + } + } + + protected override void OnDrawItem(DrawItemEventArgs e) + { + e.DrawBackground(); + Color fontColor = ForeColor; + if (e.Index == NewEntryIndex) + { + fontColor = SystemColors.GrayText; + } + if (Items.Count > 0) // Without this, I receive errors + { + e.Graphics.DrawString(Items[e.Index].ToString(), Font, new SolidBrush(fontColor), e.Bounds); + } + e.DrawFocusRectangle(); + } + + private void mAdditionalSequenceEdit_KeyDown(object sender, KeyEventArgs e) + { + if (e.KeyCode == Keys.Up) + { + FinishEditing(); + if (SelectedIndex > 0) + { + SelectedIndex--; + } + } + else if (e.KeyCode == Keys.Down) + { + FinishEditing(); + if (SelectedIndex < Items.Count - 1) + { + SelectedIndex++; + } + } + else if (e.KeyCode == Keys.Escape) + { + FinishEditing(false); + } + } + + private void mAdditionalSequenceDelete_Click(object sender, EventArgs e) + { + RemoveEntry(); + } + + private void mEdit_Leave(object sender, EventArgs e) + { + FinishEditing(); + } + + protected override void OnDoubleClick(EventArgs e) + { + CreateEditBox(); + } + + protected override void OnSelectedIndexChanged(EventArgs e) + { + SetDeleteEntryButton(); + + if (SelectedItem == mNewEntry) + { + CreateEditBox(); + } + + base.OnSelectedIndexChanged(e); + } + } +} diff --git a/AutoTypeSearch/AutoTypeSearch.csproj b/AutoTypeSearch/AutoTypeSearch.csproj index c55c5b8..8572a12 100755 --- a/AutoTypeSearch/AutoTypeSearch.csproj +++ b/AutoTypeSearch/AutoTypeSearch.csproj @@ -54,6 +54,7 @@ + @@ -82,8 +83,14 @@ SearchWindow.cs + + Component + + + EditableStringList.cs + Options.cs diff --git a/AutoTypeSearch/EditableStringList.cs b/AutoTypeSearch/EditableStringList.cs new file mode 100755 index 0000000..64b064d --- /dev/null +++ b/AutoTypeSearch/EditableStringList.cs @@ -0,0 +1,281 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace AutoTypeSearch +{ + class FinishEditEventArgs : EventArgs + { + public bool Accepted { get; private set; } + public FinishEditEventArgs(bool accepted) + { + Accepted = accepted; + } + } + delegate void FinishEditEventHandler(object sender, FinishEditEventArgs e); + + class EditableStringList : ListBox + { + private Button mDelete; + private TextBox mEdit; + + public EditableStringList() : base() + { + this.mDelete = new System.Windows.Forms.Button(); + this.mEdit = new System.Windows.Forms.TextBox(); + this.SuspendLayout(); + // + // mDelete + // + this.mDelete.Image = global::AutoTypeSearch.Properties.Resources.Delete; + this.mDelete.Location = new System.Drawing.Point(667, 46); + this.mDelete.Margin = new System.Windows.Forms.Padding(2); + this.mDelete.Name = "mDelete"; + this.mDelete.Size = new System.Drawing.Size(22, 24); + this.mDelete.TabIndex = 11; + this.mDelete.UseVisualStyleBackColor = true; + // + // mEdit + // + this.mEdit.Location = new System.Drawing.Point(566, 54); + this.mEdit.Margin = new System.Windows.Forms.Padding(2); + this.mEdit.Name = "mEdit"; + this.mEdit.Size = new System.Drawing.Size(76, 26); + this.mEdit.TabIndex = 10; + this.mEdit.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.mEdit_KeyPress); + this.mEdit.Leave += new EventHandler(this.mEdit_Leave); + this.ResumeLayout(false); + + this.DrawMode = DrawMode.OwnerDrawFixed; + + SetNewEntryItem(); + } + + public new void EndUpdate() + { + base.EndUpdate(); + + SetNewEntryItem(); + } + + private const int LB_ADDSTRING = 0x180; + private const int LB_INSERTSTRING = 0x181; + private const int LB_DELETESTRING = 0x182; + private const int LB_RESETCONTENT = 0x184; + + private object mNewEntry = null; + public int NewEntryIndex { get { return Items.IndexOf(mNewEntry); } } + private void SetNewEntryItem() + { + if (mNewEntry != null) + { + Items.Remove(mNewEntry); + } + + mNewEntry = Items[Items.Add("+ new sequence")]; + Control control = mNewEntry as Control; + if (control != null) + { + control.ForeColor = SystemColors.GrayText; + } + } + + private int mEditIndex; + + private bool mEditing = false; + public bool IsEditing { get { return mEditing; } } + + public event EventHandler StartEdit; + + private void CreateEditBox() + { + if (mEditing) return; + + if (SelectedIndex < 0 || SelectedIndex >= Items.Count) return; + + mEditing = true; + + int delta = 0; + + mEditIndex = SelectedIndex; + + Rectangle r = GetItemRectangle(mEditIndex); + + string itemText = (string)Items[mEditIndex]; + + bool editingNewEntry = Items[mEditIndex] == mNewEntry; + + if (editingNewEntry) + { + itemText = ""; + } + + mEdit.Location = new System.Drawing.Point(r.X + delta, r.Y + delta); + mEdit.Size = new System.Drawing.Size(r.Width - (editingNewEntry ? 0 : r.Height), r.Height - delta); + mEdit.Show(); + Controls.AddRange(new System.Windows.Forms.Control[] { this.mEdit }); + mEdit.Text = itemText; + mEdit.Focus(); + mEdit.SelectAll(); + + SetDeleteEntryButton(); + + StartEdit?.Invoke(this, EventArgs.Empty); + } + + public event FinishEditEventHandler FinishEdit; + + private bool mLeavingEdit = false; + private void FinishEditing(bool accepted = true) + { + if (!mEditing || mLeavingEdit) return; + mLeavingEdit = true; + + + BeginUpdate(); + if (mEdit.Text.Trim().Length == 0) + { + Items.RemoveAt(mEditIndex); + } + else if (accepted) + { + Items[mEditIndex] = mEdit.Text; + } + + SetNewEntryItem(); + + EndUpdate(); + + Focus(); + + mEdit.Hide(); + + SetDeleteEntryButton(); + + FinishEdit?.Invoke(this, new FinishEditEventArgs(accepted)); + + mEditing = false; + mLeavingEdit = false; + } + + private void SetDeleteEntryButton() + { + if (SelectedIndex < 0 || SelectedIndex >= Items.Count || SelectedItem == mNewEntry) + { + mDelete.Hide(); + } + else + { + Controls.AddRange(new System.Windows.Forms.Control[] { this.mDelete }); + mDelete.Show(); + + Rectangle r = GetItemRectangle(SelectedIndex); + + mDelete.Location = new Point(r.Right - r.Height, r.Y); + mDelete.Size = new Size(r.Height, r.Height); + } + } + + protected override void OnKeyDown(KeyEventArgs e) + { + if (e.KeyCode == Keys.Enter) + { + CreateEditBox(); + } + else if (e.KeyCode == Keys.Delete) + { + RemoveEntry(); + } + else + { + base.OnKeyDown(e); + } + } + + private void RemoveEntry() + { + if (SelectedIndex >= 0 && SelectedIndex < Items.Count && SelectedItem != mNewEntry) + { + Items.RemoveAt(SelectedIndex); + } + } + + private void mEdit_KeyPress(object sender, KeyPressEventArgs e) + { + if (e.KeyChar == 13) + { + FinishEditing(); + } + } + + protected override void OnDrawItem(DrawItemEventArgs e) + { + e.DrawBackground(); + Color fontColor = ForeColor; + if (e.Index == NewEntryIndex) + { + fontColor = SystemColors.GrayText; + } + if (Items.Count > 0) // Without this, I receive errors + { + e.Graphics.DrawString(Items[e.Index].ToString(), Font, new SolidBrush(fontColor), e.Bounds); + } + e.DrawFocusRectangle(); + } + + private void mAdditionalSequenceEdit_KeyDown(object sender, KeyEventArgs e) + { + if (e.KeyCode == Keys.Up) + { + FinishEditing(); + if (SelectedIndex > 0) + { + SelectedIndex--; + } + } + else if (e.KeyCode == Keys.Down) + { + FinishEditing(); + if (SelectedIndex < Items.Count - 1) + { + SelectedIndex++; + } + } + else if (e.KeyCode == Keys.Escape) + { + FinishEditing(false); + } + } + + private void mAdditionalSequenceDelete_Click(object sender, EventArgs e) + { + RemoveEntry(); + } + + private void mEdit_Leave(object sender, EventArgs e) + { + FinishEditing(); + } + + protected override void OnDoubleClick(EventArgs e) + { + CreateEditBox(); + } + + protected override void OnSelectedIndexChanged(EventArgs e) + { + SetDeleteEntryButton(); + + if (SelectedItem == mNewEntry) + { + CreateEditBox(); + } + + base.OnSelectedIndexChanged(e); + } + } +} diff --git a/AutoTypeSearch/EditableStringList.resx b/AutoTypeSearch/EditableStringList.resx new file mode 100755 index 0000000..2f1e86b --- /dev/null +++ b/AutoTypeSearch/EditableStringList.resx @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 65 + + + 161, 22 + + + False + + \ No newline at end of file diff --git a/AutoTypeSearch/AutoTypeSearch.csproj b/AutoTypeSearch/AutoTypeSearch.csproj index c55c5b8..8572a12 100755 --- a/AutoTypeSearch/AutoTypeSearch.csproj +++ b/AutoTypeSearch/AutoTypeSearch.csproj @@ -54,6 +54,7 @@ + @@ -82,8 +83,14 @@ SearchWindow.cs + + Component + + + EditableStringList.cs + Options.cs diff --git a/AutoTypeSearch/EditableStringList.cs b/AutoTypeSearch/EditableStringList.cs new file mode 100755 index 0000000..64b064d --- /dev/null +++ b/AutoTypeSearch/EditableStringList.cs @@ -0,0 +1,281 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace AutoTypeSearch +{ + class FinishEditEventArgs : EventArgs + { + public bool Accepted { get; private set; } + public FinishEditEventArgs(bool accepted) + { + Accepted = accepted; + } + } + delegate void FinishEditEventHandler(object sender, FinishEditEventArgs e); + + class EditableStringList : ListBox + { + private Button mDelete; + private TextBox mEdit; + + public EditableStringList() : base() + { + this.mDelete = new System.Windows.Forms.Button(); + this.mEdit = new System.Windows.Forms.TextBox(); + this.SuspendLayout(); + // + // mDelete + // + this.mDelete.Image = global::AutoTypeSearch.Properties.Resources.Delete; + this.mDelete.Location = new System.Drawing.Point(667, 46); + this.mDelete.Margin = new System.Windows.Forms.Padding(2); + this.mDelete.Name = "mDelete"; + this.mDelete.Size = new System.Drawing.Size(22, 24); + this.mDelete.TabIndex = 11; + this.mDelete.UseVisualStyleBackColor = true; + // + // mEdit + // + this.mEdit.Location = new System.Drawing.Point(566, 54); + this.mEdit.Margin = new System.Windows.Forms.Padding(2); + this.mEdit.Name = "mEdit"; + this.mEdit.Size = new System.Drawing.Size(76, 26); + this.mEdit.TabIndex = 10; + this.mEdit.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.mEdit_KeyPress); + this.mEdit.Leave += new EventHandler(this.mEdit_Leave); + this.ResumeLayout(false); + + this.DrawMode = DrawMode.OwnerDrawFixed; + + SetNewEntryItem(); + } + + public new void EndUpdate() + { + base.EndUpdate(); + + SetNewEntryItem(); + } + + private const int LB_ADDSTRING = 0x180; + private const int LB_INSERTSTRING = 0x181; + private const int LB_DELETESTRING = 0x182; + private const int LB_RESETCONTENT = 0x184; + + private object mNewEntry = null; + public int NewEntryIndex { get { return Items.IndexOf(mNewEntry); } } + private void SetNewEntryItem() + { + if (mNewEntry != null) + { + Items.Remove(mNewEntry); + } + + mNewEntry = Items[Items.Add("+ new sequence")]; + Control control = mNewEntry as Control; + if (control != null) + { + control.ForeColor = SystemColors.GrayText; + } + } + + private int mEditIndex; + + private bool mEditing = false; + public bool IsEditing { get { return mEditing; } } + + public event EventHandler StartEdit; + + private void CreateEditBox() + { + if (mEditing) return; + + if (SelectedIndex < 0 || SelectedIndex >= Items.Count) return; + + mEditing = true; + + int delta = 0; + + mEditIndex = SelectedIndex; + + Rectangle r = GetItemRectangle(mEditIndex); + + string itemText = (string)Items[mEditIndex]; + + bool editingNewEntry = Items[mEditIndex] == mNewEntry; + + if (editingNewEntry) + { + itemText = ""; + } + + mEdit.Location = new System.Drawing.Point(r.X + delta, r.Y + delta); + mEdit.Size = new System.Drawing.Size(r.Width - (editingNewEntry ? 0 : r.Height), r.Height - delta); + mEdit.Show(); + Controls.AddRange(new System.Windows.Forms.Control[] { this.mEdit }); + mEdit.Text = itemText; + mEdit.Focus(); + mEdit.SelectAll(); + + SetDeleteEntryButton(); + + StartEdit?.Invoke(this, EventArgs.Empty); + } + + public event FinishEditEventHandler FinishEdit; + + private bool mLeavingEdit = false; + private void FinishEditing(bool accepted = true) + { + if (!mEditing || mLeavingEdit) return; + mLeavingEdit = true; + + + BeginUpdate(); + if (mEdit.Text.Trim().Length == 0) + { + Items.RemoveAt(mEditIndex); + } + else if (accepted) + { + Items[mEditIndex] = mEdit.Text; + } + + SetNewEntryItem(); + + EndUpdate(); + + Focus(); + + mEdit.Hide(); + + SetDeleteEntryButton(); + + FinishEdit?.Invoke(this, new FinishEditEventArgs(accepted)); + + mEditing = false; + mLeavingEdit = false; + } + + private void SetDeleteEntryButton() + { + if (SelectedIndex < 0 || SelectedIndex >= Items.Count || SelectedItem == mNewEntry) + { + mDelete.Hide(); + } + else + { + Controls.AddRange(new System.Windows.Forms.Control[] { this.mDelete }); + mDelete.Show(); + + Rectangle r = GetItemRectangle(SelectedIndex); + + mDelete.Location = new Point(r.Right - r.Height, r.Y); + mDelete.Size = new Size(r.Height, r.Height); + } + } + + protected override void OnKeyDown(KeyEventArgs e) + { + if (e.KeyCode == Keys.Enter) + { + CreateEditBox(); + } + else if (e.KeyCode == Keys.Delete) + { + RemoveEntry(); + } + else + { + base.OnKeyDown(e); + } + } + + private void RemoveEntry() + { + if (SelectedIndex >= 0 && SelectedIndex < Items.Count && SelectedItem != mNewEntry) + { + Items.RemoveAt(SelectedIndex); + } + } + + private void mEdit_KeyPress(object sender, KeyPressEventArgs e) + { + if (e.KeyChar == 13) + { + FinishEditing(); + } + } + + protected override void OnDrawItem(DrawItemEventArgs e) + { + e.DrawBackground(); + Color fontColor = ForeColor; + if (e.Index == NewEntryIndex) + { + fontColor = SystemColors.GrayText; + } + if (Items.Count > 0) // Without this, I receive errors + { + e.Graphics.DrawString(Items[e.Index].ToString(), Font, new SolidBrush(fontColor), e.Bounds); + } + e.DrawFocusRectangle(); + } + + private void mAdditionalSequenceEdit_KeyDown(object sender, KeyEventArgs e) + { + if (e.KeyCode == Keys.Up) + { + FinishEditing(); + if (SelectedIndex > 0) + { + SelectedIndex--; + } + } + else if (e.KeyCode == Keys.Down) + { + FinishEditing(); + if (SelectedIndex < Items.Count - 1) + { + SelectedIndex++; + } + } + else if (e.KeyCode == Keys.Escape) + { + FinishEditing(false); + } + } + + private void mAdditionalSequenceDelete_Click(object sender, EventArgs e) + { + RemoveEntry(); + } + + private void mEdit_Leave(object sender, EventArgs e) + { + FinishEditing(); + } + + protected override void OnDoubleClick(EventArgs e) + { + CreateEditBox(); + } + + protected override void OnSelectedIndexChanged(EventArgs e) + { + SetDeleteEntryButton(); + + if (SelectedItem == mNewEntry) + { + CreateEditBox(); + } + + base.OnSelectedIndexChanged(e); + } + } +} diff --git a/AutoTypeSearch/EditableStringList.resx b/AutoTypeSearch/EditableStringList.resx new file mode 100755 index 0000000..2f1e86b --- /dev/null +++ b/AutoTypeSearch/EditableStringList.resx @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 65 + + + 161, 22 + + + False + + \ No newline at end of file diff --git a/AutoTypeSearch/WatermarkTextBox.cs b/AutoTypeSearch/WatermarkTextBox.cs new file mode 100755 index 0000000..a867368 --- /dev/null +++ b/AutoTypeSearch/WatermarkTextBox.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace AutoTypeSearch +{ + // https://www.codeproject.com/Articles/27849/WaterMark-TextBox-For-Desktop-Applications-Using-C + + class WatermarkTextBox : TextBox + { + private Font mOldFont = null; + private Boolean mWaterMarkTextEnabled = false; + + #region Attributes + private Color _waterMarkColor = Color.Gray; + public Color WaterMarkColor + { + get { return _waterMarkColor; } + set + { + _waterMarkColor = value; Invalidate();/*thanks to Bernhard Elbl + for Invalidate()*/ + } + } + + private string _waterMarkText = "Water Mark"; + public string WaterMarkText + { + get { return _waterMarkText; } + set { _waterMarkText = value; Invalidate(); } + } + #endregion + + //Default constructor + public WatermarkTextBox() + { + JoinEvents(true); + } + + //Override OnCreateControl ... thanks to "lpgray .. codeproject guy" + protected override void OnCreateControl() + { + base.OnCreateControl(); + WaterMarkToggle(null, null); + } + + //Override OnPaint + protected override void OnPaint(PaintEventArgs args) + { + // Use the same font that was defined in base class + System.Drawing.Font drawFont = new System.Drawing.Font(Font.FontFamily, + Font.Size, Font.Style, Font.Unit); + //Create new brush with gray color or + SolidBrush drawBrush = new SolidBrush(WaterMarkColor);//use Water mark color + //Draw Text or WaterMark + args.Graphics.DrawString((mWaterMarkTextEnabled ? WaterMarkText : Text), + drawFont, drawBrush, new PointF(0.0F, 0.0F)); + base.OnPaint(args); + } + + private void JoinEvents(Boolean join) + { + if (join) + { + this.TextChanged += new System.EventHandler(this.WaterMarkToggle); + this.LostFocus += new System.EventHandler(this.WaterMarkToggle); + this.FontChanged += new System.EventHandler(this.WaterMark_FontChanged); + //No one of the above events will start immeddiatlly + //TextBox control still in constructing, so, + //Font object (for example) couldn't be catched from within + //WaterMark_Toggle + //So, call WaterMark_Toggel through OnCreateControl after TextBox + //is totally created + //No doupt, it will be only one time call + + //Old solution uses Timer.Tick event to check Create property + } + } + + private void WaterMarkToggle(object sender, EventArgs args) + { + if (this.Text.Length <= 0) + EnableWaterMark(); + else + DisableWaterMark(); + } + + private void EnableWaterMark() + { + //Save current font until returning the UserPaint style to false (NOTE: + //It is a try and error advice) + mOldFont = new System.Drawing.Font(Font.FontFamily, Font.Size, Font.Style, + Font.Unit); + //Enable OnPaint event handler + this.SetStyle(ControlStyles.UserPaint, true); + this.mWaterMarkTextEnabled = true; + //Triger OnPaint immediatly + Refresh(); + } + + private void DisableWaterMark() + { + //Disbale OnPaint event handler + this.mWaterMarkTextEnabled = false; + this.SetStyle(ControlStyles.UserPaint, false); + //Return back oldFont if existed + if (mOldFont != null) + this.Font = new System.Drawing.Font(mOldFont.FontFamily, mOldFont.Size, + mOldFont.Style, mOldFont.Unit); + } + + private void WaterMark_FontChanged(object sender, EventArgs args) + { + if (mWaterMarkTextEnabled) + { + mOldFont = new System.Drawing.Font(Font.FontFamily, Font.Size, Font.Style, + Font.Unit); + Refresh(); + } + } + } +}