获取文本框中所选文本在字符串中的位置

Get position of selected text in string in textbox(获取文本框中所选文本在字符串中的位置)
本文介绍了获取文本框中所选文本在字符串中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在尝试在这里做一些具体的事情.我想在文本框中获取所选文本的位置.

Ok, I'm trying to do something a little specific here. I want to get the location of the selected text in a textbox.

详细说明-我可以使用位置来选择文本.如果我有一个 textBox1 我可以这样做:

To elaborate- I can use location to select text. If I have a textBox1 I could do:

textBox1.SelectionStart = 1;
textBox1.SelectionLength = 4;

这将从第二个字母开始并选择 4 个字母.

That would start at the second letter and select 4 letters.

我想做的恰恰相反:当用户选择文本时,我想找出开始是什么,长度是多少(或者开始是什么,结束是什么.两者都行).

What I want to do is the opposite: when the user selects text, I want to find out what the start is and what the length is (or what the start is and what the end is. Either will work).

我想过只在字符串中搜索选定文本 (textBox1.SelectedText).如果它是一个常用词或多次使用的字符串,就会出现问题.例如.

I thought about just searching the string for the selectedtext (textBox1.SelectedText). The problem comes if it is a common word or a string that is used multiple times. For instance.

这是一只猫.这是一只猫.这是一只猫.

This is a cat. This is a cat. This is a cat.

如果他们选择第二个句子,使用 SelectedText 搜索该特定句子的字符串对我没有好处.它可以是 3 个中的任何一个.

If they select the second sentence, using SelectedText to search the string for that specific sentence does me no good. It could be either of the 3.

所以,我的问题是:当用户点击一个按钮时,我如何确定用户选择的确切元素,以便我以后可以操作这些特定元素?重要的是要注意后面的部分-我可能不仅想在按下按钮时操作文本.我还想稍后再操作它,此时文本可能不再突出显示.这意味着我要存储一些东西来告诉我我正在处理的句子的具体部分.如果该解决方案不可行,是否有您可以想到的解决方案,在上面的这是一只猫"示例中,用户可以选择第二个句子,点击一个按钮,然后我知道选择了哪个句子当他按下那个按钮时?

So, my question is: When the user clicks a button, how do I determine the exact elements that are selected by the user, so that I can later manipulate those specific elements? Important to note the later part- I likely will not only want to manipulate the text when the button is pressed. I will also want to manipulate it later, at a time when the text may no longer be highlighted. This means I'll want to store SOMETHING to tell me what specific parts of the sentence I'm dealing with. If that solution isn't viable, is there a solution you can think of where, in the above "this is a cat" example, the user could select the second sentence, hit a button, and then later I know which sentence was selected when he hit that button?

推荐答案

根据documentation、SelectionStartSelectionLength 都可以设置和读取.就用那些吧.

According to the documentation, SelectionStart and SelectionLength can be both set and read. Just use those.

这篇关于获取文本框中所选文本在字符串中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)