Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextBox, MultiLineTextBox & MaskedTextBox Context menu improvements #233

Merged
merged 5 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 3 additions & 90 deletions MaterialSkin/Controls/BaseTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace MaterialSkin.Controls
[ToolboxItem(false)]
public class BaseTextBox : TextBox, IMaterialControl
{
#region "Public Properties"

//Properties for managing the material design properties
[Browsable(false)]
public int Depth { get; set; }
Expand Down Expand Up @@ -38,13 +40,10 @@ public string Hint
});
}

#endregion

public BaseTextBox()
{
MaterialContextMenuStrip cms = new BaseTextBoxContextMenuStrip();
cms.Opening += ContextMenuStripOnOpening;
cms.OnItemClickStart += ContextMenuStripOnItemClickStart;
ContextMenuStrip = cms;
}


Expand Down Expand Up @@ -108,46 +107,6 @@ protected override void WndProc(ref Message m)

}


private void ContextMenuStripOnItemClickStart(object sender, ToolStripItemClickedEventArgs toolStripItemClickedEventArgs)
{
switch (toolStripItemClickedEventArgs.ClickedItem.Text)
{
case "Undo":
Undo();
break;
case "Cut":
Cut();
break;
case "Copy":
Copy();
break;
case "Paste":
Paste();
break;
case "Delete":
SelectedText = string.Empty;
break;
case "Select All":
SelectAll();
break;
}
}

private void ContextMenuStripOnOpening(object sender, CancelEventArgs cancelEventArgs)
{
var strip = sender as BaseTextBoxContextMenuStrip;
if (strip != null)
{
strip.undo.Enabled = CanUndo;
strip.cut.Enabled = !string.IsNullOrEmpty(SelectedText);
strip.copy.Enabled = !string.IsNullOrEmpty(SelectedText);
strip.paste.Enabled = Clipboard.ContainsText();
strip.delete.Enabled = !string.IsNullOrEmpty(SelectedText);
strip.selectAll.Enabled = !string.IsNullOrEmpty(Text);
}
}

}

[ToolboxItem(false)]
Expand Down Expand Up @@ -186,13 +145,8 @@ public string Hint

public BaseMaskedTextBox()
{
MaterialContextMenuStrip cms = new BaseTextBoxContextMenuStrip();
cms.Opening += ContextMenuStripOnOpening;
cms.OnItemClickStart += ContextMenuStripOnItemClickStart;
ContextMenuStrip = cms;
}


protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
Expand Down Expand Up @@ -252,47 +206,6 @@ protected override void WndProc(ref Message m)
}

}


private void ContextMenuStripOnItemClickStart(object sender, ToolStripItemClickedEventArgs toolStripItemClickedEventArgs)
{
switch (toolStripItemClickedEventArgs.ClickedItem.Text)
{
case "Undo":
Undo();
break;
case "Cut":
Cut();
break;
case "Copy":
Copy();
break;
case "Paste":
Paste();
break;
case "Delete":
SelectedText = string.Empty;
break;
case "Select All":
SelectAll();
break;
}
}

private void ContextMenuStripOnOpening(object sender, CancelEventArgs cancelEventArgs)
{
var strip = sender as BaseTextBoxContextMenuStrip;
if (strip != null)
{
strip.undo.Enabled = CanUndo;
strip.cut.Enabled = !string.IsNullOrEmpty(SelectedText);
strip.copy.Enabled = !string.IsNullOrEmpty(SelectedText);
strip.paste.Enabled = Clipboard.ContainsText();
strip.delete.Enabled = !string.IsNullOrEmpty(SelectedText);
strip.selectAll.Enabled = !string.IsNullOrEmpty(Text);
}
}

}

[ToolboxItem(false)]
Expand Down
105 changes: 98 additions & 7 deletions MaterialSkin/Controls/MaterialMaskedTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

