2013-11-24 23 views
1

我有這樣的代碼:如何在richTextBox中通過鼠標單擊事件選擇一個字符並將其添加到textBox1然後添加到textBox2?

private void richTextBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left) 
      { 
       if (mouseisup == false) 
       { 
        int positionToSearch = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y)); 
        richTextBox1.SelectionStart = positionToSearch; 
        textBox1.Text = richTextBox1.Text.Substring(positionToSearch, 1); 
        previousChar = positionToSearch; 
       } 
       else 
       { 
        currentChar = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y)); 
        if (currentChar > 2 && currentChar > previousChar) 
        { 
         richTextBox1.SelectionStart = currentChar; 
         textBox2.Text = richTextBox1.Text.Substring(currentChar, 1); 
        } 
       } 
      } 
     } 

     private void richTextBox1_MouseUp(object sender, MouseEventArgs e) 
     { 
      mouseisup = true; 
     } 

mouseisup和previousChar ABD currentChar所有在Form1的頂部聲明。 previousChar和currentChar都是int,並在form1的頂部設置爲-1。

我想要做的是,當任何字符/信用戶點擊它會顯示它在textBox1的。這部分工作。 現在我想這樣做,之後用戶選擇一個字符/信及其在textBox1中的下一個字符/字母的用戶會點擊將在TextBox2中和規則應是下一個字符/字母不能是同一臺我的意思是在同一個positionToSearch索引中,它必須多於兩個字符/遠離第一個字母的字母。

例如我點擊了一封信:W 這W是在positionToSearch 432 下一個字符或字母i可以單擊並選擇現在應該是任何其他指數則432,應該是長/遠432由兩個至少。

例如:

如果RichTextBox的,我有這樣的文字:

丹尼斯就在這裏。

我點擊D 下一個字母/字母我將能夠點擊選擇應該是n,我,s,h,e,r,e。 但我不會能不會選擇它顯示在TextBox2中,如果我在同d或下一個接近信/炭它在這種情況下點擊:一

或者例如我有這樣的文字:

丹尼爾在這裏丹尼爾在那裏。

我可以選擇第一個D和第二個D 但我不能選擇第一個D和第一個下一個。 或者我不能選擇我和則S

必須有選擇的字符/字母之間至少有兩個以上的遠選擇。

我的代碼是不工作的。

這是我做過什麼至今:

private void richTextBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left) 
      { 
       if (mouseisup == false) 
       { 
        int positionToSearch = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y)); 
        richTextBox1.SelectionStart = positionToSearch; 
        textBox1.Text = richTextBox1.Text.Substring(positionToSearch, 1); 
        previousChar = positionToSearch; 
       } 
       else 
       { 
        currentChar = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y)); 
        if (currentChar > previousChar + 2) 
        { 
         richTextBox1.SelectionStart = currentChar; 
         textBox2.Text = richTextBox1.Text.Substring(currentChar, 1); 
        } 
        currentChar = -1; 
        previousChar = -1; 
        mouseisup = false; 
       } 
      } 
     } 

     private void richTextBox1_MouseUp(object sender, MouseEventArgs e) 
     { 
      mouseisup = true; 
     } 

但隨着布爾mouseisup的邏輯是錯誤的,因爲在MouseUp事件它每次我點擊一個char或信將是真實的。

這是工作代碼,我想這是對所有的規則:

private void richTextBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left) 
      { 
       if (mouseisup == false) 
       { 
        textBox1.Text = ""; 
        int positionToSearch = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y)); 
        richTextBox1.SelectionStart = positionToSearch; 
        textBox1.Text = richTextBox1.Text.Substring(positionToSearch, 1); 
        previousChar = positionToSearch; 
        textBox2.Text = ""; 
        mouseisup = true;//add this statement 
       } 
       else 
       { 
        currentChar = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y)); 
        if (currentChar > previousChar + 2 || currentChar < previousChar - 2) 
        { 
         richTextBox1.SelectionStart = currentChar; 
         textBox2.Text = richTextBox1.Text.Substring(currentChar, 1); 
        } 
        mouseisup = false; 
       } 
      } 
     } 

     private void richTextBox1_MouseUp(object sender, MouseEventArgs e) 
     { 
      if (textBox2.Text == "") 
      { 
       mouseisup = true; 
      } 
     } 
+0

首先你說「這部分工作。」並且比你說「我的代碼根本不工作」。那麼至少有什麼工作呢? –

+0

我的意思是textBox1的部分正在工作,我點擊一個字符/字母,並添加鼠標到文本框1上的字符/字母。現在第二次點擊不起作用,應該與規則不是相同的字符/字母(索引),而不是下一個字符/字母。如果我有「丹尼爾D」我點擊第一個D我現在不能選擇第一個D旁邊的,但我可以選擇第二個D.你不能選擇相同的索引,而不是下一個字符/字母。 –

+0

編輯我的問題到目前爲止我所做的。 –

回答

0

問題:你是不是在你的if條件塊mouseisup變量設置爲true。如果你不將它設置爲true你將永遠不會進入else塊就是爲什麼你不能看到TextBox2

解決方案的下一個選擇的文本:設置mouseisup變量trueif block 如下:

mouseisup = true; 

完整的解決方案:

private void richTextBox1_MouseDown(object sender, MouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Left) 
    { 
    if (mouseisup == false) 
    { 
     int positionToSearch = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y)); 
     richTextBox1.SelectionStart = positionToSearch; 
     textBox1.Text = richTextBox1.Text.Substring(positionToSearch, 1); 
     previousChar = positionToSearch; 
     mouseisup = true;//add this statement 
    } 
    else 
    { 
    currentChar = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y)); 
    if (currentChar > previousChar + 2) 
    { 
     richTextBox1.SelectionStart = currentChar; 
     textBox2.Text = richTextBox1.Text.Substring(currentChar, 1); 
    } 
    currentChar = -1; 
    previousChar = -1; 
    mouseisup = false; 
    } 
    } 
} 

     private void richTextBox1_MouseUp(object sender, MouseEventArgs e) 
     { 
      mouseisup = true; 
     } 
+0

Sudhakar我以前試過這個,現在你的解決方案。 Thep roblem是它的第一次工作,但在選擇textBox2的字符/字母后,我可以再次選擇它附近的任何字母或textBox1中的第一個字母。這個想法是,它應該是一個塊或鎖定區域。我點擊一個字母/字符,並將其添加到textBox1,然後做文本框2,但如果我現在點擊另一個字母/字母它應該重置一切,再次制定規則,但它dosent我可以點擊任何字符的信件,它會將其替換爲textBox2。 –

+0

問題出在MouseUp事件之後,每次第一次它會成爲true時,因爲它會將allywas到達mouseup事件。因此,即使我在ELSE部分將其設置爲false,它也會因爲我點擊而成爲真實。 –

+0

請加入這個房間http://chat.stackoverflow.com/rooms/41819/discussion-between-sudhakar-tillapudi-and-doron-muzar –

相關問題