2013-03-16 116 views
0

程序員,碩士。在RichTextBox中選擇單詞並右鍵單擊,然後在ContextMenu C中顯示#

請幫助我...讓我通過你的榜樣學習。

描述: 當單詞被選中並右鍵單擊時,單詞顯示在彈出框中。例如, 輸入:「巴塞羅那是我最喜歡的足球俱樂部」。

我選擇的詞「足球」,然後右鍵點擊並字顯示在彈出菜單後,「這是足球」。

當我在彈出式菜單中單擊這些單詞時,它將用彈出式菜單中的單詞代替INPUT 中的單詞,如此示例。

OUTPUT:巴薩是我最喜歡的這就是足球俱樂部「

請...幫助我

我真是瞎了眼文本菜單的..

這裏是代碼。:

ContextMenu contextMenu = new ContextMenu(); 
private EventHandler menuHandler; 

public Form1() 
{ 
    InitializeComponent(); 
    menuHandler = new System.EventHandler(this.Menu_Click);// what's menu_click? 
} 

private void Menu_Click(object sender, EventArgs e) 
{ 
    richTextBox1.SelectionFont = new Font("Times New Roman", 12); 
    richTextBox1.SelectionColor = Color.Black; 

    richTextBox1.SelectedText = ((MenuItem)sender).Text; 
} 

private void richTextBox1_MouseDown(object sender, MouseEventArgs e) 
{ 
    try 
    { 
     if (e.Button == MouseButtons.Right) 
     { 
     Point point = new Point(e.X, e.Y); 
     int index = richTextBox1.GetCharIndexFromPosition(point); 
     textBox1.Text = Convert.ToString(index); 

     int length = 1; 

     if (!Char.IsWhiteSpace(richTextBox1.Text[index])) 
     { 
      while (index > 0 && !Char.IsWhiteSpace(richTextBox1.Text[index - 1])) 
      { index--; length++; } 

       while (index + length < richTextBox1.Text.Length && 
        !Char.IsWhiteSpace(richTextBox1.Text[index + length]) && 
        (!Char.IsPunctuation(richTextBox1.Text[index + length]) || 
        richTextBox1.Text[index + length] == Char.Parse("'")) 
      ) length++; 

       richTextBox1.SelectionStart = index; 
       richTextBox1.SelectionLength = length; 
       contextMenu.MenuItems.Clear(); // error here 
       contextMenu.MenuItems.Add("This is "+richTextBox1.SelectedText, menuHandler); //error here 
       //What's next Sir? 
      } 
     } 
    } 
    } 

//未來......,我真的不知道。

它不起作用。請幫助:) :) :)

+0

你得到了什麼錯誤,你在哪裏聲明contextMenu – Civa 2013-03-16 04:33:21

+0

最新錯誤? – 2013-03-16 04:38:12

+0

先生,contextMenu1不顯示。 「我contextMenu1.Show(richTextBox1,點);」在contextMenu.MenuItems.Add(「.....」)之後。 有什麼建議嗎? – 2013-03-16 04:43:13

回答

0

來顯示文本菜單添加

richTextBox1.ContextMenu = contextMenu ; 

到RHE initalization或在Form1中()

0

你剛剛用波紋管的事情,以顯示菜單

contextMenu1.Show(richTextBox1, point); 
相關問題