public class MaterialMaskedTextBox : Control, IMaterialControl
{

MaterialContextMenuStrip cms = new BaseTextBoxContextMenuStrip();
ContextMenuStrip _lastContextMenuStrip = new ContextMenuStrip();

//Properties for managing the material design properties
[Browsable(false)]
public int Depth { get; set; }
Expand All @@ -32,8 +36,10 @@ public class MaterialMaskedTextBox : Control, IMaterialControl

[Browsable(false)]
public int SelectionStart { get { return baseTextBox.SelectionStart; } set { baseTextBox.SelectionStart = value; } }

[Browsable(false)]
public int SelectionLength { get { return baseTextBox.SelectionLength; } set { baseTextBox.SelectionLength = value; } }

[Browsable(false)]
public int TextLength { get { return baseTextBox.TextLength; } }

Expand Down Expand Up @@ -156,6 +162,25 @@ public string PrefixSuffixText

//TextBox properties

public override ContextMenuStrip ContextMenuStrip
{
get { return baseTextBox.ContextMenuStrip; }
set
{
if (value != null)
{
baseTextBox.ContextMenuStrip = value;
base.ContextMenuStrip = value;
}
else
{
baseTextBox.ContextMenuStrip = cms;
base.ContextMenuStrip = cms;
}
_lastContextMenuStrip = base.ContextMenuStrip;
}
}

[Browsable(false)]
public override Color BackColor { get { return Parent == null ? SkinManager.BackgroundColor : Parent.BackColor; } }

Expand Down Expand Up @@ -210,7 +235,25 @@ public string PrefixSuffixText
public bool ResetOnSpace { get { return baseTextBox.ResetOnSpace; } set { baseTextBox.ResetOnSpace = value; } }

[Category("Behavior")]
public bool ShortcutsEnabled { get { return baseTextBox.ShortcutsEnabled; } set { baseTextBox.ShortcutsEnabled = value; } }
public bool ShortcutsEnabled
{
get
{ return baseTextBox.ShortcutsEnabled; }
set
{
baseTextBox.ShortcutsEnabled = value;
if (value == false)
{
baseTextBox.ContextMenuStrip = null;
base.ContextMenuStrip = null;
}
else
{
baseTextBox.ContextMenuStrip = _lastContextMenuStrip;
base.ContextMenuStrip = _lastContextMenuStrip;
}
}
}

[Category("Behavior")]
public bool SkipLiterals { get { return baseTextBox.SkipLiterals; } set { baseTextBox.SkipLiterals = value; } }
Expand Down Expand Up @@ -268,6 +311,10 @@ public bool ReadOnly

public void Cut() { baseTextBox.Cut(); }

public void Undo() { baseTextBox.Undo(); }

public void Paste() { baseTextBox.Paste(); }

#region "Events"

[Category("Action")]
Expand Down Expand Up @@ -1158,27 +1205,27 @@ public event EventHandler ReadOnlyChanged
}
}

public new event EventHandler TextChanged
public event EventHandler TextAlignChanged
{
add
{
baseTextBox.TextChanged += value;
baseTextBox.TextAlignChanged += value;
}
remove
{
baseTextBox.TextChanged -= value;
baseTextBox.TextAlignChanged -= value;
}
}

public event EventHandler TextAlignChanged
public new event EventHandler TextChanged
{
add
{
baseTextBox.TextAlignChanged += value;
baseTextBox.TextChanged += value;
}
remove
{
baseTextBox.TextAlignChanged -= value;
baseTextBox.TextChanged -= value;
}
}

Expand Down Expand Up @@ -1353,6 +1400,11 @@ public MaterialMaskedTextBox()

baseTextBox.TabStop = true;
this.TabStop = false;

cms.Opening += ContextMenuStripOnOpening;
cms.OnItemClickStart += ContextMenuStripOnItemClickStart;
ContextMenuStrip = cms;

}

private void Redraw(object sencer, EventArgs e)
Expand Down Expand Up @@ -1815,5 +1867,44 @@ public bool GetErrorState()
return _errorState;
}

private void ContextMenuStripOnItemClickStart(object sender, ToolStripItemClickedEventArgs toolStripItemClickedEventArgs)
{
switch (toolStripItemClickedEventArgs.ClickedItem.Text)
{
case "Undo":
Undo();
break;
case "Cut":
Cut();
break;
case "Copy":
Copy();
break;
case "Paste":
Paste();
break;
case "Delete":
SelectedText = string.Empty;
break;
case "Select All":
SelectAll();
break;
}
}

private void ContextMenuStripOnOpening(object sender, CancelEventArgs cancelEventArgs)
{
var strip = sender as BaseTextBoxContextMenuStrip;
if (strip != null)
{
strip.undo.Enabled = baseTextBox.CanUndo && !ReadOnly;
strip.cut.Enabled = !string.IsNullOrEmpty(SelectedText) && !ReadOnly;
strip.copy.Enabled = !string.IsNullOrEmpty(SelectedText);
strip.paste.Enabled = Clipboard.ContainsText() && !ReadOnly;
strip.delete.Enabled = !string.IsNullOrEmpty(SelectedText) && !ReadOnly;
strip.selectAll.Enabled = !string.IsNullOrEmpty(Text);
}
}

}
}
Loading