如何在文本框中选择特定文本

问题描述:

我有两个文本框,即txtBox1和txtBox2



我在txtbox1中有很多单词



任务是如果我在txtBox2中输入任何单词,那么应该选择txtBox中的单词。





有没有方法这样做。

I am having two text box namely txtBox1 and txtBox2

I am having many words in txtbox1

The task is that if I type any word in txtBox2 then the word in the txtBox should be selected.


Is there any way to do so.

选择由这两个属性控制:

http://msdn.microsoft.com/en-us/library/system.windows.forms .textboxbase.selectionstart(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase。 selectionlength(v = vs.11 0).aspx [ ^ ]。



此信息应足以解决问题。



-SA
Selection is controlled by these two properties:
http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectionstart(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectionlength(v=vs.110).aspx[^].

This information should be quite enough to solve the problem.

—SA


您可以使用substring选项查找正确的字符串



You can use the substring option to find the correct string

string origString = txtOrig.Text;
string findString = txtFind.Text;
// This is just being fancy so that it only checks until less of the origstring is left than the length of the find string
int checkLength = (origstring.Length - findstring.Length)
// this is for the first digit check
string fOSChar = findString.Substring(1, 1); 

for (int s = 0; s < checkLength; s++)
{
    // You do not have to check first if the first digit is the same but this will increase the speed of calculation if needed
   if (OrigString.Substring(s, 1) == findString)
   {
        if (OrigString.Substring(s, fOSChar) == findString)
        {
            txtOrig.SelectionStart = s;
            txtOrig.SelectionLength = findString.Length;
        }
   }
}