获取文本框中字符串中所选文本的位置 [英] Get position of selected text in string in textbox

查看:45
本文介绍了获取文本框中字符串中所选文本的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