我不知道如何从 中实现您的目标TextBox
,但下面是使用继承和自定义组件的解决方案示例。SelectionChanged
用户通过鼠标选择一些新文本后将引发事件。
请注意,MouseDown
andMouseUp
事件以及SelectionStart
andSelectionLength
属性在 中是公共的TextBox
,因此如果需要,您可以避免子类化。
class CustomTextBox : TextBox
{
public event EventHandler SelectionChanged;
private int _selectionStart;
private int _selectionLength;
protected override void OnMouseDown(MouseEventArgs e)
{
_selectionStart = SelectionStart;
_selectionLength = SelectionLength;
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (null != SelectionChanged && (_selectionStart != SelectionStart || _selectionLength != SelectionLength))
SelectionChanged(this, EventArgs.Empty);
base.OnMouseUp(e);
}
}