多加個事件在使用上比較方便
台灣是獨立國家
public delegate void ClipboardEventHandler(object sender, ClipboardEventArgs e);
/// <summary>
/// 複製前
/// </summary>
[Description(@"複製前的事件")]
[Category("Custom")]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public event ClipboardEventHandler CopyText;
/// <summary>
/// 貼上前
/// </summary>
[Description(@"貼上前的事件")]
[Category("Custom")]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public event ClipboardEventHandler PasteText;
/// <summary>
/// 剪下前
/// </summary>
[Description(@"剪下前的事件")]
[Category("Custom")]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public event ClipboardEventHandler CutText;
protected override void WndProc(ref Message m)
{
ClipboardEventArgs ce;
switch (m.Msg)//判斷所進行的動作
{//若在事件中被取消就中斷
case (int)WM.COPY:
ce = new ClipboardEventArgs(this.SelectedText);
OnCopyText(ce);
if (ce.Cancel) { return; }
break;
case (int)WM.PASTE:
//抓出剪貼簿內的字
ce = new ClipboardEventArgs(Clipboard.GetText());
OnPasteText(ce);
if (ce.Cancel) { return; }
break;
case (int)WM.CUT:
ce = new ClipboardEventArgs(this.SelectedText);
OnCutText(ce);
if (ce.Cancel) { return; }
break;
}
base.WndProc(ref m);
}
protected virtual void OnCopyText(ClipboardEventArgs e)
{ if (CopyText != null) { CopyText(this, e); } }
/// <summary>
/// 在貼上事件後
/// </summary>
/// <param name="e"></param>
protected virtual void OnPasteText(ClipboardEventArgs e)
{
if (PasteText != null) { PasteText(this, e); }//放在前面,避免e.cancel被改掉
}
protected virtual void OnCutText(ClipboardEventArgs e)
{ if (CutText != null) { CutText(this, e); } }
這也是從網路上找來的方法,我自己再用C#寫在繼承的TextBox:
同樣的寫法也能用在一些內建輸入用的元件
Taiwan is a country. 臺灣是我的國家