文本框中是否有选择文本事件?(Is there selection text event in text box)

我正在创建一个小文本编辑器(就像记事本)。我的表单上有几个按钮(剪切,删除,复制)。 我希望他们在没有选择文本时无法使用,反之亦然......文本选择时是否发生了某些事件? 我使用文本框控件。

I'm creating a little text editor(just like notepad).I have a few buttons on my form(cut, delete, copy). I want them to be unable when there's no text selected and vice versa...Is there some event that happens when the text is selecting? I use text box control.

最满意答案

没有这样的事件,但幸运的是有解决方法:

1)通过你自己更新的UI在Application.Idle事件上做到这一点(我承认这不是最好的解决方案,但它比我最喜欢的更常见,因为它更容易实现):

Application.Idle += OnIdle;

接着:

private void OnIdle(object sender, EventArgs e) { btnCopy.Enabled = txtEditor.SelectionLength > 0; }

2)从RichTextControl导出你自己的类(如果你必须处理巨大的文件而不是最好的解决方案,则不是最好的解决方案),并处理EN_SELCHANGE通知(最强大的通知也与我看到的每个IME兼容)。 概念验证(从MSDN中选择合适的值,不要忘记用EM_SETEVENTMASK设置ENM_SELCHANGE ):

public class TextBoxEx : TextBox { public event EventHandler SelectionChanged; protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_NOTIFY && m.lParam == EN_SELCHANGE) { OnSelectionChanged(EventArgs.Empty); } } // ... }

你可能会这样做,但...默认控件已经为你提供了这个功能:它有一个SelectionChanged事件。

如果您还支持剪贴板粘贴,请小心,因为您需要根据剪贴板内容更新粘贴按钮(然后再次在Application.Idle更容易的位置)。 在RichTextControl上调用CanPaste()和类似方法可能会破坏一些IME( 另请参阅In Idle无法访问RichTextControl或IME不起作用 )。

There is not such event but fortunately there are workarounds:

1) Do it by your own updating UI on Application.Idle event (I admit this isn't best solution but it's more often than not my favorite because it's easier to implement):

Application.Idle += OnIdle;

And then:

private void OnIdle(object sender, EventArgs e) { btnCopy.Enabled = txtEditor.SelectionLength > 0; }

2) Derive your own class from RichTextControl (not best solution if you have to handle huge - not just big - files) and handle EN_SELCHANGE notification (most robust one also compatible with every IME I saw around). Proof of concept (pick proper values from MSDN and don't forget to set ENM_SELCHANGE with EM_SETEVENTMASK):

public class TextBoxEx : TextBox { public event EventHandler SelectionChanged; protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_NOTIFY && m.lParam == EN_SELCHANGE) { OnSelectionChanged(EventArgs.Empty); } } // ... }

You might do it but...default control already has this feature for you: it has a SelectionChanged event.

Be careful if you also support clipboard pasting because you need to update your paste button according to clipboard content (then easier place is again in Application.Idle). Calling CanPaste() and similar methods on RichTextControl may break some IMEs (see also In Idle cannot access RichTextControl or IME will not work).

文本框中是否有选择文本事件?(Is there selection text event in text box)

我正在创建一个小文本编辑器(就像记事本)。我的表单上有几个按钮(剪切,删除,复制)。 我希望他们在没有选择文本时无法使用,反之亦然......文本选择时是否发生了某些事件? 我使用文本框控件。

I'm creating a little text editor(just like notepad).I have a few buttons on my form(cut, delete, copy). I want them to be unable when there's no text selected and vice versa...Is there some event that happens when the text is selecting? I use text box control.

最满意答案

没有这样的事件,但幸运的是有解决方法:

1)通过你自己更新的UI在Application.Idle事件上做到这一点(我承认这不是最好的解决方案,但它比我最喜欢的更常见,因为它更容易实现):

Application.Idle += OnIdle;

接着:

private void OnIdle(object sender, EventArgs e) { btnCopy.Enabled = txtEditor.SelectionLength > 0; }

2)从RichTextControl导出你自己的类(如果你必须处理巨大的文件而不是最好的解决方案,则不是最好的解决方案),并处理EN_SELCHANGE通知(最强大的通知也与我看到的每个IME兼容)。 概念验证(从MSDN中选择合适的值,不要忘记用EM_SETEVENTMASK设置ENM_SELCHANGE ):

public class TextBoxEx : TextBox { public event EventHandler SelectionChanged; protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_NOTIFY && m.lParam == EN_SELCHANGE) { OnSelectionChanged(EventArgs.Empty); } } // ... }

你可能会这样做,但...默认控件已经为你提供了这个功能:它有一个SelectionChanged事件。

如果您还支持剪贴板粘贴,请小心,因为您需要根据剪贴板内容更新粘贴按钮(然后再次在Application.Idle更容易的位置)。 在RichTextControl上调用CanPaste()和类似方法可能会破坏一些IME( 另请参阅In Idle无法访问RichTextControl或IME不起作用 )。

There is not such event but fortunately there are workarounds:

1) Do it by your own updating UI on Application.Idle event (I admit this isn't best solution but it's more often than not my favorite because it's easier to implement):

Application.Idle += OnIdle;

And then:

private void OnIdle(object sender, EventArgs e) { btnCopy.Enabled = txtEditor.SelectionLength > 0; }

2) Derive your own class from RichTextControl (not best solution if you have to handle huge - not just big - files) and handle EN_SELCHANGE notification (most robust one also compatible with every IME I saw around). Proof of concept (pick proper values from MSDN and don't forget to set ENM_SELCHANGE with EM_SETEVENTMASK):

public class TextBoxEx : TextBox { public event EventHandler SelectionChanged; protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_NOTIFY && m.lParam == EN_SELCHANGE) { OnSelectionChanged(EventArgs.Empty); } } // ... }

You might do it but...default control already has this feature for you: it has a SelectionChanged event.

Be careful if you also support clipboard pasting because you need to update your paste button according to clipboard content (then easier place is again in Application.Idle). Calling CanPaste() and similar methods on RichTextControl may break some IMEs (see also In Idle cannot access RichTextControl or IME will not work).