2012-07-11 10 views
1

在C#中,如何將光標移動到富文本框中的下一個單詞?例如如何將光標移動到C#中富文本框中的下一個單詞#

給出的句子

「他是個男孩」

讓我們假設,目前「他」,我想之前將其移動到的位置之後,前「是」,即光標定位「一」即在「是」之後。

可以richtextbox.SelectionStart用於執行此類操作嗎?

回答

0

,如果你不介意使用的SendKeys然後TEH下面的例子將實現您的要求

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     richTextBox1.Focus(); 
     SendKeys.SendWait("^{LEFT}"); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     richTextBox1.Focus(); 
     SendKeys.SendWait("^{RIGHT}"); 
    } 
} 
+0

非常感謝您的幫助。有效!。實際上,我正在爲URDU文本開發一個標記工具,並且在兩個單詞之間插入標記後,我希望光標在下兩個標記之間自動移動。那麼,用戶可以省去手動移動光標的不方便 – 2012-07-13 06:53:04

1

您應該嘗試使用.Find(Char)方法找到下一個空格的位置。

int x = richtextbox.Find(' '); 
richtextbox.SelectionStart = x; 
richtextbox.Focus(); 

沒有測試它的可能性,但我應該工作。

0

這應該工作:

int curr = richTextBox1.SelectionStart; 

int x = Regex.Match(richTextBox1.Text.Substring(curr), @"\s[^\s]").Index; 

if (x != 0) 
    richTextBox1.SelectionStart = x + curr + 1; 

richTextBox1.Focus(); 
+1

和兩個連續的空格? – Steve 2012-07-11 10:19:50

+0

固定。 @Steve,謝謝 – 2012-07-11 10:41:51

0

如果你的目的是爲了尋找一個字;突出顯示並將光標移動到找到的單詞: 注意:搜索按鈕的工作方式爲「每次點擊」;所以每次點擊它將搜索richtextBox文本文件中的下一個匹配詞。

// Search text and Highlight it (Per Click) 
    // Manually add the "FindText" 
    public int FindText(string txtToSearch, int searchStart, int searchEnd) 
    { 
     // Unselect the previously searched string 
     if (searchStart > 0 && searchEnd > 0 && IndexOfSearchText >= 0) 
     { rtb.Undo(); } 

     // Set the return value to -1 by default. 
     int retVal = -1; 

     // A valid starting index should be specified. 
     // if indexOfSearchText = -1, the end of search 
     if (searchStart >= 0 && IndexOfSearchText >= 0) 
     { 
      // A valid ending index 
      if (searchEnd > searchStart || searchEnd == -1) 
      { 
       // Find the position of search string in RichTextBox 
       IndexOfSearchText = rtb.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None); 
       // Determine whether the text was found in richTextBox1. 
       if (IndexOfSearchText != -1) 
       { 
        // Return the index to the specified search text. 
        retVal = IndexOfSearchText; 
       } 
      } 
     } 
     return retVal; 
    } 

    // Button to Perform the Search (By Click) 
    private void btn_Search_Click(object sender, EventArgs e) 
    { 
     int startIndex = 0; 

     if (txt_Search.Text.Length > 0) startIndex = FindText(txt_Search.Text.Trim(), start, rtb.Text.Length); 

     // If string was found in the RichTextBox, highlight it 
     if (startIndex >= 0) 
     { 
      int x = rtb.Find(txt_Search.Text); 
      rtb.SelectionStart = x; 
      rtb.Focus(); 
      // Set the highlight color as red 
      rtb.SelectionBackColor = Color.LightYellow; // Remove to avoid minor Bug 
      rtb.SelectionColor = Color.Black;   // Remove to avoid Minor Bug 
      // Find the end index. End Index = number of characters in textbox 
      int endindex = txt_Search.Text.Length; 
      // Highlight the search string 
      rtb.Select(startIndex, endindex); 
      // mark the start position after the position of 
      // last search string 
      start = startIndex + endindex; 
     } 

    } 

    // TextBox to Search 
    private void txt_Search_TextChanged(object sender, EventArgs e) 
    { 
     start = 0; 
     IndexOfSearchText = 0; 
    } 
} 

}

希望這有助於。

相關問題