覆盖粘贴到文本框
我想在特定文本框中覆盖粘贴功能。当文本粘贴到该文本框中时,我希望它执行以下操作:(
AddressTextBox.Text = Clipboard.GetText().Replace(Environment.NewLine, " ");
从多行更改为单行)
我该如何执行此操作?
I want to override the paste function when in a specific textbox. When text is pasted into that textbox, I want it to execute the following:
AddressTextBox.Text = Clipboard.GetText().Replace(Environment.NewLine, " ");
(Changing from multiline to single)
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是可能的,您可以拦截本机
TextBox
控件获取的低级 Windows 消息,告诉它从剪贴板粘贴。WM_PASTE
消息。当您使用键盘按 Ctrl+V 或使用上下文菜单的粘贴命令时都会生成。您可以通过重写控件的WndProc()
方法来捕获它,根据需要执行粘贴,并且不将其传递给基类。将新类添加到您的项目中并复制/粘贴下面所示的代码。编译。将新控件从工具箱顶部拖放到窗体上,替换现有控件。
That's possible, you can intercept the low-level Windows message that the native
TextBox
control gets that tells it to paste from the clipboard. TheWM_PASTE
message. Generated both when you press Ctrl+V with the keyboard or use the context menu's Paste command. You catch it by overriding the control'sWndProc()
method, performing the paste as desired and not pass it on to the base class.Add a new class to your project and copy/paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing the existing one.
要拦截文本框控件中的消息,请从 TexBox 派生一个类并
在此处实施
建议
To intercept messages in textbox control, derive a class from TexBox and
implement
suggested here