-1

プログラマー、マスター。

私を助けてください...あなたの例によって私に学ばせてください。

説明:単語を選択して右クリックすると、その単語がポップアップに表示されます。たとえば、INPUT:「バルセロナは私のお気に入りのサッカークラブです」。

「サッカー」という単語を選択した後、右クリックすると、ポップアップメニュー「これはサッカーです」にその単語が表示されます。

ポップアップメニューでそれらの単語をクリックすると、この例のように、INPUTの単語がポップアップメニューの単語に置き換えられます。

アウトプット:バルセロナは私のお気に入りですこれはサッカークラブです。」

私を助けてください。

私はContextMenuを本当に知らない。

コードは次のとおりです。

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?
             }
         }
     }
  }

//次へ...、本当にわかりません。

動作しません。助けてください :) :) :)

4

2 に答える 2

0

以下のものを使ってメニューを表示します

contextMenu1.Show(richTextBox1, point);
于 2013-03-16T04:28:37.930 に答える
0

contextMenuを表示するにはadd

richTextBox1.ContextMenu = contextMenu ; 

初期化またはForm1()

于 2013-03-16T04:58:49.773 に答える