指定双击文本区域或文本框时的突出显示行为?
我注意到许多希望您进行大量文本编辑的应用程序将为双击文本提供非默认行为,因为应用程序会突出显示它认为您最有可能尝试与之交互的文本。
举个简单的例子,这句话在不同的应用程序中表现不同:
这是一个“示例”句子
如果我在记事本中输入该句子,然后双击单词“样本”(最好是单词的中间,例如样本的“m”和“p”之间),那么记事本会突出显示从第一个引用到第二个引号后面的空格(含在内)。如果该句子位于 Visual Studio 中的注释中,并且我双击同一位置,它会突出显示示例中从“s”到“e”的部分,而不突出显示引号。
如何在我自己的应用程序中自定义这些突出显示行为? winform和WPF有什么不同吗?我想我可以破解我的方法,使其在双击事件上工作,但是是否有专门为此目的的更优雅/深思熟虑的解决方案?
I've noticed many applications that expect you to do a lot of text editing will provide non-default behavior for double clicking text in that the application highlights text that it believes you're most likely trying to interact with.
As a quick example, this sentence behaves differently in different applications:
This is a "sample" sentence
If I type that in Notepad and double click the word 'sample' (ideally middle of the word, say, between the 'm' and 'p' of sample) then notepad highlights from the first quote to the space after the second quote inclusive. If that sentence is in a comment in Visual Studio and I double click in the same location, it highlights from the 's' to the 'e' of sample without highlighting the quotes.
How can I customize these highlighting behaviors in my own application? Is it different from winforms to WPF? I suppose I could hack my way to make it work on a double click event, but is there a more elegant/deliberate solution meant exclusively for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,使用 DoubleClick 事件来完成您想要的操作是很混乱的,因为它看起来要进行两次选择,速度较慢,看起来更糟,并且可能会触发不需要的事件/代码。
所以下面的代码至少应该可以解决 Winforms 的问题。创建一个新类,并以通常的方式扩展 TextBox(或 RichTextBox)(使用新创建的控件,该控件应该神奇地出现在设计器中)。我制作了一个简单的例程,您可以在其中指定要使用的分隔符。只要多做一点工作,就可以很容易地考虑一系列字符,而不仅仅是单个字符,甚至可以创建其他更高级的选择方式。
如果您使用的是 TextBox 而不是 RichTextBox,只需删除在类定义中出现两次的“Rich”位即可。
Yes, using the DoubleClick event to do as you want is kludgy as it appears it's doing the selection twice which is slower, looks worse, and could trigger unwanted events/code.
So the code below should do the trick for Winforms at least. Create a new class, and extend the TextBox (or RichTextBox) in the usual manner (using the newly created control which should magically appear in the designer). I've made a simple routine where you can specify the delimiter to use. With a little more work, it should be easy to account for a range of a characters instead of just a single one, or even create other more advanced ways of selecting.
If you're using a TextBox instead of a RichTextBox, simply remove the "Rich" bit that appears twice in the class definition.
DarkPh03n1X 的改进答案几乎对我有用,但是它有一个令人讨厌的错误:如果分隔符可以找不到,
Text.IndexOf(c, start)
将返回-1
,这会将right
设置为-1
然后if (right == -1) right = Text.Length
触发。所以现在我们已经选择到文本末尾,尽管预期的选择应该更短。我认为开始处理得正确。
我删除了
if (right == -1) right = Text.Length
但添加了&&位置!= -1
。这是固定版本:为了验证行为,这是我使用的示例文本:
我添加了一些其他分隔符,请随意选择您需要的。
The improved answer from DarkPh03n1X was almost working for me, however it has a nasty bug: if a delimiter character could not be found,
Text.IndexOf(c, start)
will return-1
which will setright
to-1
and thenif (right == -1) right = Text.Length
triggers.So now we have selected until the end of the text, even though the expected selection should be shorter. The start is handled correctly I think.
I have removed
if (right == -1) right = Text.Length
but added&& pos != -1
. Here is the fixed version:To verify the behavour, here is the sample text I was using:
I have added a few other delimiter, feel free to choose what you need.
我使用等于
[a-zA-Z0-9_]
的正则表达式\w
来替换TextBox
选择原始结果:
这是一个
“样本”
句子使用上述方法
这是一个“
样本
”句子I use Regex
\w
which equal to[a-zA-Z0-9_]
to replaceTextBox
selectionoriginal result:
This is a
"sample"
sentencewith method above
This is a "
sample
" sentence在 DanW 的工作中添加了多个分隔符,这似乎效果很好。
另一件事:要实现将上面的代码粘贴到您要使用它的表单的“代码”中,现在使用“表单设计器”将 RichTextBox 拖放到您的表单上,现在转到表单的设计器类我的案例 form1.desigener.cs 并找到实现字符串,例如:
并将其替换为
此后该控件将像普通的 RichTextBox 实现一样工作,并具有额外的覆盖功能
Adding on to DanW's Work i have added multiple delimiters, that seems to work out nicely.
One more thing: to implement paste the code above into your form's "code" that you are going to use it on,now use the "Form Designer" to drag and drop a RichTextBox onto your form, now go to the form's designer class in my case form1.desigener.cs and find the implementation string eg:
and replace it with
After this that control will work like a normal RichTextBox implementation with the extra overide feature