代码之家  ›  专栏  ›  技术社区  ›  Berry Harahap

在RichTextBox中选择单词,右键单击,然后显示在上下文菜单C中#

  •  -1
  • Berry Harahap  · 技术社区  · 12 年前

    程序员,硕士。

    请帮帮我…让我以你为榜样学习。

    说明: 当选择一个单词并右键单击时,该单词会显示在弹出窗口中。 例如 输入:“巴塞罗那是我最喜欢的足球俱乐部”。

    在我选择单词“ 足球运动 ”,然后右键单击,单词显示在弹出菜单中“这是 足球运动 ".

    当我点击弹出菜单中的这些单词时,它将替换INPUT中的单词 与弹出菜单中的单词,如本例所示。

    输出:巴塞罗那是我的最爱 这是足球 俱乐部"

    请帮助我。

    我真的不懂上下文菜单。。

    这是代码:

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

    //接下来。。。,我真的不知道。

    它不起作用。请帮助:):):

    2 回复  |  直到 12 年前
        1
  •  0
  •   internals-in    12 年前

    显示上下文菜单添加

    richTextBox1.ContextMenu = contextMenu ; 
    

    到初始化或在Form1()中

        2
  •  0
  •   Civa    12 年前

    你只是用下面的东西来显示菜单

    contextMenu1.Show(richTextBox1, point);